My commit message workflow

Shreyas Minocha - Jun 22 '18 - - Dev Community

This article was originally published on http://shreyasminocha.me/blog

Commit message example

I totally obsess over commit messages. I often spend minutes staring into space trying to come up with the best way to summarize the changes a commit brings. I religiously follow the seven rules of good commit messages. As you might have guessed, I've spent some time developing a workflow for writing commit messages.

I use Sublime Text 3 for most of my text-editing needs. I've also set it as my core editor.

git config --global core.editor "subl -n -w"
Enter fullscreen mode Exit fullscreen mode

Note: The above requires the you to install the subl command if it doesn't work out of the box. Installation instructions: macOS · Linux · Windows.

Earlier, I used to use a package to provide syntax highlighting for commit messsages. However, build 3170 has brought native support for various git formats, commit message included.

Sublime allows you to override settings for specific syntaxes. You can edit these from ‹Preferences› → ‹Settings – Syntax Specific›. Here's my Git Commit.sublime-settings file:

{
    "rulers": [50, 72],
    "spell_check": true,
    "word_wrap": "true",
    "wrap_width": 72,
    "font_size": 14,
    "draw_centered": true
}
Enter fullscreen mode Exit fullscreen mode

Note: VS Code fans can do it this way.

Somewhere on the internet, I found a template for commit messages:

# If applied, this commit will…


# Explain why this change is being made

# Provide links to any relevant tickets, articles or other resources
Enter fullscreen mode Exit fullscreen mode

This template makes it easier to frame commits in accordance with the seven rules I mentioned earlier. I can't seem to remember where I found this, but in my attempts to trace it, I found a blog post providing a very similar template. As it turns out, git allows you to use a text file as a template for your commit messages.

git config --global commit.template "/Users/example/dotfiles/commit-msg-template"
Enter fullscreen mode Exit fullscreen mode

When I started using this template, I had a small pet peeve about my setup. Running git commit would fire up Sublime with the cursor on the first line and to actually write the message, I would have to move the cursor one line below. With research and some experimentation, I solved the issue:

git config --global core.editor = "sublime -n -w $1:2"
Enter fullscreen mode Exit fullscreen mode

The $1:2 at the end of the value opens the argument with the cursor on row 2.

Another one of my pet-peeves with the commit message text is this little snippet above the commented out git status:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
Enter fullscreen mode Exit fullscreen mode

While helpful to the newbie, this little notice ended up becoming an annoyance. I found a helpful answer on StackOverflow which advised using a global prepare-commit-msg hook. If you don't already have a global git hooks directory, create one and tell git about it:

git config --global core.hookspath "/Users/example/dotfiles/git-hooks"
Enter fullscreen mode Exit fullscreen mode

Create prepare-commit-msg in said directory with the following content:

#!/usr/bin/env bash

sed -i.bak '/^# Please/,/^#$/ d' $1 && rm $1.bak
Enter fullscreen mode Exit fullscreen mode

Edit: This snippet originally used perl followed by grep. Thanks @shaiay and @jwmevans_77's for your suggestions in the comments.

Make sure the hook is set to be executable with chmod +x. Now that pesky help notice will bother you no more.

Relevant xkcd

Recently, I found this really cool utility which allows you to validate commit messages from the command line. I use a modified form of the utility in a global commit-msg hook to automatically validate every commit I make. Create commit-msg in your global git hooks directory:

#!/usr/bin/env bash

commit-msg file $1
Enter fullscreen mode Exit fullscreen mode

Note: The above requires the commit-msg command to be on your $PATH. Install it using npm i -g commit-msg manually from source code.

Again, make sure the script is set to be executable. The result of this hook is that the commit would abort if my commit message does not meet the criteria.

An example of an invalid commit message

Thanks for reading!

Further reading

. .
Terabox Video Player