23

How do I find the full path to the .git/hooks directory of the current Git repository?


I see the following issues:

Deep in the directory structure

I may be anywhere deep in the git repository directory structure, e.g.

/home/user/project/.git/hooks

and I am in folder

/home/user/project/foo/bar/baz/

Submodules

Newer git versions put submodule meta data not into .git/ but in the main repository and .git is only a file, e.g.

$ cat /home/user/project/vendor/foo/.git
gitdir: ../../.git/modules/vendor/foo

So in this case, the hooks dir is in

/home/user/project/.git/modules/vendor/foo/hooks
cweiske
  • 30,033
  • 14
  • 133
  • 194
  • At least for the parent repo (not sure yet for the submodules), make sure to check `git config core.hooksPath` with git 2.9 (June 2016) or more. See [my answer below](http://stackoverflow.com/a/37293090/6309) – VonC May 18 '16 at 07:38

4 Answers4

22

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)

kelvin
  • 1,421
  • 13
  • 28
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
20

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

qqx
  • 18,947
  • 4
  • 64
  • 68
  • can this be used with `git submodule foreach`? for instance, something like `git submodule foreach cp .git/hooks/commit-msg $(git rev-parse --git-dir)/hooks`? the problem i'm seeing is that since shell expands the git rev-parse first, it evaluates to the git-dir of the superproject. – Woodrow Barlow Mar 09 '16 at 19:59
  • @WoodrowBarlow that would probably be better addressed as a new question rather than a comment, since comments here are quite limited in length and formatting capabilities. But have you tried placing single quotes around everything after `foreach`? That should delay evaluation as needed. – qqx Mar 10 '16 at 15:13
  • that worked (although i also had a different problem)! thank you. – Woodrow Barlow Mar 10 '16 at 16:56
  • 9
    This is no longer correct, use `git rev-parse --git-path hooks/hook-name` to find where a hook is stored. – r3m0t Jan 26 '17 at 09:43
5

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.

Till
  • 22,236
  • 4
  • 59
  • 89
4

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.

Alaa Moneam
  • 509
  • 5
  • 10