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目录下,则不要放大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配置”的答案:

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

把道路清理干净。很好。

以防万一,如果您要将此路径提供给Git本身,请使用:/

# this adds the whole working tree from any directory in the repo
git add :/

# and is equal to
git add $(git rev-parse --show-toplevel)

从Git 2.13.0开始,它支持一个新的选项来显示根项目的路径,即使在子模块内部使用也可以工作:

git rev-parse --show-superproject-working-tree