Why are they so important?
Have you ever asked a question and in return received a vague answer? Seeing a non-descriptive commit message can be just as frustrating.
When working with a team, communication is key. That is why a good commit message is very important. Anyone looking at your commit history should have a clear idea what was done with each commit. You do not want to post a commit message such as, “Fixed it.” What is exactly did you fix? Instead you should put a message that described what you fixed, such as “Fix incorrect error message output with invalid username/password combinations”.
What exactly is a commit message?
A git commit saves changes to code. Before version control software, you may save revisions of your software by having multiple copies of the entire directory such as: my_app_v1, my_app_v2, myapp_v2minorfix, myapp_final. This approach is unwieldy and comparing differences between versions is difficult. Commits will save a revision of your code and git provides a history of each commit so changes can be easily viewed, or reverted.
For more information about git history, click here.
For more information about reverts, click here.
Guidelines for a good commit message
Listed below are a few guidelines you should keep to heart when writing your commit messages. You never know who will be looking at your past commits!
Please, do keep in mind these are not my rules but a general rule of thumb that many go by.
1. Capitalize the subject and paragraph line
Not much to explain here since it is pretty straightforward. Make sure you capitalize your subject and paragraph lines.
git commit -m "Capitalize subject line"
2. Don’t end a subject line with a period
Also fairly straightforward although I am guilty of this myself. This is a subject line, not a sentence. A period has no place here!
3. Use an imperative mood in subject line
What the heck is an imperative mood? This is the definition given by Wikipedia:
Given this definition here are a few examples of a commit message with an imperative mood:
4. Wrap subject line at 50 characters and body at 72 characters
Why would you want to wrap at specific characters? You see, a git log does not wrap any commit messages for you. Meaning that any long messages you write will start flowing off of the screen of the log.
Why 72?
The given explanation is that on an 80 column terminal you would want to subtract about 4 columns from the left and right for any indentations and to make it look symmetrical.
5. Body should be used to explain what/why
Let me clarify this. In your body of the commit you not only want to explain what changes you have made but also why. Remember, what I said earlier? Communication is key.
When writing a body do not go into detail on how those changes are made. We want to our commit messages to be detailed, but not overly detailed. Keeping to the what/why will be beneficial for anybody working with you and even the people just looking at your code/repo. This will save you and your team much time that could have been wasted on reaching out to you to get answers.
Conclusion
A commit message is a vital part of staying in communication with your fellow workers/teams. It does not take much effort to write a good commit message, as they say “A little goes a long way”. As long as you keep to these small details and guidelines your commits will be sure to impress anybody working with you!