In the past I discussed Git commit templates. In today’s short post I want to share with you the template I use globally for all my projects.
First the Configuration
Update: Thanks to bankslain on Reddit for correcting me regarding
First you need to set a global value for the
commit.template property, giving it the name of the file that is your template; I used the filename
.git-commit-template, so I configured it like so:
git config --global commit.template "/home/eric/.git-commit-template". Whatever you decide to name it, Git will use that as the initial message for every commit you create. However, there are three semi-common exceptions by which Git will not use your template:
If you use
--messageto provide the commit message from the command-line.
If you use
-C, or their verbose counterparts,
If you use
--templateto instruct Git to use a different commit template.
Otherwise Git will use the contents of
$HOME/.git-commit-template (or whatever filename you set to
commit.template), so that’s where you want to put your template. Simple and straightforward.
Here it is:
# Type: Subject # Body # GitHub-Issue: # Ticket: # Reviewed-By: # See-Also: # Fixes: # Resolves: # Closes: # Types: # * [Feature] # * [Fix] # * [Docs] # * [Style] # * [Refactor] # * [Test] # * [Chore]
The ‘body’ is simply that, the bulk of the commit message.
Everything that comes after the body are lines of meta-data which I sometimes include in commits. For example, if the commit fixes a ticket on GitHub then I will un-comment
Fixes: and add the GitHub issue number; when GitHub sees a commit message like that it will automatically close the issue, so that’s useful. And sometimes I use
See-Also: to provide URLs to places where developers can find more information about libraries (for example) used in the commit.
That’s it! If you frequently use Git then you may find a global template like this to be helpful. Add the things you often use, commenting them out so they don’t accidentally end up as part of the commit. And then un-comment them as needed.