Skip to main content

Command Palette

Search for a command to run...

Commit Like A Pro

Published
4 min readView as Markdown

Remember your first open-source contribution? You typed git commit -m "your commit", pressed enter and suddenly an error showed up with a suggestion link:

And all of a sudden your all-git knowledge just crashed and you started thinking "Isn't it the way to commit a message?"

Yes, it is the correct way no doubt about that. Still, when you contribute to a project on GitHub or any repository, the maintainers of those repositories expect you to make your commit message more concise, to the point and crisp, which not only helps them to maintain the changelog but also helps you and the other contributors to know precisely what is going on.

Why Use Conventional Commit?

  1. Everyone can easily understand the changes by simply exploring the commit history.

  2. Following a rule can make things more systematic and understandable. Otherwise, if everyone starts writing commits without any rule, they can write a paragraph about the changes they have made which will eventually make things messy and may everyone not understand the changes at all.

  3. Trigger build and publish processes.

  4. Automatically generates CHANGELOG.md file.

CHANGELOG is a log or a record of all the changes you made to a project. You can either create a file and save all the record there or you can auto-generate your changelog from the commit messages.

An easy way create an changelog is :

git log --pretty=-%s > CHANGELOG.md

where %s means the commit title.

The conventional way is:

type(scope): description

- type: Commit Type like build, ci, docs, feat, fix etc.

- scope: Phrase describing a section of the codebase.

- description: description should be in the present tense. Not capitalized. No period in the end. Each description consists of a Title, Body and Footer. The Title and Body are mandatory if the description is large enough. The footer is optional.

The command is:

git commit -m "type of commit(optional scope): description title/description" -m "description body(optional)" -m "description footer(optional)"

Example:

git commit -m "docs(guide): change in how to write a professional commit" -m "add rules, specification, example, type"

Rules & Specification:

  • A commit must be prefixed with a type and an additional scope(which is optional), then a colon after that, then the message describing the changes you made and at the very last a footer.

  • There must be a space between the colon and the message (this space is mistaken by people commonly and they get errors).

type(scope): description
  • All words should be lowercase.

  • No dot (.) at the end of the description.

  • Use imperative verbs while writing the commit message like add, fix, solve not adding, fixing, solving etc.

  • Commit must be a brief and clear message that describes what changes you have made.

  • The commit title should not be more than 50 characters, and the commit body should be restricted to 72 characters.

  • Do not use filler words and phrases like though, maybe, kind of etc.

  • The footer should contain additional issue references, BREAKING CHANGE, and external links about the code changes.

Type of Commits:

  • feat – use to introduce new features with the changes

  • fix – use to fix a bug or error

  • chore – use when changes that do not relate to a fix or feature, for example updating dependencies etc.

  • refactor – use when performs changes to the code that neither fix a bug nor add a feature such as restructuring the code

  • docs – use when updates the documentation such as a README or other markdown files

  • style – use when changes that do not affect the meaning of the code, which are likely related to code formatting such as white space, missing semi-colons, and so on.

  • test – use when including new or correcting previous tests

  • perf – use for implementing performance improvements

  • ci – use for performing changes, refer to continuous integration or delivery process

  • build – use, for changes that affect the build system or external dependencies

  • revert – reverts to a previous commit

Some examples of good commit messages:

  1. Commit message with no body or footer

    docs: correct spelling in README.md file

  2. Commit message with a body

    style: change page style

    add change home page style after login

  3. Commit message with a body and footer

    fix: solve hover issue

    see the issue for details

    fixes issue #999

  4. Commit message with scope

    docs(guide/migration): fix spelling mistake

How a Good Commit History looks like

Git commits history of Angular.js GitHub repository

S

always kept up with the conventionals in my repos! Also, do you think that the types are enough and you must stick with them or you can standardize them for every project?

1
S

So, are these enough? Yes, I guess, because I tried to cover the most and these are the standardized ones as far as my experience, but you may find some extra types depending on the project, but still, it will not be much.

And the next question is- if you should stick with them or standardize them, where I am having a little difficulty in understanding the words stick and standardize, are you meaning renaming the existing types?

1
S

Sulagna Ghosh Yep. I was wondering if somehow you cannot (or struggle to) categorize a certain thing that you did in the project.

For example: if I have a React component and I add a stylesheet to it, this will fall under feat. But when I'm going to change some properties inside the styling file, this will fall under fix or refactor?

So I thought that inside your team, you can extend a little bit the description of every type, just to have more defined boundaries for every type, or add more type in order to better categorize your work :)

1
S

Simone Aiello That is an excellent point. Often this seems confusing because it is just not about using a type but where to use what type. So, of course, adding some extra examples or some explanation, like you gave in your last answer, can give everyone enough clarity to choose the types and obviously covers some extra lengths. And really thanks to you for pointing that out, I am going to add some extra examples for better clarity. Thank you.

1
P

A good commit message helps provide context and clarity to the changes made in a code repository.

1
S

True

D
Deep Roy3y ago

Keep up ✌🏻✌🏻

1
S

Thanks