Commit message templates
Tags: git, workflow
Published: 2019-10-14 22:00:28

---
title: Commit message templates
published: true
tags: git, workflow
canonical_url: https://thomashartmann.dev/blog/commit-message-templates 
cover_image: https://thomashartmann.dev/images/commit-message-templates.svg
---
<p>Here’s a statement for ya: if you’re working on a project with someone else, you <em>should</em> be using a commit message template. Not only will it help increase the consistency and usefulness of your messages, but also, and perhaps more importantly, it will reduce the cognitive load required to write a message and the time it takes to write it. I can only speak for myself, but I know that having to go look up information about how a commit message should be structured and what it references causes some much unneeded context switching, resulting in a less efficient workflow.</p>
<h2 id="setup">Setup</h2>
<p>Setting up commit message templates requires only a few steps:</p>
<ol>
<li>
<p>Create a template.</p>
<p>You can use any text file as a template; just put what you want to show up in the commit message buffer in there. Plain lines of text will show up as content, while lines starting with the comment character (<code>#</code> by default) show up as comments.</p>
</li>
<li>
<p>Add it to the git config as a commit template: <code>git config commit.template path/to/file</code>.</p>

</li>
<li><p>Done! Now try committing something.</p></li>
</ol>
<hr>
<p>When doing this, you’re assigning the template locally (to the current repo). If you’d rather do it globally, pass the <code>--global</code> option.</p>
<p>If you want to share the template across multiple projects in a certain subdirectory, you can do that by combining the template with git’s functionality for conditional includes. Be aware, however, that given a relative path to a file, git will look for the file relative to the including configuration’s location. While this can potentially be exploited to use different templates for different projects, I’ve not found the need for it thus far, and have stuck with a single template with an absolute path.</p>
<p>If you want to unset the template, use the <code>git config --unset</code> functionality:</p>
```sh
git config --unset commit.template
```
<h2 id="commit-template-ideas">Commit template ideas</h2>
<p>Different teams and projects have different needs when it comes to commit messages, so there’s no ‘one size fits all’, but here’s a few ideas based on message formats in teams I’ve been a part of.</p>
<h3 id="label-listing">Label listing</h3>
<p>If you label your commits with the type of work they contain to and have a defined set of labels (e.g. ‘feature’, ‘bug’, ‘chore’, ‘refactor’, etc.), it might be a good idea to list all the different options in the template, so that you can look through them whenever you’re committing some code. List them using commented lines and you don’t even have to delete them; they’ll just show up as extra info in the commit buffer.</p>
<p>While experienced members of the team are likely to know these by heart, it’s very helpful for a new person to have the list right there in the commit buffer so that they don’t have to go look them up.</p>
<h3 id="issue-identification">Issue identification</h3>
<p>If the standard says to have an issue/ticket ID listed in the commit message, you could put a little placeholder in the template that tells you to replace it with the relevant ID.</p>
<p>If you also work with feature branches and name them according to the feature/issue ID that they correspond to, you could even have git fill in this issue number automatically by using hooks, as explained in my previous post. I have found this to save me a surprisingly large amount of time and mental overhead.</p>
<h3 id="very-specific-formats">Very specific formats</h3>
<p>For when your message needs to match a very specific format and it’s hard to remember exactly what goes where, templates are a perfect solution.</p>
<p>For instance, if you’re working with a combination of the above two, where you want both the label <em>and</em> the ID, and you want them in a specific order, maybe even with a specific separator (<em>seems like a hassle, but just roll with it</em>), you could create a template that you could just fill in with the relevant parts, saving you having to look up what the format is every time.</p>