How to run local Git project hooks in addition to core.hooksPath global hooks?

In order to execute the local hook from within the global one the following script snipped can be used:

if [ -e ./.git/hooks/commit-msg ]; then
    ./.git/hooks/commit-msg "$@"
fi

The global hook runs within the repo directory where the commit is made and can therefore check if a local hook exists in it's .git directory.

Note that you have to adopt the hook name if you are using something different than a commit message hook.


This will need to be done per hook, but I confirmed it works for prepare-commit-msg.

# run the global prepare-commit-msg if it is present
if [ -e `git config --global core.hookspath`/prepare-commit-msg ]; then
    `git config --global core.hookspath`/prepare-commit-msg "$@"
fi
# otherwise do nothing

I'm using that with Husky 7 in a file at .husky/prepare-commit-msg. Should work as a local hook as well.


I think the only way is for your global hooks to check if a corresponding local hook exists and run it.

This is not a complete solution because some hooks (pre-push, for example) accepts standard input in addition to command line parameters. If one of the hooks consumes the standard input the other doesn't have a chance.

Tags:

Git

Githooks