DEV Community

Rafael Almeida
Rafael Almeida

Posted on • Originally published at hackernoon.com

Enhancing your git commit messages

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
Enter fullscreen mode Exit fullscreen mode

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@example.com>
Date:   Tue Jul 23 17:35:25 2019 +0100

    ABC-1234 - This is a testing message
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (7)

Collapse
 
rbukovansky profile image
Richard Bukovansky • Edited

Serious question: If the name of the branch already has the ticket number, because you create one branch per ticket, why would "poison" commit message with the same information?
If you are worried that with merge to develop branch and following deletion of branch you lose that information then make sure to include that ticket number in the merge commit message. You will have then focus of commit messages in the branch just only on what's really going on in the code.

Collapse
 
rafaelcpalmeida profile image
Rafael Almeida

Hey Richard,

Well, I also share the same opinion, however, as we currently use Jira + GitHub, if you add your ticket ID to each of your commit messages you’ll see on Jira the number of commits. As I hate repetitive tasks I decided to come up with this ✌️

Collapse
 
rbukovansky profile image
Richard Bukovansky

Hi Rafael,
oh, I see... So you are not using JIRA+BitBucket on premise... Carry on... :)

Collapse
 
kbiedrzycki profile image
Kamil Biedrzycki

What value can you see by the number of commits?

Thread Thread
 
rafaelcpalmeida profile image
Rafael Almeida

Actually, I’m not sure 🤔 I reckon that management use them for performance analytics or so

Collapse
 
zejnilovic profile image
Saša Zejnilović

Hello,

as much as I like your post, the sed did not work for me. Now is because my machine is compromised in some way or is there a typo? I am not a sed master so can't tell.

$> echo "feature/444-ala-bala-dala" | sed -E -e 's:^(\w+)\/([^-]*-[^-]*)-.*:\2:' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
FEATURE/444-ALA-BALA-DALA

Otherwise, it made me think about a lot of interesting things. Thank you.

Collapse
 
rafaelcpalmeida profile image
Rafael Almeida

Hey Saša,

It appears to be something wrong with your Terminal. Take a look at mine:

~/projects/git-hooks-templates  master ✔                                                                                                                                                                     11m  
▶ echo "feature/444-ala-bala-dala" | sed -E -e 's:^(\w+)\/([^-]*-[^-]*)-.*:\2:' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
444-ALA