Last active
April 28, 2026 15:07
-
-
Save luc-tielen/2890b3a2403ca0ad3ed15ac5470eedaf to your computer and use it in GitHub Desktop.
Pre-commit hook for adding ticket automatically to commit messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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