paint-brush
Enhancing Your Git Commit Messagesby@rafaelcpalmeida
2,116 reads
2,116 reads

Enhancing Your Git Commit Messages

by Rafael AlmeidaJuly 25th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, I'm going to show you how to use git hooks to automatically prepend the ticket ID you're working on to your commit message. Every time that you create or clone new repositories your commit messages will be appended with ticket ID based on the branch that you're using. branch names similar to: Feature/ABC-1234-Feature-testing-testing.                .                ™™™: Git hooks are scripts that Git executes before or after events such as: commit, push, and receive.
featured image - Enhancing Your Git Commit Messages
Rafael Almeida HackerNoon profile picture

Photo by Yancy Min on Unsplash

If you're reading this chances are that you're already aware of what git is and you also work with any ticket-based software, like Jira. In this article, I'm going to show you how automatically prepend the ticket ID you're working on to your commit message, assuming that you're using you're branch names similar to

feature/ABC-1234-Feature-testing
.

To achieve this, you're going to use git hooks.

Git hooks

According to Git Hooks:

Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally.

Now that you know what you need, just create a new file under

.git/hooks
folder. Name it
prepare-commit-msg
.

Copy the code below and paste it into your newly created file.

#!/bin/bash

# List the branches that don't apply bellow
if [ -z "$BRANCHES_TO_IGNORE" ]; then
  BRANCHES_TO_IGNORE=(master develop staging test)
fi

# Pick the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_IGNORED=$(printf "%s\n" "${BRANCHES_TO_IGNORE[@]}" | grep -c "^$BRANCH_NAME$")

# Remove the unnecessary parts
TRIMMED=$(echo $BRANCH_NAME | sed -E -e 's:^(\w+)\/([^-]*-[^-]*)-.*:\2:' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')  

# If it isn't excluded, prepend the part that interests us to the given commit message
if [ -n "$BRANCH_NAME" ] &&  ! [[ $BRANCH_IGNORED -eq 1 ]]; then
  sed -i.bak -e "1s/^/$TRIMMED - /" $1
fi

After the creation of the file, you'll need to give it its proper permissions. This can be achieved by executing

chmod +x prepare-commit-msg
.

If you want to see it in action just make a new commit and type any message you want. You should see now, given the branch name above, something like:

commit 976f4354779a824e5edfd851857b26c9bcfd3e14 (feature/ABC-1234-Feature-testing)
Author: Rafael <[email protected]>
Date:   Tue Jul 23 17:35:25 2019 +0100

    ABC-1234 - This is a testing message

Apply your changes globally

Now that I've shown you how to improve your commit messages you'll want to apply the condition to every other repo in your machine, right? I'm going to show you how to achieve it.

The first step is to create a folder that's going to contain all of your hooks. I'm using this repo I've created but you can use whichever you prefer. After having your own folder you execute the following command:

git config --global init.templatedir '~/projects/git-hooks-templates'

Within you should have another folder named

hooks
, this is where we're going to place our git hooks.

Now that the folder structure is correct you can copy the file created above into the, in this example (yours might differ)

~/projects/git-hooks-templates
directory. The command to execute after is:

chmod a+x ~/projects/git-hooks-templates/hooks/prepare-commit-msg

Now you have everything set up. Every time that you create or clone new repositories your commit messages will be appended with the ticket ID based on the branch that you're working on.