Skip to content

Instantly share code, notes, and snippets.

@yogsototh
Last active June 23, 2016 18:40
Show Gist options
  • Save yogsototh/0f0fae6c1f9c6e2afbbb6e467d9e07a0 to your computer and use it in GitHub Desktop.
Save yogsototh/0f0fae6c1f9c6e2afbbb6e467d9e07a0 to your computer and use it in GitHub Desktop.
enforce commit message to start with issue number
#!/bin/bash
#
# This file should be in .git/hooks/commit-msg
# It uses the current branch name to prefix the commit message.
name=$(git rev-parse --abbrev-ref HEAD | sed 's/\(issue-[0-9]*\).*/\1/')
if [ $name != 'HEAD' ]; then
cat $1 | sed "s/^$name: //" | egrep -v "^#" > $1.tmp
echo -n "$name"': ' > $1
cat $1.tmp >> $1
rm $1.tmp
fi
@yogsototh
Copy link
Author

yogsototh commented Jun 23, 2016

Copy this file in your .git/hooks/ it will prepend the branch name to your commit messages.
If your branch is named issue-451-something-else it will only prepend issue-451.

It works nicely with amending commit. During rebasing it doesn't prepend anything as during rebasing it is not possible to determine which is the current branch.

@d12frosted
Copy link

Very nice, thanks!

@yogsototh
Copy link
Author

you're welcome! I've updated it again since there was a bug with multiline comments.

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