There are two hooks that maybe we will be interested to work in: prepare-commit-msg and commit-msg. In the Git directory, there is a “hooks” directory where you have to create the scripts that you want to execute in the hooks.
If you want to add automated the branch name in the commit message, you can create the “prepare-commit-msg” and add the next code:
BRANCH=`git branch | grep '^\*' | cut -b3-` echo "[$BRANCH]" > "$1.msg" cat "$1" >> "$1.msg" cat "$1.msg" > "$1"
With that script, the branch name will be added automated at the begining of the message, between braces . If you add the message in the command line with the option -m, you don’t have to add the branch name, the hook will do it for you. But be careful, because if you do a commit ammend, the hook is executed and then you have to edit the message because you will have twice the branch name.
Now that I’m learning more trick about Git, I think that it’s not recommended to put the branch name in the messages of your commits, if you are going to edit the timeline of your branches, editing or moving commits and doing some complicate actions, because now some commits belongs to the current branch, but maybe in the future that commits are moved to other branch and you will have to update all the commit messages.