TIL: Creating Git Commit Message Template

Today I learned how to create a template for Git’s commit message from thoughtbot’s blog.


Running $ git commit opens an editor window with some default text in it. Normally we would put our commit message at top of the file where the empty line is.

We can create a template for the space in the commit window with the text we want.

First, we would need to create a text file with our custom messages. I want to start the commit message with #TICKET_ID to refer to the related ticket in my ticket system. Then follow with the commit summary.

Filename can be anything but I like to name it .gitmessage.

$ touch .gitmessage
'#[TICKET_ID] - [COMMIT SUMMARY]
(empty line)
  • Note: I prefix the line with ' to prevent Git to see it as a comment. (A line started with # is excluded from the commit message.)
  • I also add an empty line at the end to separate my template with Git’s default template.

Then we update the Git’s config commit.template to point to the file.

$ git config commit.template .gitmessage

Then when we run $ git commit, the editor window is now pre-filled with our template.

git commit with template
commit message with pre-filled template

I prefer to have the same template for all repositories, so move the template to my home folder and make it a global configuration.

$ mv .gitmessage ~/
$ git config --global commit.template "~/.gitmessage"

Note: the template file will not work with the shorthand command $ git commit -am.


Using tools to build a better habit

We can see this feature of Git as a tool to help us to build a better habit of writing good commit messages.

Instead of saying “I will try to write better commit messages”, having this pre-filled template every time we going to make a commit can help us to try more. Because some of it is already there and we will see it always. It enforces us just a little more.

Of course, we can delete the whole pre-fill text and go back to the bad habit like putting “fix bugs” or “update code” as a commit message. But it is less likely to happen. We typically don’t want to destroy our own good will to build a better habit.

Be sure to check Matija Marohnić’s post: The Importance of Git History and some of the even better templates for the commit message from r00k’s tweet.

Related Posts