当我可能在子目录中时如何找到本地 git 存储库的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12293944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the path of the local git repository when I am possibly in a subdirectory
提问by halloleo
I'm looking for something like git list-path
printing the
path of the associated repository (the .git
directory).
我正在寻找诸如git list-path
打印关联存储库(.git
目录)的路径之类的东西。
A bit of background: I have set up git version control on quite a few of my projects/folders. Some of them are nested, so one directory with echo own repository is a subfolder to another directory tracked with another repository.
一点背景知识:我已经在很多项目/文件夹上设置了 git 版本控制。其中一些是嵌套的,因此带有 echo 自己存储库的一个目录是另一个存储库跟踪的另一个目录的子文件夹。
E.g. in my home directory (~
) I use git to track my dot files, but
in folder ~/photo/meta-mix/
I have another project I track with a
separate repository. Now, say,I have set up another project in directory
~/photo/meta-match/
, but I don't know anymore whether it has its own
repository. So I want to find out whether this directory is version
controlled and where its repository is.
例如,在我的主目录 ( ~
) 中,我使用 git 来跟踪我的点文件,但在文件夹中~/photo/meta-mix/
我有另一个项目,我使用单独的存储库进行跟踪。现在,比如说,我在 directory 中设置了另一个项目
~/photo/meta-match/
,但我不知道它是否有自己的存储库。所以我想知道这个目录是否受版本控制以及它的存储库在哪里。
How can I do this with a git command? git status
can give me
如何使用 git 命令执行此操作?git status
可以给我
nothing to commit
in bothcases, when ~/photo/meta-match/
has its own repository or when it just refers to the repository of ~
.
在这两种情况下,when~/photo/meta-match/
都有自己的存储库,或者仅引用~
.
回答by VonC
git rev-parse --show-toplevel
could be enough if executed within a git repo.
From git rev-parse
man page:
如果在 git repo 中执行就足够了。
从git rev-parse
手册页:
--show-toplevel
Show the absolute path of the top-level directory.
显示顶级目录的绝对路径。
For older versions (before 1.7.x), the other options are listed in "Is there a way to get the git root directory in one command?":
对于旧版本(1.7.x 之前),其他选项列在“有没有办法在一个命令中获取 git 根目录?”:
git rev-parse --git-dir
That would give the path of the .git
directory.
这将给出.git
目录的路径。
The OP mentions:
OP提到:
git rev-parse --show-prefix
which returns the local path under the git repo root. (empty if you are at the git repo root)
它返回 git repo root 下的本地路径。(如果您在 git repo 根目录下,则为空)
Note: for simply checking if one is in a git repo, I find the following command quite expressive:
注意:为了简单地检查一个是否在 git repo 中,我发现以下命令非常具有表现力:
git rev-parse --is-inside-work-tree
And yes, if you need to check if you are in a .git
git-dir folder:
是的,如果您需要检查您是否在.git
git-dir 文件夹中:
git rev-parse --is-inside-git-dir