32

I have a git repository with a pre-commit hook set up :

my-repo
|- .git
   |- hooks
      |- pre-commit     # I made this file executable

Until there, everything works. The hook is running when I commit.

=================================

I now run git config core.hooksPath ./git-config/hooks in my-repo.

The folder structure is this one :

my-repo
|- .git
   |- hooks
|- git-config
   |- hooks
      |- pre-commit     # I made this file executable as well

What happens is :

  • the new pre-commit script doesn't run on commit
  • the old pre-commit script still runs on commit if I leave it in my-repo/.git/hooks
  • running git config --get core.hooksPath in my-repo outputs ./git-config/hooks

How can I make the new pre-commit hook run on commit ?

Here's the link to the docs I apparently don't understand well :
https://git-scm.com/docs/git-config
https://git-scm.com/docs/githooks

Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
  • Trying to implement `core.hooksPath` myself. Will the default fit hooks be run in addition to you custom hooks or will only your custom hooks be run? – Xerri Mar 02 '18 at 09:17
  • @Xerri The `core.hooksPath` option overrides the default value (`./.git/hooks`) so only the hook scripts in the new folder you specified will be detected and run. – Nicolas Marshall Mar 02 '18 at 13:43

1 Answers1

44

The core.hooksPath support is new in Git version 2.9, having been put in with commit 867ad08a2610526edb5723804723d371136fc643. If your Git version is not at least 2.9.0, setting a hooks-path variable will have no effect at all.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 2
    Hmmm I can't believe I didn't check that before ! Anyways, that was it, I updated git and it works perfectly. Thanks ! – Nicolas Marshall Sep 15 '16 at 08:18
  • 8
    @onmyway133: hooks are kind of a pain, as there are about 40 gazillion ways to *prevent* them from running, and Git says absolutely nothing when any one of those ways does the preventing. So you must enumerate all the ways that hooks fail, checking each one in turn: (1) can Git find it? (2) is it executable (chmod +x)? (3) is it *really* executable (#! interpreter line if needed)? (4) is it *really really* executable? (ACLs, etc) ... – torek Jan 14 '17 at 00:09
  • Thanks @torek, your comment should be a response on its own as it saved my time since I forgot to make it executable – iomv Jun 14 '21 at 16:59
  • 2
    My hooksPath setting is stil ignored... having updated git to 2.33 ... I've created symlinks from .git/hooks to the actual path but I'd rather get it working ... – Guian Sep 15 '21 at 11:32
  • @Guian: so when symlinked into place, the hook files work, but when referred-to via `core.hooksPath`, they don't? That's a bit odd; look into any OS specific weirdness, such as limiting exec() across file systems for instance. (This will depend highly on your OS, ACLs, etc.) – torek Sep 16 '21 at 00:44