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

hg root

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


当前回答

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

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

其他回答

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

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

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

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

alias cdroot='cd $(git root)'

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

Yes:

git rev-parse --show-toplevel

如果你想更直接地复制Mercurial命令,你可以创建一个别名:

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

现在git根和hg根一样。


注意:在子模块中,这将显示子模块的根目录,而不是父存储库。如果您使用的是Git >=2.13或更高版本,有一种方法可以让子模块显示超级项目的根目录。如果你的git比这更老,请参阅另一个答案。

“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)。

——show- topllevel是最近才添加到git rev-parse的吗?为什么没有人提到它?

从git rev-parse手册页:

   --show-toplevel
       Show the absolute path of the top-level directory.