Posted on

Commit Templates

On a lot of our commit messages we add a ‘Redmine-Ticket’ line with the appropriate ticket number. On some projects we also add a ‘Version’ line naming the build for which that patch is associated (using tags would be better for that in my opinion, but that’s a different subject). So most commits on one project have two lines like so:

Redmine-Ticket: 1763
Version: 6.80

Typing that over and over can get old, but fortunately it can be automated. Git allows you to specify a template file for commits, which you can do globally or per-repository by configuring the value of ‘commit.template’, which should name the template file. Let’s say I make a file called ‘ProjectCommit.template’ with these contents:

Redmine-Ticket:
Version: 6.80

Then I can do this inside the repository:

git config commit.template ‘ProjectCommit.template’

Now whenever I commit anything, Git will automatically use the contents of that file as my initial commit message, which saves me the trouble of having to type that stuff out. I just add the ticket number and an appropriate message.

You do not have to commit these templates to use them. You can make your own template and then add a line in the file .git/info/exclude to ignore it locally. Or for certain projects it may be valuable to have a template as part of the repository that everyone uses.