Skip to content

Instantly share code, notes, and snippets.

@tomviner
Forked from oryband/prepare-commit-msg
Last active November 5, 2023 14:27
Show Gist options
  • Save tomviner/faf373e5c05a8cb4bc49c048504dbea6 to your computer and use it in GitHub Desktop.
Save tomviner/faf373e5c05a8cb4bc49c048504dbea6 to your computer and use it in GitHub Desktop.
Appends branch name to git commit message. `cp prepare-commit-msg .git/hooks/prepare-commit-msg`

Clone

git clone [email protected]:faf373e5c05a8cb4bc49c048504dbea6.git git-hook-prepare-commit-msg
#!/bin/bash
# prepends branch name and description to commit message
# https://gist.github.com/tomviner/faf373e5c05a8cb4bc49c048504dbea6/
#
# put this in repo/.git/hooks/ and chmod +x the file
# see the following stackoverflow question about how
# to do this for every new/cloned repo:
# http://stackoverflow.com/a/8842663/207894
# fetch branch name after last instace of '/'
FULL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
BRANCH=${FULL_BRANCH##*/}
# don't process if branch in excluded list
if [ -z "$GIT_BRANCHES_TO_SKIP" ]; then
# default skip list is master and rebase branches
# list can be set outside if this script
GIT_BRANCHES_TO_SKIP=(master HEAD staging);
fi
EXCLUDED=$(printf "%s\n" "${GIT_BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH$")
# skip if commit message is non-empty (e.g. with --amend or --message)
# or if excluded branch,
# or if branch was already added
if [[ -z $(head -n1 $1) && $EXCLUDED -eq 0 && $(echo $1 | grep -c "$BRANCH") -eq 0 ]] ; then
sed -i.bak "1s/^/$BRANCH /" $1
fi
@alexpeits
Copy link

still using that daily 😹

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