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)
#!/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
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?
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:
*
After that you can simply add this file to your ~/.gitconfig
[commit]
template = ~/.gitmessage
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)
Top comments (7)
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 ahooks
directory and then tell people to update the.git/config
file to look like:An other possibility is for you to write the same kind of config code in
~/.config/git/config
and have yourprepare-commit-msg
script in just one place ;)Hope this helps.
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")
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!
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!
I'm a fan of using commit templates as well to take our commit messages to the next level. robots.thoughtbot.com/better-commi...
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.
Sounds like a permission problem. Please have a look at the last hint, that is solving your problem. ✌🏻