My Global Git Commit Template

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 commit.template

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:

  1. If you use -m or --message to provide the commit message from the command-line.

  2. If you use -c or -C, or their verbose counterparts, --reuse-message and --reedit-message.

  3. If you use -t or --template to 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.

My Template

Here it is:


# Type: Subject

# Body

# GitHub-Issue:
# Ticket:
# Reviewed-By:
# See-Also:
# Fixes:
# Resolves:
# Closes:

# Types:
# * [Feature]
# * [Fix]
# * [Docs]
# * [Style]
# * [Refactor]
# * [Test]
# * [Chore]

I prefix the first line with a ‘Type’, using one of the types listed at the end of the template. For example, here is a commit message for a feature and here is a message for a bug fix.

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.

Conclusion

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.

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s