Automatically prefix JIRA issue numbers to Git commit messages

Nivrith
Evergreen Engineer
Published in
4 min readOct 30, 2020
Photo by ZMorph Multitool 3D Printer on Unsplash

If you are lazy like me, you know it can get a little annoying having to type the issue number in every commit message. We can easily automate this using the git prepare-commit-message hook which can prepend commit messages with JIRA issue number to the commit message extracted from the git branch name (and get the benefits of Smart Commits). You can use this trick for any issue tracker you might use. Read more to know how to do this.

TICKET-123 — a useful commit message

Naming your git branches

For this technique to work, you need to make sure that you include the issue number in the name of the git branch. You can use the git flow naming convention as long as the ticket number is in the branch name. Here are some examples:

  • AWESOME-1234
  • THING-4456
  • feature/PANTS-1234
  • fix/LOL-1567

Linux/macOS

We can create a prepare-commit-message git hook script to automatically extract ticket number from git branch name and prepend it to the commit message.

Step 1: Create the git hook file

  • Open the .git/hooks directory in your project's root
  • Open prepare-commit-message.sample file with vscode (or your favourite code editor)
  • Remove the sample boilerplate code

Step 2: Add script to be run on commit

  • Copy and paste the following script into the file
  • Rename and save the file as prepare-commit-message (remove the `.sample`)
  • Go through the comments in the script and modify the variables as you deem appropriate
Many Thanks to johncmunson for this public GitHub gist
  • Commit a test commit message

That’s it 🎉🎉🎉 You should see your commit being prepended with the issue number extracted from your branch name

Windows

Photo by Vincent Giersch on Unsplash

On Unix-like operating systems, the #! (she-bang) is called the interpreter directive. This tells the operating system to execute the current file using the path to the interpreter’s executable that follows it. /bin/bash is the path to the interpreter you want to use. In this case, we’re using bash.

This will be meaningless in windows as it is not Unix like. Git for Windows can run Bash commands and shell scripts via Cygwin. We just need to change the interpreter path to make this work.

#!C:/Program\ Files/Git/usr/bin/sh.exe

Note: You need to escape spaces with \ in windows paths

That’s it and your hook should now work on windows too!

JavaScript Projects (Cross Platform)

Photo by Caspar Camille Rubin on Unsplash

The bash script solution will still work for JavaScript projects. But if you want to stay away from bash scripts there is an alternative solution we can use.

We will use husky and jira-prepare-commit-msg as dev dependencies to help us achieve our goal.

  • husky is a handy tool to manage git hooks for JavaScript projects from package.json file (or a config file)
  • jira-prepare-commit-msg is an npm CLI module which does exactly what we want

Step1: Install the dependencies

npm install husky jira-prepare-commit-msg --save-dev

or

yarn add --dev husky jira-prepare-commit-msg

Step 2: Prepare Commit Message hook

Add the following configuration to your package.json file to invoke jira-prepare-commit-msg when the prepare-commit-message is triggered

// package.json
{...
"husky": {
"hooks": {
"prepare-commit-msg": "jira-prepare-commit-msg"
}
}
...
}

Configuration

You can configure jira-prepare-commit-message to use custom message pattern or ticket number pattern. You can also set a comment character and specify if you use conventional commit (which you should!).
You can also see package’s readme here to know more configuration details.

// package.json
{
...
"jira-prepare-commit-msg": { "messagePattern": "[$J]\n$M", "jiraTicketPattern": "([A-Z]+-\\d+)", "commentChar": "#", "isConventionalCommit": false }
...
}

NVM Users Please Note

If you use Node Version Manager (NVM), husky will automatically use the node version that is set by nvm on your shell. If you use a git GUI client like GitKraken or Source Tree, they may fall back to using the default node version. So, you may have to set your default node version to your most used project’s node version. If you know any other solution, I would love to know it in the comments below. Also, if you’re tired of typing nvm use each time you open a terminal in a JavaScript project, this article will show you how to never have to do this again!

Conclusion

Having issue numbers in commit messages can be very useful for tracking commits in your issue tracker. However, manually adding the issue number takes time and is easy to forget. In this article, we learnt how to automate this using prepare-commit-message git hooks.

Many issue trackers like JIRA, GitHub, Azure DevOps support smart commits which automatically associate commit messages or Pull Requests with issue numbers to the appropriate issue. If you want to learn and understand git better, check this book out

Thank you for reading and I hope this helps you as it helped me.

I would love to know in the comments below if this was useful!

You can follow me on GitHub or Twitter
Happy Engineering!

--

--

Nivrith
Evergreen Engineer

Software Engineer living a polymath lifestyle in Adelaide, Australia