Skip to content

Instantly share code, notes, and snippets.

@axxapy
Last active December 2, 2025 13:46
Show Gist options
  • Select an option

  • Save axxapy/2062b1faea3c1db93d0b to your computer and use it in GitHub Desktop.

Select an option

Save axxapy/2062b1faea3c1db93d0b to your computer and use it in GitHub Desktop.
Git hook which adds ticket number extracted from branch name (like TICKET-123_comment) to every commit message.
#!/bin/bash
#
# This hook adds ticket number from branch name to every commit message
#
BRANCH_NAME=$(git symbolic-ref --short HEAD)
MSG=$(head -n 1 $1)
# Check if commit message starts with "fixup!", if so, do nothing and exit
# https://git-scm.com/docs/git-commit/2.32.0#Documentation/git-commit.txt---fixupamendrewordltcommitgt
[[ "$MSG" =~ ^fixup! ]] && exit 0
[[ -z "$BRANCH_NAME" ]] && exit 0
if [[ "$BRANCH_NAME" =~ ^(master|main)$ ]]; then
APPEND="[${BRANCH_NAME}] "
else
#Darwin comes with version of sed from dated FreeBSD. commented code works only with gnu-sed
TICKET=`echo "$BRANCH_NAME" | sed 's/\([A-Z][A-Z]*[A-Z]-[0-9][0-9]*\).*/\1/'`
[ -z "$TICKET" -o "$TICKET" = "$BRANCH_NAME" ] && exit 0
APPEND="[$TICKET] "
fi
[[ "${MSG:0:${#APPEND}}" == "$APPEND" ]] && exit 0
sed -i.bak -e "1s#^#${APPEND}#" $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment