Insert JIRA Issue Key in Git Commit Message

To create a link between your Git commits and their Jira issue, developers must include the issue key into their commit comment. Often, developers forget to add the issue key to their commits. Instead, we use a Git commit hook to prefill the commit message with the issue key automatically.

A common Git commit convention is beneficial for releases (release notes) of your project.

Git Flow

The most common commit conventions follow this pattern:

<type>[optional scope]: <description> 

[optional body] 
[optional footer] 

Our development workflow follows a typical scheme. First, we create a new issue in Jira. From there, a new branch is created in Bitbucket. Second, the developer clones the repository, implements his feature, pushes the changes, and finally opens a pull request in Bitbucket.

Feature branch in Jira
Create feature branch in Jira

Git Commit Hook

The following script has to be put manually in a prepare-commit-msg file in the .git/hooks/ folder located in your cloned Git repository. Git executes it whenever you run git commit.

#!/bin/bash

# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`

# search jira issue id in a pattern such a "feature/ABC-123-description"
jiraId=$(echo $branchName | sed -nr 's,[a-z]+/([A-Z]+-[0-9]+)-.+,\1,p')

# only prepare commit message if pattern matched and jiraId was found
if [[ ! -z $jiraId ]]; then
 # $1 is the name of the file containing the commit message
 sed -i.bak -e "1s/^/\n\n$jiraId:\n/" $1
fi
#!/bin/bash

# get branch name
branchName=`git rev-parse --abbrev-ref HEAD`

First, we get the current branch name.

In this example, the command results in feat/ITBC-900-use-artifactory-for-go-modules.

# search Jira issue key, such as "feature/ABC-123"
jiraId=$(echo $branchName | sed -nr 's,[a-z]+/([A-Z]+-[0-9]+)-.+,\1,p')

Next, we pipe the branchName variable to the sed command.

We search for a regular expression within the message using the -r argument.

The given pattern [a-z]+/ is rather general as it searches for lines starting with a lowercase word followed by a slash. To make it more restrictive, you should search for occurences of feat/bug/hotfix/release/etc.

The next part of the branch name is of the form ([A-Z]+-[0-9]+), that is a word and digits seperated by a minus sign.

In the example, jiraId is ITBC-900. Use the -n option along with the /p print flag to display only the replaced lines.

# prepare commit message if pattern matched and issue key was found
if [[ ! -z $jiraId ]]; then
 sed -i.bak -e "1s/^/\n\n$jiraId:\n/" $1
fi

When the pattern is found in the branch name, we precede the commit message with the branch name.

Again, we use the sed command. -i is used to add the branch name in front of the message. 1s means that we only insert in the first line. Here, $1 is the name of the file containing the commit message.

Enfore Git Commit Style

If you do no want to rely on the goodwill of your developers, you can use the commitlint NPM package.

It helps your team adhering to a commit convention.

This package allows you to define rules for common Git commit styles, such as the allowed scopes, minimal and maximal length of body, footer, and subject.