Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:10
  • 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 Pivotal Tracker story id to git commits
#!/usr/bin/env ruby
# Git "Prepare Commit Message" Hook Script
# Description
This script will automatically add the correct Pivotal Story ID to the
beginning of each commit message when the branch name has the Pivotal Story 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/pivotal-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_1234567 => '[#1234567] commit message'
# Author
This is just converted to Ruby by lwalen.
message_file = ARGV[0]
message =
matches = /^\[#([0-9]{7,16})\]/.match(message)
if matches.nil? || matches[1].nil?
current_branch = `git rev-parse --abbrev-ref HEAD`
matches = /([0-9]{7,16})/.match(current_branch)
if !matches.nil? && !matches[1].nil?
message = "[##{matches[1]}] #{message}"
File.write(message_file, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment