Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

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 .gitconfig file...

[commit]
  template = ~/.gitmessage

... and create a ~/.gitmessage 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.

Git commit in the CLI including a custom Git commit message

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.

If you enjoyed this article...

Join 5.7k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles