DEV Community

Noel Worden
Noel Worden

Posted on • Updated on

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

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:

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:
  • 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.

Top comments (0)