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

hg root

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


当前回答

alias git-root='cd \`git rev-parse --git-dir\`; cd ..'

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

其他回答

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

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

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

以防万一,如果您要将此路径提供给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)
alias git-root='cd \`git rev-parse --git-dir\`; cd ..'

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

今天必须自己解决这个问题。在c#中解决了它,因为我需要它的程序,但我猜它可以轻松重写。考虑到这是公共领域。

public static string GetGitRoot (string file_path) {

    file_path = System.IO.Path.GetDirectoryName (file_path);

    while (file_path != null) {

        if (Directory.Exists (System.IO.Path.Combine (file_path, ".git")))
            return file_path;

        file_path = Directory.GetParent (file_path).FullName;

    }

    return null;

}