在shell上查找git钩子目录的路径 [英] Find path to git hooks directory on the shell

查看:319
本文介绍了在shell上查找git钩子目录的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到当前Git仓库的 .git / hooks 目录的完整路径?






我看到以下问题:

目录结构深处



我可能位于git存储库目录结构的任何位置,比如

  /home/user/project/.git/钩子

我在文件夹中

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



子模块



新的git版本将子模块元数据放入 .git / 中,但是在主存储库中, .git 只是一个文件,例如

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

因此,在这种情况下,钩子目录位于

  /home/user/project/.git / modules / vendor / foo / hooks 


解决方案

您可以使用 git rev-parse --git-dir 来获取用于当前目录的存储库的路径。这将处理gitlinks(使用文件来代替 .git 目录)以及使用 $ GIT_DIR 环境变量。 hooks目录总是在里面,所以你可以使用:

 `git rev-parse --git-dir` /钩子

获取钩子目录的路径。



这在 git中有记载rev-parse manpage

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

解决方案

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

这篇关于在shell上查找git钩子目录的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆