Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@azizmb

This comment has been minimized.

Show comment
Hide comment
@azizmb

azizmb Oct 22, 2012

Hi! Thanks for the script. One issue though. When I use this hook, I lose any line breaks in the commit messages. Any idea how to fix this?

azizmb commented Oct 22, 2012

Hi! Thanks for the script. One issue though. When I use this hook, I lose any line breaks in the commit messages. Any idea how to fix this?

@pzelnip

This comment has been minimized.

Show comment
Hide comment
@pzelnip

pzelnip Jul 16, 2013

On a "git commit -a" I always get "aborting commit due to empty commit message".

pzelnip commented Jul 16, 2013

On a "git commit -a" I always get "aborting commit due to empty commit message".

@chrishough

This comment has been minimized.

Show comment
Hide comment
@chrishough

chrishough Aug 13, 2013

Great post +11

chrishough commented Aug 13, 2013

Great post +11

@danwald

This comment has been minimized.

Show comment
Hide comment
@danwald

danwald Feb 20, 2015

you should put this in your prepare-commit-msg instead

danwald commented Feb 20, 2015

you should put this in your prepare-commit-msg instead

@san7hos

This comment has been minimized.

Show comment
Hide comment
@san7hos

san7hos Mar 16, 2016

@danwald you cannot start lines with # in the vim editor, that makes it harder to put in the prepare hook

san7hos commented Mar 16, 2016

@danwald you cannot start lines with # in the vim editor, that makes it harder to put in the prepare hook

@synthesizerpatel

This comment has been minimized.

Show comment
Hide comment
@synthesizerpatel

synthesizerpatel May 3, 2017

A tiny amount of code golf for getting the branch name if you care:

NAME=$(git branch | sed -n -e 's/^* //gp')

synthesizerpatel commented May 3, 2017

A tiny amount of code golf for getting the branch name if you care:

NAME=$(git branch | sed -n -e 's/^* //gp')

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