Exploring Git: Customizing Commit Messages

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

  • 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

  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

  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

Conclusion

pre-commitprepare-commit-msgcommit-msgpost-commit

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

I work in the DC area as a software engineering consultant. I spend most of my time working on fullstack web applications and the big data space.

I work in the DC area as a software engineering consultant. I spend most of my time working on fullstack web applications and the big data space.