Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
当前回答
从Git 2.13.0开始,它支持一个新的选项来显示根项目的路径,即使在子模块内部使用也可以工作:
git rev-parse --show-superproject-working-tree
其他回答
git-config的手册页(在Alias下)说:
如果别名展开以感叹号作为前缀,则它将被视为外壳 命令。[…注意shell 命令将从存储库的顶级目录执行,这可能不是 必须是当前目录。
在UNIX上,你可以这样做:
git config --global --add alias.root '!pwd'
alias git-root='cd \`git rev-parse --git-dir\`; cd ..'
其他所有东西都会在某一时刻失败要么是到主目录,要么就是悲惨地失败。这是返回GIT_DIR的最快和最短的方法。
git-extras
添加$ git根目录 看到https://github.com/tj/git-extras/blob/master/Commands.md git-root
$ pwd
.../very-deep-from-root-directory
$ cd `git root`
$ git add . && git commit
git-extra的可用性
Homebrew (osx)/linuxbrew(linux) $ brew安装git-extras Debian /ubuntu repos (https://packages.debian.org/sid/git-extras) $ apt-get安装git-extras
下面是我编写的处理这两种情况的脚本: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 config alias.root '!pwd'
# then you have:
$ git root