Question

git pre- and post- commit hooks not running

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

 46  52770  46
1 Jan 1970

Solution

 67

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
2018-04-19

Solution

 20

In my case core.hooksPath in projRootDir/.git/config was set incorrectly. Removing the entry from config file did the trick

2021-02-04