Writing good commit messages

last updated: 18th June,2021

What is a commit message?

The commit command is used to save changes to a local repository after staging in Git. However, before you can save changes in Git, you have to tell Git which changes you want to save as you might have made tons of edits. A great way to do that is by adding a commit message to identify your changes.

Commit Options

Why should you write good commit messages?

You might say, "It's just a personal project." Yes, you work alone now, but what happens when you work with a team or contribute to open source?

A well-crafted Git commit message is the best way to communicate context about a change to other developers working on that project, and indeed, to your future self.

Have you ever tried running git log on one of your old projects to see the "weird" commit messages you have used since its inception? It can be hard to understand why you made some changes in the past, and you'll wish you read this article earlier :).

Commit messages can adequately communicate why a change was made, and understanding that makes development and collaboration more efficient.

template of a good commit message

  1. Specify the type of commit:
    • feat: The new feature you're adding to a particular application
    • fix: A bug fix
    • style: Feature and updates related to styling
    • refactor: Refactoring a specific section of the codebase
    • test: Everything related to testing
    • docs: Everything related to documentation
    • chore: Regular code maintenance.[ You can also use emojis to represent commit types]
  2. Separate the subject from the body with a blank line
  3. Your commit message should not contain any whitespace errors
  4. Remove unnecessary punctuation marks
  5. Do not end the subject line with a period
  6. Capitalize the subject line and each paragraph
  7. Use the imperative mood in the subject line
  8. Use the body to explain what changes you have made and why you made them.
  9. Do not assume the reviewer understands what the original problem was, ensure you add it.
  10. Do not think your code is self-explanatory
  11. Follow the commit convention defined by your team

Conclusion

The most important part of a commit message is that it should be clear and meaningful. In the long run, writing good commit messages shows how much of a collaborator you are. The benefits of writing good commit messages are not only limited to your team, but indeed expand to yourself and future contributors.