Including JIRA Issue Number in Commit Messages

Git Hooks to the Rescue

Posted by David Van Loon on April 28, 2020

The project on which I am currently working requires by convention that all commits associated with a specific JIRA issue have messages that begin with the issue number, followed by a colon and the rest of the commit message.

Over time, I’ve grown tired of typing (and mistyping) the issue number for each commit message, and I’ve decided to automate the process with a script.

The obvious place to add this functionality is in a git hook. Git hooks are scripts that are called by git at specific times, and they allow the user to customize the behavior of git. Learn more about git hooks specifically here.

Due to integration between JIRA and Bitbucket on my project, each git branch already includes the issue number of the related JIRA issue. Thus, my git hook runs at commit time and checks the branch name for a JIRA issue number. If present, it makes sure my commit message begins with the issue number.

I’ve used PowerShell Core as a cross-platform scripting environment, so the script should work on Windows, OSX, and Linux with minimal modifications.

After a bit of experimentation, I settled on the following:

I placed the script in the git hooks directory for my project, and it ran on my next commit.

To use with your project, place the script in <project_directory>\.git\hooks\commit-msg.ps1 and ensure it has execute permissions. You may also need to update the first line with the correct path to your PowerShell executable.