Mercurial有一种打印根目录(包含.hg)的方法

hg root

git中是否有等价的东西来获取包含.git目录的目录?


当前回答

下面是我编写的处理这两种情况的脚本: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配置”的答案:

git config --global --add alias.root '!pwd -P'

把道路清理干净。很好。

如果你正在寻找一个好的别名来执行此操作,如果你不在git目录下,则不要放大cd:

alias ..g='git rev-parse && cd "$(git rev-parse --show-cdup)"'

在这里写一个简单的答案,这样我们就可以用

git root

要完成这项工作,只需使用

git config --global alias.root "rev-parse --show-toplevel"

然后你可能想在~/.bashrc中添加以下内容:

alias cdroot='cd $(git root)'

这样你就可以用croot到回购的顶部。

git-config的手册页(在Alias下)说:

如果别名展开以感叹号作为前缀,则它将被视为外壳 命令。[…注意shell 命令将从存储库的顶级目录执行,这可能不是 必须是当前目录。

在UNIX上,你可以这样做:

git config --global --add alias.root '!pwd'
alias git-root='cd \`git rev-parse --git-dir\`; cd ..'

其他所有东西都会在某一时刻失败要么是到主目录,要么就是悲惨地失败。这是返回GIT_DIR的最快和最短的方法。