Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run both local and global git hooks

Tags:

git

githooks

I have a global git hook post-commit which is situated under:

~/.git_templates/hooks/post-commit

I have made it global by

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

and using git init to update the settings for my git project.

Yet one project had its own post-commit hook under:

~/src/git.repo/.git/hooks/post-commit

The local one ran but prevented the global one from running. How can I achieve that both are run on post commit?

I want to avoid adding the command in the local post commit hook.

like image 627
k0pernikus Avatar asked Mar 17 '26 09:03

k0pernikus


1 Answers

You can set a global hook path.

git config --global core.hooksPath "$CONFIG_DIR/hooks"

This will ignore local hook and run global hook scripts only.

And if you want to run both local & global hook, you can call local hook from global hook manually.

For example: call this script from your global pre-commit hook.

...
/path/to/run-local-hook pre-commit

run-local-hook file:

# Run local hook if exists
HOOK=$1
PROJECT_ROOT=$(git rev-parse --show-toplevel)

if [ -e "$PROJECT_ROOT/.git/hooks/$HOOK" ]; then
  $PROJECT_ROOT/.git/hooks/$HOOK "$@"
else
  exit 0
fi
like image 91
user4097210 Avatar answered Mar 20 '26 18:03

user4097210