Exploring Git: Customizing Commit Messages

Jake Henningsgaard
3 min readDec 21, 2016

--

I recently stared playing with Git hooks and thought it might be useful to customize my Git commit messages. The default commit message that appears in the editor was not terribly helpful. It also doesn’t do much in the way of enforcing some sort of commit message convention.

Commit Message Convention

There are many ideologies on what a good commit message should look like. Based on my own experience and some advice found here and here, this is what I believe a good commit message should include:

  • Subject line should be a summary of changes in 50 characters or less
  • The body of the commit message should focus on why the changes are being made as well as how the changes address the issue and any implications or side effects caused by the change
  • If a lot of explanation is necessary format the message so that its clear and easy to read (i.e. bullet points, new line separation between paragraphs, etc.)
[JCH-296] - Fixed the 404 error for the Document download linkWhy: When viewing a pdf document in a users profile the document will preview but the download link is displays a 404 error. The link to the database was broken and needed updating.How:  
* Created variable to store static database path
* UserID & document name is appended to database path variable to retrieve document
Side Effects: This a static fix, may need to retrieve the database location dynamically to support future database changes/migrations.

Create a Template

This step is not necessary but, it allows you to replace the default commit message prompt with something more useful to your team. Like…some guidelines for a good commit message.

  1. Create a template file (e.g. ~/.git-commit-message)
Why:
*
How:
*
Side Effects:
*

2. Set the commit template in the .gitconfig file: git config --global commit.template ~/.git-commit-message. This will insert the following configuration in your .gitconfig file:

[commit]
template = ~/.git-commit-message

You can of course omit the --global option and just configure for an individual repo.

Insert Issue Name & Number

It can be helpful to include the issue and/or branch name in the message subject line. In my case, I usually create a new branch with the name of the issue I am working on (e.g. JCH-296). I can then use a script found here to get my branch name as the beginning part of the commit message.

  1. Create a directory to store your hooks: mkdir ~/hooks
  2. Create file titled prepare-commit-msgin the hooks directory.
  3. Make the file executable: chmod 755 prepare-commit-msg
  4. Copy the following script into prepare-commit-msg:
# Ignore branches with specific names
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
# Get the name of the current branch
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
# Check if current branch should be ignored
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
# Add branch name to commit messages
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] - /" $1
fi

Test

After you’ve saved the prepare-commit-msg file you can then cd into your Git repo and test out your new customization. Create or change a file in your repo and run git commit and you should see your new template (if you created one) and the addition of the branch name at the beginning of the commit message.

Conclusion

This is just an introduction into what you can do with Git commit messages. I also found it helpful to know exactly what order the commit hooks are executed. I encourage you to read more but, here is a quick reference:

pre-commitprepare-commit-msgcommit-msgpost-commit

Additionally, there are several more advanced features you can add using Git hooks.

--

--

Jake Henningsgaard

I work in the DC area as a software engineering consultant. I enjoy playing with emerging technologies and exploring their potential.