41

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

David Wolever
  • 148,955
  • 89
  • 346
  • 502

6 Answers6

59

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
David Wolever
  • 148,955
  • 89
  • 346
  • 502
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 5
    Ahh yep, that's it! I had set it a while ago, and assumed it would run in addition to local hooks, not instead of. You seem to be correct - if `core.hooksPath` is present, then `.git/hooks/` is never checked (even if the `core.hooksPath` is missing a hook script). – David Wolever Apr 19 '18 at 04:29
  • 1
    @DavidWolever Well done. You do post challenging questions! – VonC Apr 19 '18 at 06:26
  • Let's say there's no `core.hooksPath` at all. But, you've found that running `git config --list --show-origin` shows you your global config location and now you just want every single project to always run the `pre-commit` hook to do the same thing (in my case to run tests if I forgot to). Where should I set my global hooks path AND how to tell the script that runs `npm test -- --updateSnapshots` to first cd to the specific project's root dir? – Neil Gaetano Lindberg Feb 11 '22 at 20:13
  • 1
    @NeilGuyLindberg Where: any folder you want, in order to host your hooks, including the pre-commit one. But since it takes no args, you cannot find the root folder of a specific project. Installing it locally, like this one does, is safer: https://pre-commit.com/ – VonC Feb 12 '22 at 00:42
  • 1
    This solved my problem. I thought `hooksPath` was additive. – martins16321 May 24 '22 at 06:02
  • Oh my! Husky seem to do this, and suddenly break people's own private repo-hooks. That's quite the nasty surprise. – odinho - Velmont May 30 '22 at 17:59
  • I was surprised this was the case even with a `hooksPath` that does not exist! (Copy pasted my `.gitConfig` from another computer without thinking). No errors or anything, just... local git hooks didn't work. Amazing! – J.M. Janzen May 18 '23 at 18:47
17

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

Alexander
  • 576
  • 5
  • 17
9

Executing this command in project(repository) root should fix the probem

git config --local core.hooksPath .git/hooks
Mustapha GANGA
  • 505
  • 7
  • 12
0

I just want to add that if you're having any confusion about what your path is to your git hooks then open your git hooks file with your terminal and run pwd or cd from your terminal to get the absolute path and use that for your hooksPath =

0

Check that your version of Git is greater than 2.9.

Hiroki Moto
  • 1
  • 1
  • 1
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 01 '21 at 05:55
  • 1
    Can't edit, but the missing detail here is that Git versions below 2.9 don't support `core.hooksPath`. Therefore, it's possible someone might encounter this problem when attempting to run hooks from a non-standard location (as they might erroneously assume `core.hooksPath` works, because Git does not tell you if a configuration option is unused) – Michael Jan 19 '22 at 02:10
  • As of Jun 2023, git is only at version 2.41.0. (see https://github.com/git/git/tags) Are you using a different git or did you specify the wrong version #? – Sue Raisty Jun 16 '23 at 18:06
0

In the case of pre-commit in particular, the pre-commit hook was not running because in that particular repo I had done pip install pre-commit, but not pre-commit install to install it into .git/hooks/pre-commit.

Noumenon
  • 5,099
  • 4
  • 53
  • 73