当我可能在一个子目录[duplicate]中时,如何找到本地git仓库的路径(How to find the path of the local git repository when I am po

编程入门 行业动态 更新时间:2024-10-26 07:39:30
当我可能在一个子目录[duplicate]中时,如何找到本地git仓库的路径(How to find the path of the local git repository when I am possibly in a subdirectory [duplicate])

这个问题已经在这里有一个答案:

有没有办法在一个命令中获取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 answers

I'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 commit

in 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-toplevel

could be enough if executed within a git repo. From git rev-parse man page:

--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?":

git rev-parse --git-dir

That would give the path of the .git directory.


The OP mentions:

git rev-parse --show-prefix

which 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-tree

And yes, if you need to check if you are in a .git git-dir folder:

git rev-parse --is-inside-git-dir

更多推荐

本文发布于:2023-07-18 11:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   在一   个子   中时   路径

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!