How to write better Git commit messages ?
Table of contents
No headings in the article.
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.
The 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.
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.
If I need to go back to an old commit to see what exactly has been done within it.
If any issue happened due to a certain commit, so I need to get my whole application to an older version before this commit.
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 the 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 the 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 learned, 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.