我如何找出 Git 认为顶级工作目录是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6748083/
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 do I find out what Git thinks the top-level working directory is?
提问by imz -- Ivan Zakharyaschev
Possible Duplicate:
Is there a way to get to the git root directory in one command?
可能的重复:
有没有办法在一个命令中进入 git 根目录?
Sometimes, I'm confused with git thinking that I'm inside a Git working dir, but it's not obvious to me what the top-level working directory (containing .git/
) is. (Probably, that repo was created by a mistake.)
有时,我对 git 感到困惑,认为我在 Git 工作目录中,但我不清楚顶级工作目录(包含.git/
)是什么。(可能,那个 repo 是由一个错误创建的。)
So, how do I find out the top-level Git repo directory if I'm somewhere inside the subdirectories? How do I ask git
to print what it thinks the current top-level working directory is?
那么,如果我在子目录中的某个地方,我如何找到顶级 Git 存储库目录?我如何要求git
打印它认为当前顶级工作目录是什么?
回答by Graham Borland
Try this:
尝试这个:
git rev-parse --show-toplevel
回答by imz -- Ivan Zakharyaschev
I've written a simple script (git-find-git-dirsin my "git-shortcuts" collection) to make such queries to Git handy:
我编写了一个简单的脚本(我的“ git-shortcuts”集合中的git-find-git-dirs)来方便地对 Git 进行此类查询:
#!/bin/bash
# find-git-dirs -- A simple script to "find" (i.e., "print") the GIT_DIR, as assumed by Git. (Useful if you are in a subdir, and you are not sure about the top-level repo dir.)
SUBDIRECTORY_OK=yes
. "$(git --exec-path)/git-sh-setup"
echo "GIT_DIR=$GIT_DIR"
and put it to ~/bin/
; now I can do my simple query like this:
并把它放在~/bin/
; 现在我可以像这样做我的简单查询:
$ git find-git-dirs
GIT_DIR=/home/imz/.git
GIT_WORK_TREE=
$
The only thing I lack from the initial question is the printing of the top-level workingdir, now it just prints the path to the internal git repo dir...
我在最初的问题中唯一缺少的是顶级工作目录的打印,现在它只打印内部 git repo 目录的路径......