How to apply a global Git commit template
- Published at
- Updated at
- Reading time
- 2min
When you commit things in Git via the CLI, you'll be greeted with something that looks like this:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Changes to be committed:
# new file: ...
You'll write your commit title on the first line, hit save, close the file, and go on with your nerdy day.
The lines starting with a #
will be ignored and not included in your commit data. As you see, most lines in this boilerplate message start with a #
. I think they're there to enable you to make a good commit. It's good to know which files will be included and double-check that you're on the correct branch, right?
But what makes a good commit?
Creating good commits is complicated because every project/team has its own rules. Some bet on Gitmoji. Some want you to include a ticket number. Others might prefer lengthy explanations of the changes to generate an automated changelog.
Today I learned that Git supports commit message templates to make your life easier if you're facing specific commit requirements.
Run this command...
git config --global commit.template ~/.gitmessage
... to add this line to your general
file...
[commit]
template = ~/.gitmessage
... and create a ~/
file...
# Yo, waz up! Dude, remember to follow these conventions (👇) , okay?
#
# 1. do this!
# 2. do that!
# ...
... that contains your custom commit template.
Now, you'll be greeted with your custom message when you make a commit.
Pretty sweet!
Setting a global commit template isn't very user friendly. It'd be great to use Git's includeIf
to set up project-dependent commit templates somehow, but I'll leave this for another day.
Join 5.7k readers and learn something new every week with Web Weekly.