Skip to content

Instantly share code, notes, and snippets.

@jared-christensen
Forked from bartoszmajsak/prepare-commit-msg.sh
Last active August 4, 2023 08:11
Show Gist options
  • Save jared-christensen/f9db573183ba76c12b6125eda6125ebc to your computer and use it in GitHub Desktop.
Save jared-christensen/f9db573183ba76c12b6125eda6125ebc to your computer and use it in GitHub Desktop.
How to automatically prepend git commit with a branch name
#!/bin/sh
#
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.
# check if commit is merge commit or a commit ammend
if [ $2 = "merge" ] || [ $2 = "commit" ]; then
exit
fi
ISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+" | grep -o "[A-Z]\{2,\}-[0-9]\+"`
if [ $? -ne 0 ]; then
# no issue key in branch, use the default message
exit
fi
# issue key matched from branch prefix, prepend to commit message
sed -i -e "1s/^/$ISSUE_KEY /" $1
@jared-christensen
Copy link
Author

jared-christensen commented Apr 20, 2021

Run this command to add the script to the hooks folded and fix the permission.
curl https://gist.githubusercontent.com/jared-christensen/f9db573183ba76c12b6125eda6125ebc/raw/e02ab3b97ac930ab5c2ddc343fb54ebddd85ee96/prepare-commit-msg.sh > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment