jp7d5q
Last Updated: October 05, 2016
·
16.01K
· venables
B7f5b6064b7cb49f6bbe59cea978b52d

Create a global git commit hook

git

1. Enable git templates:

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

This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init

2. Create a directory to hold the global hooks:

mkdir -p ~/.git-templates/hooks

3. Write your hooks in ~/.git-templates/hooks.

For example, here's a post-commit hook (located in ~/.git-templates/hooks/post-commit):

#!/bin/sh

# Copy last commit hash to clipboard on commit
git log -1 --format=format:%h | pbcopy

# Add other post-commit hooks 

4. Make sure the hook is executable.

chmod a+x ~/.git-templates/hooks/post-commit

5. Re-initialize git in each existing repo you'd like to use this in:

git init

NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

4 Responses
Add your response

8646
6208d7eb888b9c22d62bd1c4b4b34345

thanks for this, really saves some time to be able to predefine similar post-commit hooks for new projects

over 1 year ago ·
12063
6370a17ce82811e2975a22000a1fb425 7

thank you! it really helps a lot

over 1 year ago ·
16098
40272affb7fc4db3c6cf9f1f86f16877

Much appreciated... I have many projects, was looking for a way of ensuring the same hooks were present .. this sorted it!

over 1 year ago ·
22130
6b0aef8fd76cc6106d11c1c0f8494e6f normal