Skip to content

Instantly share code, notes, and snippets.

@shytikov
Created July 17, 2012 21:07
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save shytikov/3132078 to your computer and use it in GitHub Desktop.
Save shytikov/3132078 to your computer and use it in GitHub Desktop.
commit-msg hook script
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //')
DESCRIPTION=$(git config branch."$NAME".description)
TEXT=$(cat "$1" | sed '/^#.*/d')
if [ -n "$TEXT" ]
then
echo "$NAME"': '$(cat "$1" | sed '/^#.*/d') > "$1"
if [ -n "$DESCRIPTION" ]
then
echo "" >> "$1"
echo $DESCRIPTION >> "$1"
fi
else
echo "Aborting commit due to empty commit message."
exit 1
fi
@blkwtkns
Copy link

This is great, thank you. It has cause a little problem when doing an interactive rebase and attempting to reword a commit.

@rbobillot
Copy link

rbobillot commented Jul 12, 2021

Also, if you just wanna get your branch name, without using sed, grep nor awk:
NAME=$(git symbolic-ref --short HEAD)
NAME=$(git branch --show-current)

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