DEV Community

Sebastian
Sebastian

Posted on • Edited on

How to customise your git commit message

1. Add default template directory

git config --global init.templatedir '~/.git-templates'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

#!/bin/sh

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

BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"

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
fi
Enter fullscreen mode Exit fullscreen mode

See git-hooks

4. Make the hook executable

chmod +x ~/.git-templates/hooks/*
Enter fullscreen mode Exit fullscreen mode

Git hooks are not made executable by default.

5. Re-initialize git in each existing repository

git init
Enter fullscreen mode Exit fullscreen mode

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

See git-init

6. Commit

git commit -v
Enter fullscreen mode Exit fullscreen mode

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)

Why:
* 
Enter fullscreen mode Exit fullscreen mode

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

[commit]
    template = ~/.gitmessage
Enter fullscreen mode Exit fullscreen mode

Hints

👉 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)

RELATED:

Chris Beams about git commit messages

Top comments (7)

Collapse
 
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:

[core]
hooksPath = hooks/
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
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")

Collapse
 
ebud7 profile image
Sebastian

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!

Collapse
 
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!

Collapse
 
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...

Collapse
 
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.

Collapse
 
ebud7 profile image
Sebastian

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