Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Git Hooks
# Adapted from original source:
# Docs:
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.
COMMIT_MSG_FILE=$1 # $1 is the file name containing log message
COMMIT_SOURCE=$2 # $2 is the commit message source (message | template | squash | commit)
SHA1=$3 # $3 is the commit SHA-1
# Check a commit ammend
if [ $COMMIT_SOURCE = "merge" ] || [ $COMMIT_SOURCE = "commit" ]; then
# Ex.) feature/TICKET-1234
# - ISSUE_KEY = "TICKET-1234"
ISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+" | grep -o "[A-Z]\{2,\}-[0-9]\+"`
# If no issue key in branch, use the default message
if [ -z $ISSUE_KEY ]; then
# Issue key matched from branch prefix
# Prepend ticket ID to commit message
sed -i -e "1s/^/$ISSUE_KEY: /" $COMMIT_MSG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment