Skip to content

Prefix Git Commit Messages with Branch Information

Edit the prepare-commit-msg hook found in each repository .git/hooks. Alternatively a global config or templates can be created.

sh
#!/bin/bash

MESSAGE=$(cat $1)
TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo '[a-zA-Z]+-[0-9]+' | head -n 1 | tr "[:lower:]" "[:upper:]")

if [[ -z "$TICKET" ]]; then
  # TICKET is empty
  exit 0
elif [[ "$MESSAGE" == *"$TICKET"* ]]; then
  echo "Ticket found in commit message. Skipping..."
  exit 0
fi

echo "$TICKET: $MESSAGE" > $1

The branch is matched against the regex '[a-zA-Z]+-[0-9]+' and the commit message is then prefixed by that result "$TICKET: $MESSAGE".

Examples:

Executing git commit -m "much code, very changes" would result in the following messages...

branch namematchcommit message
omg/refactoring-everythingn/amuch code, very changes
feature/foo/bar/SCHMU-1_lorem-ipsum-larum/devSCHMU-1SCHMU-1: much code, very changes
feature/dev/ema/lol-232-consectetur-adipiscing-elitlol-232LOL-232: much code, very changes
feature/SPO-23411SPO-24311SPO-24311: much code, very changes
YOLO-42-live-fast-die-youngYOLO-42YOLO-42: much code, very changes