Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Automatically add JIRA task id to git commits
#!/usr/bin/env ruby
# Git "Prepare Commit Message" Hook Script
# Description
This script will automatically add the correct JIRA task ID to the
beginning of each commit message when the branch name has the JIRA task ID.
It can be overridden if specified in the message.
# Installation
For one project:
Put this file in: <repository>/.git/hooks/prepare-commit-msg
For all projects:
* Run the following commands:
mkdir -p ~/.git_template/hooks
cd ~/.git_template/hooks
ln -s /path-to-scripts/jira-prepare-commit-msg prepare-commit-msg
* Add the following to ~/.gitconfig:
templatedir = ~/.git_template
* In all of your git project folders, run `git init` to
load the hook.
branch-name_ABC-123 => 'commit message [ABC-123]'
# Author
This is converted to Ruby and JIRA by lwalen.
message_file = ARGV[0]
message =
pattern = '([A-Z]+-[0-9]+)'
matches = /#{pattern}/.match(message)
if matches.nil? || matches[1].nil?
current_branch = `git rev-parse --abbrev-ref HEAD`
matches = /_#{pattern}/.match(current_branch)
if matches && matches[1]
message = "#{message.strip} [#{matches[1]}]"
File.write(message_file, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment