Skip to content

Instantly share code, notes, and snippets.

@oryband
Last active October 20, 2017 13:46
Show Gist options
  • Save oryband/da880c74b6b8e7433adf to your computer and use it in GitHub Desktop.
Save oryband/da880c74b6b8e7433adf to your computer and use it in GitHub Desktop.
Appends branch name to git commit message.
#!/bin/bash
# prepends branch name and description to commit message
# inspired by the following articles:
# http://stackoverflow.com/a/26796665/207894
# http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
#
# 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);
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 "1s/^/$BRANCH /" $1
fi
@oryband
Copy link
Author

oryband commented Oct 28, 2015

@doodyparizada i call your prepare-commit-msg and raise it double

@avivl fyi :)

@fuzzyami @bolshoy @ofir-petrushka @gadisr @liorama add this to your repo/.git/hooks to prepend the branch name to every commit message

@liorama
Copy link

liorama commented Nov 3, 2015

cool

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