Skip to content

Instantly share code, notes, and snippets.

@luc-tielen
Last active April 28, 2026 15:07
Show Gist options
  • Select an option

  • Save luc-tielen/2890b3a2403ca0ad3ed15ac5470eedaf to your computer and use it in GitHub Desktop.

Select an option

Save luc-tielen/2890b3a2403ca0ad3ed15ac5470eedaf to your computer and use it in GitHub Desktop.
Pre-commit hook for adding ticket automatically to commit messages
# Save this to your git repo at .git/hooks/commit-msg
#!/bin/bash
set -e
FILE=$1
MESSAGE=$(cat "$FILE")
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Extract ticket from branch: handles feat/PRJ-123-... or PRJ-123-... patterns
TICKET=$(echo "${BRANCH#*/}" | grep -oiE '^[A-Z]+-[0-9]+' | tr '[:lower:]' '[:upper:]' || true)
# Only prepend if we found a ticket and message doesn't already start with one
if [ -n "$TICKET" ] && ! echo "$MESSAGE" | grep -qE '^[A-Z]+-[0-9]+:? '; then
echo "$TICKET: $MESSAGE" > "$FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment