How to customise your git commit message

ebud7 profile image Sebastian Updated on ・2 min read

1. Add default template directory

git config --global init.templatedir '~/.git-templates'

Files and directories in the default template directory (~/.git-templates) whose name do not start with a dot will be copied to the specific $GitDir after it is created.

See git-init

2. Add default hook directory

mkdir -p ~/.git-templates/hooks

The hooks are stored in the hooks subdirectory of the default template directory.

See git-hooks

3. Add your first hook (e.g. prepare-commit-msg)

vi ~/.git-templates/hooks/prepare-commit-msg

Add a custom hook that puts your branch name to the top of your commit message. (Excluding master, develop)


if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop test)

BRANCH_NAME=$(git symbolic-ref --short HEAD)

BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then 
  sed -i.bak -e "1s/^/$BRANCH_NAME /" $1

See git-hooks

4. Make the hook executable

chmod +x ~/.git-templates/hooks/*

Git hooks are not made executable by default.

5. Re-initialize git in each existing repository

git init

To use your new hook you have to re-initialize git.

See git-init

6. Commit

git commit -v

How does it look like?

alt text

7. Automation
If you'd like to add the same commit message for every git project you could configure something like this:

First create your template file. (For example ~/.gitmessage)


After that you can simply add this file to your ~/.gitconfig

    template = ~/.gitmessage


👉 If a hook is already defined in your local git repository the new hook won't overwrite it.
👉 You might have to set the permissions on your new hook. (sudo chmod 775 .git/hooks/prepare-commit-msg)


Chris Beams about git commit messages


Editor guide
dmerejkowsky profile image
Dimitri Merejkowsky

Nice :)

By the way, since a few git versions, you can also use the hooksPath config variable.

So for instance you could have prepare-commit-msg under version control, in a hooks directory and then tell people to update the .git/config file to look like:

hooksPath = hooks/

An other possibility is for you to write the same kind of config code in
~/.config/git/config and have your prepare-commit-msgscript in just one place ;)

Hope this helps.

edwin_r_c profile image
Edwin Ramirez

Getting this error after following every step by the letter.

$ git commit
.git/hooks/prepare-commit-msg: 2: .git/hooks/prepare-commit-msg: Syntax error: "(" unexpected (expecting "fi")

ebud7 profile image
Sebastian Author

Hi Edwin

please make sure that #!/bin/sh is on top of your script file.

I added this here as well, thanks for reaching out!

edwin_r_c profile image
Edwin Ramirez

Hi, sorry for not giving back any feedback. For some reason this didn't work on Linux Mint. Buuuuut it worked perfectly on Mac OS Sierra. I think, maybe, I missed that last hint:
"You might have to set the permissions on your new hook. (sudo chmod 775 .git/hooks/prepare-commit-msg)".
Thanks a lot!

Thread Thread
kirb profile image
Adam Demasi

This script uses bash-isms, which aren't supported by sh. (sh is a symlink to bash on macOS.) The hashbang line should be /bin/bash.

bgannin01 profile image
Brian Ganninger

I'm a fan of using commit templates as well to take our commit messages to the next level. robots.thoughtbot.com/better-commi...

pzelnip profile image
Adam Parkin

Hmm, I followed the steps, and on git commit I don't see the branch name in vi. I did do a git init in my repo.

ebud7 profile image
Sebastian Author

Sounds like a permission problem. Please have a look at the last hint, that is solving your problem. ✌🏻