Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
当前回答
$ git config alias.root '!pwd'
# then you have:
$ git root
其他回答
“git rev-parse——git-dir”怎么样?
F:\prog\git\test\copyMerge\dirWithConflicts>git rev-parse --git-dir
F:/prog/git/test/copyMerge/.git
——git-dir选项似乎可以工作。
它甚至可以在裸库中工作,而git rev-parse——show- topllevel会触发(在裸库中)“致命:此操作必须在工作树中运行”。
从git rev-parse手册页:
--git-dir
Show $GIT_DIR if defined else show the path to the .git directory.
您可以在这个git setup-sh脚本中看到它的作用。
如果你在子模块文件夹中,Git >=2.13,使用:
git rev-parse --show-superproject-working-tree
如果你正在使用git rev-parse——show-toplevel,请确保使用的是git 2.25+ (Q1 2020)。
在Shell框架中预先配置的Shell别名
如果你使用shell框架,可能已经有一个可用的shell别名:
$ GRT in oh-my-zsh (68k) (cd $(git rev-parse——show-toplevel || echo ".")) $ git-root in prezto (8.8k)(显示工作树根的路径) $ g . .Zimfw (1k)(将当前目录更改为工作树的顶层。)
下面是我编写的处理这两种情况的脚本:1)带工作区的存储库,2)裸存储库。
https://gist.github.com/jdsumsion/6282953
Git-root(可执行文件在你的路径):
#!/bin/bash
GIT_DIR=`git rev-parse --git-dir` &&
(
if [ `basename $GIT_DIR` = ".git" ]; then
# handle normal git repos (with a .git dir)
cd $GIT_DIR/..
else
# handle bare git repos (the repo IS a xxx.git dir)
cd $GIT_DIR
fi
pwd
)
希望这对你们有帮助。
我想进一步阐述丹尼尔·布罗克曼的精彩评论。
定义git配置全局别名。exec”!Exec '允许你做像git Exec make这样的事情,因为man git-config说:
如果别名展开以感叹号作为前缀,则它将被视为shell命令。[…注意,shell命令将从存储库的顶级目录执行,而不一定是当前目录。
知道$GIT_PREFIX将是相对于存储库的顶级目录的当前目录的路径也很方便。但是,知道这只是战斗的一半。Shell变量展开使得它很难使用。所以我建议像这样使用bash -c:
git exec bash -c 'ls -l $GIT_PREFIX'
其他命令包括:
git exec pwd
git exec make
修改一下“git配置”的答案:
git config --global --add alias.root '!pwd -P'
把道路清理干净。很好。