How to apply a global Git commit template (#tilPost)

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….


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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.

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.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2025-02-07T23:00:00+00:00) How to apply a global Git commit template (#tilPost). Retrieved from https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/

MLA
" » How to apply a global Git commit template (#tilPost)." Stefan Judis | Sciencx - Friday February 7, 2025, https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/
HARVARD
Stefan Judis | Sciencx Friday February 7, 2025 » How to apply a global Git commit template (#tilPost)., viewed ,<https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to apply a global Git commit template (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/
CHICAGO
" » How to apply a global Git commit template (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/
IEEE
" » How to apply a global Git commit template (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/. [Accessed: ]
rf:citation
» How to apply a global Git commit template (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2025/02/07/how-to-apply-a-global-git-commit-template-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.