Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pre- and post- commit hooks not running

Tags:

git

What could cause my git pre- and post- commit hooks to not run?

(please note: this question is not a duplicate; the answer to each of the other questions is either chmod +x or "don't have a file extension", and neither are the issue here)

They are executable:

$ ls -alh .git/hooks/*-commit -rwxr-xr-x … .git/hooks/post-commit -rwxr-xr-x … .git/hooks/pre-commit

And this is the content of each of them:

#!/bin/sh echo "$0 IS RUNNING" exit 1

Running them manually works:

$ .git/hooks/pre-commit .git/hooks/pre-commit IS RUNNING

But they aren't run by git on commit:

$ git commit -am "Test hooks" [master d17c0f38] Test hooks 1 file changed, 1 insertion(+)

This is with git 2.16.2

like image 325
David Wolever Avatar asked Apr 19 '18 04:04

David Wolever


Video Answer


1 Answers

I have seen for instance the config core.hooksPath being set to another path than $GIT_DIR/hooks, making your hooks in that folder ignored.

Check your git config core.hooksPath output, and more generally git config -l for any out of the ordinary settings.

Note that a git commit -n would skip the pre-commit hook.

Edit by wolever:

I've added this to the scripts in my core.hooksPath directory, which will run the repo's hooks if they exist:

#!/bin/sh
set -eu
hook_name="$(basename "$0")"
hook_script=".git/hooks/$hook_name"
[ -e "$hook_script" ] && "$hook_script"
exit 0
like image 180
VonC Avatar answered Oct 08 '22 19:10

VonC