Search code examples
githookgithooks

Applying a git post-commit hook to all current and future repositories


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?


Solution

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


    But, as noted by Adam Lindberg in the comments:

    setting a global hooks path disables all local hooks in you repos!

    Depending on your use case, that can be the goal, if you want to, as the OP puts it, "create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)".

    But if you are unaware of that side effect, and still want to retain some local hooks... those would be ignored when core.hooksPath is set.