Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find path to git hooks directory on the shell

Tags:

git

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
like image 913
cweiske Avatar asked Dec 28 '12 17:12

cweiske


People also ask

Where can I find Git hooks folder?

The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that's . git/hooks .

Are Git hooks part of repository?

Git hooks are shell scripts found in the hidden . git/hooks directory of a Git repository. These scripts trigger actions in response to specific events, so they can help you automate your development lifecycle. Although you may never have noticed them, every Git repository includes 12 sample scripts.


1 Answers

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

like image 116
qqx Avatar answered Oct 17 '22 17:10

qqx