Better Git Commit Messages with commit message template

0
100
git commit message
Photo by Yancy Min on Unsplash

If you have been using git and looking around the open source community, you might have noticed some common patterns in the way commit messages are written. 
Writing good commit messages is something that can help teams of any size, big and small overcome some key challenges like managing software projects better, work fast on the branch out-code-push and merge workflow, rolling back versions, keep track of the features and bug fixes and so on
One simple thing that we can all do to start writing better commit messages is to use a template with some structure across the team to write commit messages which takes less time to read and also keep track of what are the works that are being done currently, like lets say if we are working on bug fixes, features or refactoring.

Enough rambling, now lets get something done. To set a commit message template create a text file named .gitmessage.txt in your Home directory on Linux. 

You can then copy and paste the following template that I use:

#The summary line should start with type.
#The type can be Feat (new feature), Fix (bug fix), Docs (documentation change), Style #(formatting, missing semi colons, etc; no code change), Refactor (refactoring code), Test (adding #tests, refactoring tests; no code change) and Chore (updating build tasks, package manager #configs; no code change)

#The summary line should be descriptive but succinct. Ideally, it should be no longer than 50 #characters. It should be capitalized and written in imperative present tense. It should not end #with a period since it is effectively the commit title.

#Summary line should be followed by a blank line followed by a more thorough description. It #should be wrapped to 72 characters, explaining why the change is needed, how it addresses the #issue and what side-effects it might have.

#If a commit A depends on commit B, the dependency should be stated in the message of commit A. #Use the SHA1 when referring to commits in footer.

#Type: Summary line

#Description

#Footer

Before I continue this is not something that I have come up with on my own. This has been advocated by many people across the open source community. And from what I have heard, the 72 character limitation actually comes from the early days of Linux OS open source community where Linus Torvalds had a monitor that could only display 80 characters.

The next thing that you need to do is set this text file as your template for commit messages:
git config --local commit.template "path_to_template_file/filename"

To check if it worked you can run: 

git config --get commit.template

If you can see the path to your file then you have successfully set the commit message template

The other common thing that I wanted to point out is using git commit -m to write your commit message has led to a misconception that commit messages should be a single line. This is not right, we can write multi-line or even multi-paragraph git commit messages.

Instead you should start writing your commit message using the command git commit only and not the -m flag. Running git commit command should now open the template file in the text editor that you have set. Run git config -get core.editor to see which text editor is set. To set text editor of your liking, you can use git config --global core.editor <name_of_favorite_text_editor> . 

For further reading on how to categorize your commit messages onto different type , you can have a read at the following links:

https://udacity.github.io/git-styleguide/

https://github.com/agis/git-style-guide

I hope this was something useful for you. If you have any queries regarding this do let me know. Thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here