Use a git hook to match a Jira issue ID from the current branch, and prepend it to every commit message
Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.
-
Create a commit-msg git hook script, and make it executable. From your project's root directory:
install -b -m 755 /dev/null .git/hooks/commit-msg -
Save the following script to the newly-created .git/hooks/commit-msg
#!/bin/sh
COMMIT_FILE=$1
COMMIT_MSG=$(cat $1)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")
if [ ! -z "$JIRA_ID" ]; then
echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
fi
How can I append the issue number in the next line?
switching to :
echo "$COMMIT_MSG $JIRA_ID" > $COMMIT_FILEdoesn't work.