DEV Community

Ahmed Saber
Ahmed Saber

Posted on

How to write better Git commit messages ?

Some developers don’t pay attention to naming commit messages, as they think it’s not important, and paying attention to it is just a waste of time, but are they right ? that’s what we are going to talk about right now, so let’s start.

Naming of commit messages is so important if we are going to read them again in the future, but when should we do that ? let’s see some situations in which we need to go back and read old commits.

  1. If I’m working with a team, and I need to know what they have done, so I will expect to find clean commits with descriptive names.
  2. If I need to go back to an old commit to see what exactly has been done within it.
  3. If any issue happened due to a certain commit, so I need to get my whole application to an older version before this commit.
  4. If I’m responsible for code review in my team.

Now we know why we should write clean commit messages, Let’s see how we can do that.

1) The subject should be less than 50 letters.

  • Do: “Implement add to cart functionality”
  • Don’t: “Implement the function responsible for adding products to cart by using the product id”

2) The sentence should be capitalized.

  • Do: “Add cart indicator to navbar”
  • Don’t: “add cart indicator to navbar”

3) Don’t end the sentence with a dot.

  • Do: “Add cart indicator to navbar”
  • Don’t: “Add cart indicator to navbar.”

4) Use a verb in imperative form.

  • Do: “Add cart indicator to navbar”
  • Don’t: “Added cart indicator to navbar”

Finally, there is a great way that can help you write better commit messages, you can measure if your commit message is good or bad with this sentence.

If applied, this commit will “commit message”

  • Good: If applied, this commit will “Add cart indicator to navbar”
  • Bad: If applied, this commit will “adding cart indicator to navbar”

That’s all, now you need to practice what you have learnt, to be able to write better Git commit messages.

You can find & contact me on LinkedIn, GitHub, and Facebook, and here is my portfolio for more details about me.

Top comments (2)

Collapse
 
liborpansky profile image
Libor Pansky • Edited

Even better: use a convention for your commit messages.

If you prefer not to adhere to conventions, I would avoid the Implement keyword, it's usually an implementation of some feature and it's superfluous to mention it. In this case just Add to cart explains well enough the work done.

Collapse
 
shirlei profile image
Shirlei Chaves

And what about the Conventional Commits?