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

hg root

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


当前回答

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

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

其他回答

如果有人需要POSIX兼容的方式来做到这一点,而不需要git可执行文件:

git-root:

#$1: Path to child directory
git_root_recurse_parent() {
    # Check if cwd is a git root directory
    if [ -d .git/objects -a -d .git/refs -a -f .git/HEAD ] ; then
        pwd
        return 0
    fi

    # Check if recursion should end (typically if cwd is /)
    if [ "${1}" = "$(pwd)" ] ; then
        return 1
    fi

    # Check parent directory in the same way
    local cwd=$(pwd)
    cd ..
    git_root_recurse_parent "${cwd}"
}

git_root_recurse_parent

如果你只是想把这个功能作为脚本的一部分,删除shebang,并把最后一行git_root_recurse_parent替换为:

git_root() {
    (git_root_recurse_parent)
}

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

从git rev-parse手册页:

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

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

以防万一,如果您要将此路径提供给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-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