有没有办法在一个命令中获取 git 根目录? [英] Is there a way to get the git root directory in one command?

查看:39
本文介绍了有没有办法在一个命令中获取 git 根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Mercurial has a way of printing the root directory (that contains .hg) via

hg root

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

Is there something equivalent in git to get the directory that contains the .git directory?

推荐答案

是:

git rev-parse --show-toplevel

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

If you want to replicate the Mercurial command more directly, you can create an alias:

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

现在 git root 的功能和 hg root 一样.

and now git root will function just as hg root.

注意:在子模块中,这将显示子模块不是父存储库的根目录.如果你使用 Git >=2.13 或更高版本,有一种方法是 子模块可以显示超级项目的根目录. 如果你的 git 比那个更旧,参见其他答案.

Note: In a submodule this will display the root directory of the submodule and not the parent repository. If you are using Git >=2.13 or above, there is a way that submodules can show the superproject's root directory. If your git is older than that, see this other answer.

这篇关于有没有办法在一个命令中获取 git 根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆