Ben Newton

Ben Newton

Never Make Another Crappy Commit Message

It’s late. You’ve been through post after post on StackOverflow. You can’t find anything that resembles your bug. Hours pass and behold, you have solved your issue and you are high-fiving yourself for finally figuring it out.

Now you have to commit the code to the repo before before you fall asleep at your desk. It’s time to enter your commit message…

>git commit -m "fixed bug"_

No! What? You won’t even remember what you did when you wake up in the morning much less 6 months from now.

I’ve been guilty of this, especially on my personal projects. Occasionally I’ll dress it up by describing the bug.

>git commit -m "Fixed F'ing Bug"_

What should be in a commit message?

Simple, the commit message is a short description of the changes. Something that you and future you will understand as well as other teamates on the project.

Now, I realize most commits speak for themselves, a reason for a color change in CSS file or a property update in a JSON file may seem obvious to you. But future you will not be that smart. No matter how simple something may seem, future you may think a change was made for a completely different reason then you meant. So a description is a necessity.

So how do you make a good commit every time? Remind yourself.

The Commit Template

I’ve been using git for years and I never used a custom commit template before. Then I came across this article on DEV.to and I realized this was just what I needed to kick myself at each commit so I didn’t continue to enter useless commit messages.

How I set up My Git Template

Set up the template in ~/.git-commit-template.txt and copied the contents of this Gist to the file.

# <type>: (If applied, this commit will...) <subject> (Max 50 char)
# |<---- Using a Maximum Of 50 Characters ---->|
# Explain why this change is being made
# |<---- Try To Limit Each Line to a Maximum Of 72 Characters ---->|
# Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue #23
# --- COMMIT END ---
# Type can be
# feat (new feature)
# fix (bug fix)
# refactor (refactoring production code)
# style (formatting, missing semi colons, etc; no code change)
# docs (changes to documentation)
# test (adding or refactoring tests; no production code change)
# chore (updating grunt tasks etc; no production code change)
# --------------------
# Remember to
# Capitalize the subject line
# Use the imperative mood in the subject line
# Do not end the subject line with a period
# Separate subject from body with a blank line
# Use the body to explain what and why vs. how
# Can use multiple lines with "-" for bullet points in body
# --------------------
# For more information about this template, check out
# https://gist.github.com/adeekshith/cd4c95a064977cdc6c50

I then ran the following which will tell git to use the above file as the commit message template each time you commit.

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

I will tweak the contents of the template to my own liking depending on the project I’m on.

Notes

Notice the ”#” before every line? That is a commented line and will not be included in your commit message.

You can even include a Git template to be used with the repo if you want to require everyone to use it.

The one down side I have run into is when using VSCode, I like to use the included git tools and the input provided for the commit message is only one line. It does not show the commit template. I wish this was configurable, but I have been unable to find a way to change it. So until I do, I have to get in the habit of running git commit -m from the command line.

I hope this helps you get just a step closer being an organized developer that your team is thrilled to have on board.

Other Resources

Better Commit Messages with a .gitmessage Template

by Matt Sumner


Git commits: An Effective Style Guide

by Paul Isaris

comment

Comments

arrow_back

Previous

How I Added Additional Post Types to My GatsbyJS Site

Next

How To Handle Pesky Modals In Your Puppeteer Tests
arrow_forward