Skip to content

Instantly share code, notes, and snippets.

@KeyboardCowboy
Last active December 15, 2015 19:19
Show Gist options
  • Save KeyboardCowboy/5310979 to your computer and use it in GitHub Desktop.
Save KeyboardCowboy/5310979 to your computer and use it in GitHub Desktop.
Automatically add ticket numbers to GIT commit messages using branch descriptions and GIT hooks.
#!/bin/bash
#
# Automatically add ticket numbers to commit messages using branch descriptions.
# Place this file in .git/hooks/commit-msg
#
# Add a description to your branch:
# git branch --edit-description [branch-name]
#
# Mark your ticket numbers anywhere in the description using the following
# syntax:
#
# [#12345678]
#
# Ex.
# This branch is for tickets [#12345678], [#12345679] and
# [#23456789].
# Get the branch name
NAME=$(git branch | grep '*' | sed 's/* //')
# Get the branch description
DESC=$(git config branch."$NAME".description)
# Grab all the ticket strings out of the description.
TICKETS=$(echo $DESC | grep -o '\[\#[0-9]*\]' | tr '\n' ' ')
# Prepend the ticket numbers to the commit message
echo "$TICKETS"$(cat "$1") > "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment