DEV Community

Cover image for Git Commit Message Template in Terminal and VS Code
Abbey Perini
Abbey Perini

Posted on • Edited on

Git Commit Message Template in Terminal and VS Code

Recently I've been trying to write better commit messages. With my ADHD, my motto is always be writing it down, so I was delighted when my coworker told me about git commit message templates.

To start, I distilled down what I was looking to put in every message:

I also liked Lisa's template's wrap lines, in which she put an octothorpe in the last valid column before git would terminate the line.

Based on that, I created a .gitmessage file in my home directory:

<type>: <title>
# No more than 50 chars. #### 50 chars is here: #

<body> 
# Wrap at 72 chars. ################################## which is here: #

Issue #
Enter fullscreen mode Exit fullscreen mode

Then I ran

git config --global commit.template ~/.gitmessage
Enter fullscreen mode Exit fullscreen mode

If you wanted to do that all in one line in the terminal, you would run:

printf "<type>: <title>\n# No more than 50 chars. #### 50 chars is here: #\n\n<body>\n# Wrap at 72 chars. ################################## which is here: #\n\nIssue #" > ~/.gitmessage && git config --global commit.template ~/.gitmessage
Enter fullscreen mode Exit fullscreen mode

If you want to, you can remove the --global flag from the command and create a different git commit message template for each repository you have. If the path to the .gitmessage file is not absolute, it will be treated relative to the repository root.

All of this works great ...if I was usually running git commit in the terminal. I'm used to using VS Code's Source Control panel when I commit, and the commented out prompts in the message template don't show there. I totally could alias something in my .zshrc like:

alias commit="git add . && git commit && git push && git pull"
Enter fullscreen mode Exit fullscreen mode

...but I've grown fond of the little commit check mark. Instead, I found two commit message extension options.

The Commit Message Editor has a UI within VS Code and clear instructions. After you click the pencil icon in the source control panel, you can choose between a traditional git message template style or a form.

VS Code Git Commit Editor has an external UI to help you configure settings, a lot of options, and would appeal to someone looking for more automation. To get a similar template up and running, I had to add the following code to my settings.json.

{
  ...
  "vscodeGitCommit.template": [
    "{type}: {title}\n\n{body}\n\nIssue #{issue}"
  ]
}
Enter fullscreen mode Exit fullscreen mode

Then, if you click the chat icon in the source control panel, you will be prompted to enter values for anything in curly braces in that string.

Pretty neat! I ended up going with the Commit Message Editor because I want the commented out prompts, and now I'm covered in the terminal and in my text editor.

Top comments (5)

Collapse
 
harithzainudin profile image
Muhammad Harith Zainudin

This is cool! Thanks for sharing. It is really useful and neat. I should try this!

Collapse
 
backslashbaker profile image
Derek Baker

This was a great read. Thank you

Collapse
 
mazzgrey profile image
Manuel Z

Loved this!

Collapse
 
bpugh profile image
Brandon Pugh

Today I learned about the term "octothorpe" šŸ™‚

Thanks for this! Those are some handy extensions!

Collapse
 
migui3230 profile image
migui3230

Awesome article! also how do i make it so it automatically commits after the commit message is written