【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
Mercurial有一种打印根目录(包含.hg)的方法
hg root
在git中是否有类似的东西来获取包含.git目录的目录?
#1楼
如果你正在寻找一个好的别名来做这个加上如果你不在git目录中而不是放弃cd :
alias ..g='git rev-parse && cd "$(git rev-parse --show-cdup)"'
#2楼
$ git config alias.root '!pwd'
# then you have:
$ git root
#3楼
这是我编写的一个脚本,它处理两种情况: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
)
希望这很有帮助。
#4楼
git-config的man页(在Alias下)说:
如果别名扩展以感叹号为前缀,则将其视为shell命令。 [...]请注意,shell命令将从存储库的顶级目录执行,该目录可能不一定是当前目录。
所以,在UNIX上你可以这样做:
git config --global --add alias.root '!pwd'
#5楼
无论您是在git子目录中还是在顶层,这个shell别名都有效:
alias gr='[ ! -z `git rev-parse --show-toplevel` ] && cd `git rev-parse --show-toplevel || pwd`'
更新为使用现代语法而不是反引号:
alias gr='[ ! -z $(git rev-parse --show-toplevel) ] && cd $(git rev-parse --show-toplevel || pwd)'
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3153144