Skip to content

Instantly share code, notes, and snippets.

@boschni
Last active January 11, 2017 10:29
Show Gist options
  • Save boschni/4664152 to your computer and use it in GitHub Desktop.
Save boschni/4664152 to your computer and use it in GitHub Desktop.
Git commit hook to automatically add branch name to every commit message. Put in ".git/hooks/commit-msg" and give the file execute rights.
#!/bin/sh
#
# Automatically adds branch name to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //')
if [ -n "$NAME" ]
then
if [ "$NAME" = "master" ]
then
echo $(cat "$1") > "$1"
else
echo "$NAME"': '$(cat "$1") > "$1"
fi
fi
@branneman
Copy link

And this is how you give the file execute rights:

cd .git/hooks
sudo chmod +x commit-msg

@branneman
Copy link

I've found that adding a --no-color params prevents these kind of commit messages:

[32mfeature/ISS-666[m: WIP on ISS-666

Line 5 then becomes:

git branch --no-color | grep '*' | sed 's/* //'

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