A Command Line Reminder for Better Commit Messages

The quality of my commit messages has been inconsistent for too long now. I can write good commit messages, but I still fail to do so about thirty percent of the time.

Bad commit messages are short and non-descriptive. They’re quick to fire off in the moment, but when reviewing later, they’re essentially useless for knowing what was committed. You’ve got to open the diff and read the commit’s code directly, which is the tedious step a good commit message is meant to save us from in the first place.

A Quick and Dirty Solution

So the goal is to improve the quality (and consistency of quality) of my commit messages. The first idea that came to mind was to use pre-commit git hooks: I could write a script that would ask me if I used a good commit message. If I answered “yes”, the commit would go through; if not, the commit would not go through, and I’d be prompted to commit again with a better message.

I started tinkering with this on my own, but it turns out Victoria Drake already made a great pre-commit hook that does just what I wanted.

I tweaked her original bash script a bit to make a simpler prompt. I eventually arrived at this:

Installation is easy:

  • In any git repo you’re working in, simply head to the git hooks directory: cd .git/hooks.
  • Then create the pre-commit hook file: touch pre-commit.
  • Add the bash script’s contents to that file.
  • Finally, make the script executable: chmod +x .git/hooks/pre-commit.

The Prompt in Action

Here’s a short video showing the prompt:

Leave a Reply

Your email address will not be published. Required fields are marked *