loading...

Got 5 Minutes to Spare? Why Not Set up a Custom Commit Message Template?

noelworden profile image Noel Worden Updated on ・2 min read

This Week I Learned (17 Part Series)

1) Beginning of a Blog Series 2) Accessing localhost within a Docker Image 3 ... 15 3) Identifying and Removing Hidden Characters 4) Getting Granular with Git Diff 5) Tweaking Logger Outputs on the Fly 6) Keeping Your Hands on the Keyboard: A Few Bash and Git Shortcuts 7) Breaking Down Elixir's `with` Expression 8) How To Write A Custom Elixir Schema Validation 9) The Handful of Commands I Use When Interactive Rebasing with Vim 10) Got 5 Minutes to Spare? Why Not Set up a Custom Commit Message Template? 11) Improving Your Commit Message with the 50/72 Rule 12) Synchronously Looping Over Two Collections in Elixir 13) How to Utilize Enum.any?, with a Refactoring Twist! 14) A Rabbit Hole of Decimal Formatting 15) More Custom Validation Work: Manipulating a Keyword List To and From a Map 16) Stepped Away for a Long Weekend 17) So Many Ways to Update a Map with Elixir!

A commit message template is one of those things that takes a small amount of effort to setup, but can pay dividends in your workflow. I've been using commit message templates for a while, but revisited the process recently, and thought it would be worth sharing.

By default, if you don't use the -m option while committing, you will be taken to the editor, and presented with this text:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#   (use "git pull" to update your local branch)
#
# Changes to be committed:

This generic message can be updated to suit your specific needs. You could change the existing text file, its found at: ~/.git/COMMIT_EDITMSG. But it's better practice to create a new text file, and set that as the file git uses as the commit message template. These are the steps to do that:

  • Create a new text file

    • I set mine as a dot file and in the home directory:
    touch ~/.customcommitmessage.txt
    
  • Add whatever text helps you make the best commit messages

    • You can pre-fill often used elements, like the prefix of a ticket number
    • Add all the notes and reminders you'd like to see, just begin those lines with a #
    • This is snippet from my personal commit template:
    TICKET-:
    #Add ticket number
    #Answer this question: If applied, this commit will...
    #Description of what and why, not how.
    
  • Once the text file has all the desired text, it needs to be set in the git config:

    git config --global commit.template ~/.testgitmessage.txt
    

Now that it's setup, you'll see it every time you git commit (without the -m option)

While dealing with the git config, if you haven't done so already, you can select what editor you'd like to use for commit messages. If you'd like the editor to be VIM or Emacs just include that in the quotes:

git config --global core.editor "vim"

If you'd like it to be something like VSCode or Sublime, you need to add --wait so that git knows to wait for the data coming back from the editor:

git config --global core.editor "code --wait"

And that's it, with relatively minimal effort you can have a custom commit message template and make that commit message on whichever editor you'd like.


This post is part of an ongoing This Week I Learned series. I welcome any critique, feedback, or suggestions in the comments.

This Week I Learned (17 Part Series)

1) Beginning of a Blog Series 2) Accessing localhost within a Docker Image 3 ... 15 3) Identifying and Removing Hidden Characters 4) Getting Granular with Git Diff 5) Tweaking Logger Outputs on the Fly 6) Keeping Your Hands on the Keyboard: A Few Bash and Git Shortcuts 7) Breaking Down Elixir's `with` Expression 8) How To Write A Custom Elixir Schema Validation 9) The Handful of Commands I Use When Interactive Rebasing with Vim 10) Got 5 Minutes to Spare? Why Not Set up a Custom Commit Message Template? 11) Improving Your Commit Message with the 50/72 Rule 12) Synchronously Looping Over Two Collections in Elixir 13) How to Utilize Enum.any?, with a Refactoring Twist! 14) A Rabbit Hole of Decimal Formatting 15) More Custom Validation Work: Manipulating a Keyword List To and From a Map 16) Stepped Away for a Long Weekend 17) So Many Ways to Update a Map with Elixir!

Posted on May 27 by:

noelworden profile

Noel Worden

@noelworden

Software Engineer in Boulder, CO - Writing code and getting strategically lost in the mountains

Discussion

markdown guide