Need of Good Commit Messages

Introduction

Commits can be thought of as snapshots or milestones along the timeline of a Git project. Commits are created with the git commit command to capture the state of a project then.

git commit -m "commit message"

Whenever we made some progress in a project and we want that change to reflect to the end-user or so that other people can work on top of it we commit these changes to a feature branch and push our code, eventually ending up raising a PR to the development branch. So we have updated the previous version of the application here using version control (GitHub).

Commit Message

Now we have a good understanding of what commits are and what they do. Now Let's see what is a good commit message and what is a bad one.

To understand this Scenario, Let's take a situation where you are working in a team of 15-20 people and your team is building a communication platform, everyone working on the different features, testing it simultaneously and you being new to the team did multiple code changes and the application crashes for some edge cases which you didn't took care of. Now some senior member of your team asked you to revert your changes so for the time being the application will be up again and here comes the role of a commit message.

Scenario 1

If in your commit message, you have written something like "made changes" or "file changes done" or anything this is not give meaning to what feature you have worked on it will be a bit difficult for you or even the other team members to make sense of the work you had done. So might be they have to go through the complete Pull Request code and see what changes are there. This is an example of a bad commit message.

Scenario 2

If in your commit message, you have written something like "bug fix for username validation" or "added validation to the customer phone number" or anything other than that makes sense to the feature or bug you fixed. This will be very helpful and anyone will be able to understand what changes are there in the PR. This is an example of a Good commit message.

How to write a Good Commit message?

A Good Commit Message can be split into 2 parts :

1) The Category

2) The part of the code which performed the action

Here is a list of some categories:

  • feat: this is used when a new feature is added.

    example:- feat: added two-way communication

  • fix: this is used when a bug is fixed.

    example:- fix: validation for username

  • docs: this is used when some documentation changes are done.

    example:- docs: README.md file is updated

  • style: this is used when some CSS changes are performed.

    example:- style: used chalk library

  • refactor: this is used when there are code changes that are neither a bug fix nor a new feature, just structuring the code in a better format.

    example:- refactor: remove comments from the file.

  • chore: this is used when there is no production code change.updating some package versions etc.

    example:- chore: configure gitignore

  • test: Adding test cases or refactoring test cases, basically everything related to testing.

    example:- test: added test cases for two-way communication

When you look at the above examples you will notice something common in all the messages, colon, this is used to separate the category and the summary of the action performed in the category.