2025-08-27
| Back | Home |
Today I discovered a neat configuration option that makes using git
at work a whole lot more convenient. Git, as of 2.9+ (June 2016) has
an option to change default git hooks globally. Used in conjunction
with a commit hook for commit-msg, you can prepend a
text snippet (in my case, the branch name) to all commit messages. We
have a policy at work that all commits must be prepended with ticket
numbers. To help me remember what ticket I'm working on, I name all
my branches after the JIRA ticket I am working on. Following the
method I describe below combined with this existing workflow means I
never have to remember to include the ticket number in my commit
messages.
Something to be aware of when using the setup I describe below is that this will overide ALL git hooks for ALL repos you interact with on your system. If this does not sound like something that works for you, the same principles can be applied on a per-repo basis. You could probably also do something fancy with sourcing existing hook files in to your global hooks if they exist, but I won't cover that here. Drop me a note if that sounds like something you would be interested in and I can look in to it.
- Create a directory for your global git hooks
- Create a file named
commit-msgin that directory - Run
git config --global core.hooksPath /path/to/your/hooks - Mark the
commit-msgfile as executable - Add the following to the
commit-msgfile:
#!/bin/sh
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
if [ -n "$BRANCH_NAME" ]; then
echo "[$BRANCH_NAME] $(cat $1)" > $1
fi
And that's it. From now on, when you write a commit message the name of your branch will be prepended to your message.

