314

I've written a Git post-commit hook and it works correctly. However, I want to add this hook to apply to all current (and future) Git repositories I am working on. I tried adding the hook to my ~/.git/hooks/ directory instead of in the hooks directory in the project directory, however, this did not seem to work.

Is there a way to create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)? If not, what would be the best solution going forward -- perhaps a git-init template?

3

4 Answers 4

309

As of Git 1.7.1, you can set init.templatedir in your gitconfig to tell Git where to look for templates.

Set it like this:

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

Afterward, new repositories you create or clone will use this directory for templates. Place the hooks you want in ~/.git_template/hooks. Existing repositories can be reinitialized with the proper templates by running git init in the same directory .git is in.

For Git versions older than 1.7.1, running git init --template ~/.git_template will work if you're like me and still want to manage your .git_template directory along with the rest of your dot files. You can also use the $GIT_TEMPLATE_DIR environment to tell git init where your template directory is.

12
  • 64
    Great answer. If anybody else wonders if re-running git init on an existing repo wipes it - it does not, see: stackoverflow.com/questions/5149694/…
    – kontur
    Oct 10, 2013 at 13:16
  • 2
    Just an fyi, ~/.git_template is not automatically created for you with the command. You still have to do that yourself with mkdir ~/.git_template
    – Geuis
    May 22, 2015 at 1:04
  • 2
    On windows without quotation: ` git config --global init.templatedir d:\git\.git_template\ ` Jul 2, 2015 at 20:03
  • 1
    Tip: The template hooks are copied. If you want to be able to update the global hooks together, put the hook elsewhere and add a symbolic link in the template directory (on Linux). Make sure the link path is absolute. Jul 29, 2016 at 3:52
  • 1
    Running git init didn't actually update the hooks for me. It seems it will not overwrite files that are already there. stackoverflow.com/questions/10791486/. Instead you need to remove the old hook files first.
    – Phil R
    Feb 21, 2017 at 22:34
273

I want to add this hook to apply to all current (and future) git repositories I am working on

With git 2.9+ (June 2016), all you would do is:

git config --global core.hooksPath /path/to/my/centralized/hooks

See "change default git hooks": this has been done to manage centralized hooks.

5
  • if i have an exisiting repo and want all other dev's who pull changes to have an updated pre-commit hook for example how would i do this ? thank you
    – Richlewis
    Nov 11, 2016 at 15:45
  • 1
    @Richlewis do you mean stackoverflow.com/a/40550555/6309 was not completely clear?
    – VonC
    Nov 11, 2016 at 15:47
  • @Richlewis You would need to setup a shared folder accessible by all devs, for them to reference in their own local config.
    – VonC
    Nov 11, 2016 at 15:47
  • unfortunately i still don't quite get it. within the repo there is /.git/hooks/pre_commit can i point it to that ?
    – Richlewis
    Nov 11, 2016 at 15:49
  • @Richlewis I have answered you on your original question.
    – VonC
    Nov 11, 2016 at 15:59
73

If you want them everywhere on your system (including users besides you), you can modify the contents of the installed template directory - those are in $PREFIX/share/git-core/templates/hooks, where $PREFIX is probably /usr/local or /usr.

If you want this to just be for you, then yes, the simplest thing would be the --template option of git-init. You could easily keep a personal template directory which has symlinks back to the installed version of defaults you want to keep (individual hooks, the info directory...) and then your own content in hooks/post-commit and anything else you want to customize.

6
  • 11
    Thanks, this worked out well. And to retroactively apply it to my existing projects, I just ran git init again and it added my new hook.
    – swanson
    Feb 19, 2010 at 2:33
  • This is a neat workaround, but it would require that you change all your repos. This is feasable, but isn't there some possibility with a plugin or something (this is how it is done at Bazaar)? Jul 16, 2011 at 4:40
  • 2
    For SourceTree's embedded git on OS X, they're in /Applications/SourceTree.app/Contents/Resources/git_local/share/git-core/templates/hooks
    – CupawnTae
    Sep 24, 2014 at 8:31
  • 2
    For Windows this is found in the git install directory under mingw64\share\templates\hooks (or mingw32 for 32-bit)
    – nerdherd
    Sep 3, 2015 at 17:32
  • 1
    @nerdherd it's now at mingw64\share\git-core\templates\hooks (gfw 2.25)
    – RJFalconer
    Feb 27, 2020 at 17:28
7

A minimalist approach is to create a git_hooks/ directory in your repository to track the hooks that you write for that project, bring it to the attention of future users by mentioning it in a README, and rely on them to do the right thing after they have cloned. I have cogitated on this for a while and chose an incremental approach. Down the road I might consider using a tool like git-hooks.

1
  • 4
    This is so, so far away from being a real solution that I had to downvote, sorry. Sep 25, 2019 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.