Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abbeyjackson/f0ca249434f99a915720 to your computer and use it in GitHub Desktop.
Save abbeyjackson/f0ca249434f99a915720 to your computer and use it in GitHub Desktop.
Add feature branch name to git commit messages. Either do the global option or to add this to a repo, you can place the file in .git/hooks/prepare-commit-msg and make that file executable http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/ https://coderwall.com/p/jp7d5q/create-a-global-git-…
git config --global init.templatedir '~/.git-templates'
mkdir -p ~/.git-templates/hooks
# Create ~/.git-templates/hooks/prepare-commit-msg with the above content
chmod +x ~/.git-templates/hooks/prepare-commit-msg
# In any existing git repos you want to have this hook
git init # this will copy the hooks into the git repo, but will not overwrite any existing hooks
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment