这个问题已经在这里有一个答案:
有没有办法在一个命令中获取git根目录? 18答案我正在寻找像git list-path这样的东西来打印关联存储库的路径( .git目录)。
有一点背景:我已经在很多项目/文件夹上设置了git版本控制。 其中一些是嵌套的,所以一个具有回声自己的存储库的目录是与另一个存储库跟踪的另一个目录的子文件夹。
例如在我的主目录( ~ )我使用git跟踪我的点文件,但在文件夹~/photo/meta-mix/我有一个项目跟踪与一个单独的存储库。 现在,我已经在目录~/photo/meta-match/设置了另一个项目,但是我不知道它是否有自己的存储库。 所以我想知道这个目录是否受版本控制以及它的存储库的位置。
用git命令怎么做? git status可以给我
nothing to commit在这两种情况下, ~/photo/meta-match/有自己的存储库,或者只是指〜的存储库。
This question already has an answer here:
Is there a way to get the git root directory in one command? 22 answersI'm looking for something like git list-path printing the path of the associated repository (the .git directory).
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.
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.
How can I do this with a git command? git status can give me
nothing to commitin both cases, when ~/photo/meta-match/ has its own repository or when it just refers to the repository of ~.
最满意答案
git rev-parse --show-toplevel如果在git repo内执行,就可以了。 从git rev-parse手册页:
--show-toplevel显示顶级目录的绝对路径。
对于旧版本(1.7.x之前),其他选项列在“ 有一种方法可以在一个命令中获取git根目录 ”:
git rev-parse --git-dir这将给出.git目录的路径。
OP提到:
git rev-parse --show-prefix它返回git repo根下的本地路径。 (如果您在git repo根目录,则为空)
注意:为了简单地检查一个是否在git repo中,我发现以下命令非常表达:
git rev-parse --is-inside-work-tree是的,如果你需要检查你是否在.git git-dir文件夹中:
git rev-parse --is-inside-git-dir git rev-parse --show-toplevelcould be enough if executed within a git repo. From git rev-parse man page:
--show-toplevelShow 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?":
git rev-parse --git-dirThat would give the path of the .git directory.
The OP mentions:
git rev-parse --show-prefixwhich returns the local path under the git repo root. (empty if you are at the git repo root)
Note: for simply checking if one is in a git repo, I find the following command quite expressive:
git rev-parse --is-inside-work-treeAnd yes, if you need to check if you are in a .git git-dir folder:
git rev-parse --is-inside-git-dir更多推荐
发布评论