Find path to git hooks directory on the shell

if you are in a project directory you can know the current hooks path by typing to terminal

git config core.hooksPath

if the value is empty then you can set the path to your .githooks folder you can do that by typing this command

git config core.hooksPath .githooks/

you'll find now that hooks path changed and you can confirm by first command again.


You can use git rev-parse --git-dir to get the path to the repository used for your current directory. This will deal with gitlinks (using a file in place of the .git directory) as well as having use of the $GIT_DIR environment variable. The hooks directory will always be inside of that so you can use:

`git rev-parse --git-dir`/hooks

to get the path to the hooks directory.

This is documented in the git rev-parse manpage


In addition to --git-dir you can also use the following to find the root of a git repository since this is more generic, I am just adding it here for completion:

echo $(git rev-parse --show-toplevel)

Then append /.git/hooks to it? I haven't tried it with sub-modules though.


Starting git 2.9 (June 2016), you will need to look at the new config core.hooksPath to really be sure of the hooks folder.

git rev-parse --git-dir/hooks won't be enough anymore.

See commit 867ad08, commit de0824e, commit bf7d977, commit 49fa52f (04 May 2016) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 6675f50, 17 May 2016)

So make sure you check if git config core.hooksPath is defined or not, as it will override the default hooks folder location.

The git config documentation now includes:

core.hooksPath

By default Git will look for your hooks in the '$GIT_DIR/hooks' directory.
Set this to different path, e.g. '/etc/git/hooks', and Git will try to find your hooks in that directory, e.g. '/etc/git/hooks/pre-receive' instead of in '$GIT_DIR/hooks/pre-receive'.

The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run.


Git 2.10 (Q3 2016) uses that new core.hooksPath setting.

So to get the effective hooks path, you need to run:

git rev-parse --git-path hooks

From the git rev-parse man page:

--git-path <path>
    Resolve "$GIT_DIR/<path>" and takes other path relocation variables such
    as $GIT_OBJECT_DIRECTORY, $GIT_INDEX_FILE... into account. For example,
    if $GIT_OBJECT_DIRECTORY is set to /foo/bar then "git rev-parse
    --git-path objects/abc" returns /foo/bar/abc.

See commit 9445b49 (16 Aug 2016) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit d05d0e9, 19 Aug 2016)

Tags:

Git