Commit Template for Better Commit Messages

Writing good commit messages should be an everyday commitment, but it can be sometimes difficult to stay self-disciplined, expecially on your personal project or during rush hours. This behaviour is well represented by xkcd:

Git commit by xkcd: http://xkcd.com/1296/

Source: http://xkcd.com/1296/

A good solution is to create a default commit message, which will be used every time a commit is supposed to be done. Thanks to this message template you get used to the format and keep a good habit, and the configuration is pretty easy to do.

The configuration steps are well described on the git-scm website, chapter 7.1 Customizing Git – Git Configuration, section commit.template

Create a template

Create a file at $HOME/.gitmessage.txt if you want it global, or in your project’s folder if you want it specific to your project.
In this file, specify the template you would like to see everytime you commit.

Example (Not saying it’s the best one):

1
2
3
[theme] subjectline (keep it short)

WhatHappened

Some discussion about the message template and what a commit message should be can be found on the Erlang/Otp wiki or on the tbaggery blog

Configure Git

Then configure Git to use you brand new template, using the commit.template parameter.

If it’s for all your project, run:

1
git config --global commit.template $HOME/.gitmessage.txt

If it specific to your Git based project directoy, assuming you have saved the template in this directory:

1
2
cd project_directory/
git config --local commit.template .gitmessage.txt

Commit

And then commit as usual but without using the -m flag, so your default $EDITOR will show up with the text from the template prepopulated.

If you keep replacing the values and filling the blanks of the template, you will quickly get use to the format. It’s then a matter of time before you disable the template and keep going with your new good habit.

Comments