Enroll in Selenium Training

In the tutorial about committing changes, we discussed how important committing with a message is. It is always recommended to use commit messages while you commit the changes you made. In that tutorial, as you recall, we used -m option to type a commit message. In this tutorial, we will focus on the same part about the commit message and discuss

  • Why do we need commit messages
  • How to write good commit messages

Although there is something you should know beforehand.

Pre-Requisites

  • Basic Commit Knowledge - How to Commit your changes

Why do we need commit messages

Committing with message is a very good practice. On a project, many people work together who may or may not be located geographically together. This way there can be happening a lot of commits on a daily basis. While the programmer might know what he is committing, no one else would be really able to figure it out. Figuring out the commit by comparing the two versions of code is cumbersome and time-consuming. Moreover, committing so much daily can also make the programmer forget what he committed. This should not happen in a project of any scale.

Committing with messages helps you look behind the different versions and know what changes did you make. When you commit with the messages, your whole team is able to look back and see the different changes by different members all at once. This point should also be kept in mind while we write the messages which we will see in the next section about how to write good commit messages.

How to write good commit messages?

Just writing the commit message is not enough as you commit your changes in the repository. When you commit your message you should keep few things in mind which will play a key role when someone else reads your messages. You can understand your message but it is not the case with everyone. It is the same as when writing the test cases. The test cases that a tester writes should be understood by everyone in different departments such as development etc. So one by one we will look at the things you should keep in your mind while writing your commit messages.

Keep in mind new developers

This is a very important part of writing the messages and is often the most ignored point by most programmers. When you write your commit message, you should always keep in mind the new developers. By new, I mean new in development as well as new in your team. A developer who has just started using Git and working on projects may get confused easily by looking at the complex messages and abbreviations that you might be using for a long time now.

committing_with_improper_message

A developer who is new to your team may also have trouble decoding the message you just put up while committing. For example, you made a change in the URL, let say now the page About Us is redirected to a new URL. Committing with just the message that /about url has been changed will not help the new developer as he does not know anything about the page and the url. For this you should always write messages keeping in mind new developers.

Git message should be meaningful

The message that you write while committing should be meaningful enough so that no one has to do any hard work in guessing what the commit is all about. A meaningful commit message describes the change that you are committing. For example if you just commit with the message "change in the functionality of the Login button" instead of what change you have done like "Login with more than 32 character password solved" is much more meaningful and understandable by everyone. Also, no one has to do any extra work like asking people or see previous commits to see what functionality has been changed and implemented.

Git message should be short and descriptive (50-72 characters)

The commit message should be short in length. The more advised or recommended length of the commit message is less than 50 characters if possible or else the maximum recommended limit is 50-70 characters only. This does not mean that you try to convert every message to less than 50 characters even though it is ambiguous or maybe not clear and meaningful. Also, the message you write should be descriptive and should describe your actions and changes clearly without any confusion. Although it means that you should be as detailed as possible but you should always remember the 72 character limit.

Keep the Subject and Body separated

The subject and the body of the commit message should be written separately and should always be written to give a detailed description of the changes you have made during the commit. The subject of the message works as a headline of the commit and should not exceed 50 characters. It should be as short (and meaningful) as possible. The body of the commit message should be a detailed description of the subject and should be kept minimum as possible since space is precious to us. Also, remember to separate the body and the subject by the gap of a line.

Use Imperative mood in writing subject line

An imperative sentence is the one that looks like you are giving an order. A sentence such as, "Merging the branch 'Login'" can also be written as "Merge the branch 'Login'" in an imperative way. An imperative way is the default way used by Git itself. So when Git is making any commits on your behalf the commit message it states is like "Merge xyz" or "Make xyz" etc. This might sound like a rude sentence but this is how Git uses it's commit messages.

Add_Dashboard_To_Profile

The above image can be seen with a commit message in the imperative mood.

A simple trick can also be remembered to write the commit messages. In case you are wondering on a subject line, try to fit that subject line in this sentence:

If applied, this commit will <your subject line>

So if you are wondering to write Merging branch Login then try to fit it in this sentence and it will become If applied, this commit will Merging branch Login. And hence this does not make sense. Now try the imperative converted sentence and it becomes If applied, this commit will Merge branch Login and this sentence makes perfect sense. So you can use this simple trick to just never get your message wrong.

Always remember that writing proper commit messages is very important and a very essential part of the total concept of working on Git. It might be a little tough at the start but soon you will have a habit of it. For now, we will move on to our next tutorial.

First Commit In Git
First Commit In Git
Previous Article
Alter Files in Git
Alter Files in Git
Next Article

Similar Articles