Thursday, June 11, 2020

How to write good Git Commit messages

What is Git?

Git is an open-source distributed version-control system for tracking changes in the source code during software development. It is designed for the sole purpose of coordinating work among the programmers, but it can be used to track changes in any set of files. The goal of developing this software includes speed, data integrity, and support for distributed, non-linear workflows.

What is commit in Git?

After you do changes or add something in your code file, you do "Git commit."
Git commit sets a message about the changes done to the code. The commit also saves a revision of the code so that changes can be reverted when required.

In simple words, commit adds the latest changes of the source code to the repository, making these code changes as the head revision of the repository. The commit command is usually accompanied with -m, i.e., commit message command, which is used as a place to add a description of the changes made.

Writing a right commit message

Most of the new developers often overlook the format of the commit messages. They need to understand the fact that adequately formatted messages can do much more than just looking neat, such as
  1. Provide Code In-depth to the code reviewer - A right commit message provides the necessary information to the reviewer about what the commit is about or what are exact changes made without getting to the depth of the code. This saves a lot more time and acts as a plus point in increating your impression.
  2. Excellent communication between the team - Most of the projects involve multiple members/groups, and communication is a thing that is to be kept in mind while writing commit messages. A right commit message helps in better understanding between the different members of the group and helps in faster development of the application.
  3. Maintaining the track of features - When the application grows on such an extensive scale, tracking of feature changes is essential. Also, While writing release notes before publishing, the final developer can often look at the history of commit messages to have a broad idea of the features added/removed and modified since the last release.

How to write the right Commit Message?

Till now, we have seen why the right commit message is essential, and soon we will head over and see how to write a right commit message.

Try to use a single line.

Most of the commit messages are short enough that they don't require more than one line. For example,
Add authentication to user routes

But Sometimes, if you feel that the message is too long to fit in two lines, then what you should do is write your subject first line and then the message as a body. Separate the subject and message with a line break. For example,

Switch app architecture to MVVM

Convert components with memory management issue from MVP to MVVM arch
This gives other developers the option to see a git log with a detailed description using "git log or log with just the subject line git log --oneline

Subject limit - 50 chars


Ensuring that the subject text remains under 50 characters enables the coder to think of a brief and concise way of what their code implements. Github, by default, has made 72 characters as the max limit, and it truncates the rest of the text with the ellipsis. Also, it gives a warning when the text crosses a max threshold of 50 chars. So it is considered good practice to keep your subject under 50 letters and don't even pass 72 even if it is required.

Don't use Full Stop at the end.


Do not end the subject with a full stop; it is considered as a bad practice.

Use Present Tense While framing your message.


Think of each commit in your code as a change that is being applied to your codebase. Each commit in your codebase is used to apply the change. Few examples are
  1. Remove a feature
  2. Add a feature
  3. Update a feature
Now I would like to show how most of the new coders type a commit message.
  1. Removed a feature
  2. Added a feature
  3. Updated a feature
These messages are different from the first set of messages. These types of messages are called indicative moods (i.e., past tense). We have to write the message in the Indicative mood(i.e., Present Tense)

By default, Git uses an imperative mood to write and report its actions. Some examples are Git add, git merge master and git push origin master

To avoid any confusion, I am giving you some examples of the best way of committing messages.
  1. If applied, this commit will integrate payment integration.
  2. If applied, this commit will update the camera feature.
  3. If applied, this commit will remove the database connectivity.
Disclaimer- This rule is to be used only in the subject of the message. You subject any tense in the main message.

Conclusion


Time to look at what we learned, committing the right message, is essential for the proper understanding, Maintainance, and communication of the code. We also learned about the rules we should obey while writing a commit message. I am sure after reading this article; you will be able to write a perfect commit message for your next project. However, if you liked this article, consider bookmarking it.

Do you want me to write more such informative articles?
Previous Post
Next Post

post written by:

Hi, Navjyot Singh is a coder, content maker and a freelance developer who's pursuing an undergraduate Engineering degree in Computer Science. He started out as a web developer but later picked up the mobile as his favourite platform to develop applications. A Writer by day and coder by night is loathed to discuss himself as the third person but can be persuaded to do so from time to time.

0 comments: