33

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

2
  • 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
    Commented Mar 2, 2018 at 9: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. Commented Mar 2, 2018 at 13:43

2 Answers 2

47

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.

5
  • 2
    Hmmm I can't believe I didn't check that before ! Anyways, that was it, I updated git and it works perfectly. Thanks ! Commented Sep 15, 2016 at 8:18
  • 10
    @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
    Commented Jan 14, 2017 at 0: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
    Commented Jun 14, 2021 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
    Commented Sep 15, 2021 at 11:32
  • 1
    @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
    Commented Sep 16, 2021 at 0:44
4

In addition to git config, I also needed to add chmod +x to the pre-commit file.

git config --local core.hooksPath .githooks/

chmod +x .githooks/pre-commit

Not the answer you're looking for? Browse other questions tagged or ask your own question.