当您在某个Git目录下工作时,如何获得某个Git存储库中的Git存储库名称?有Git命令吗?

# I did check out bar repository and working in somewhere 
# under bar directory at this moment such as below.

$ git clone git://github.com/foo/bar.git
$ cd bar/baz/qux/quux/corge/grault # and I am working in here!
$ git xxx # <- ???
bar

当前回答

我认为这是明确识别存储库克隆的更好方法。

Git配置——获取remote.origin.url并检查以确保origin与ssh://your/repo匹配。

其他回答

你可以使用: Git remote -v

文档: https://git-scm.com/docs/git-remote

管理您跟踪其分支的存储库集(“远程”)。 - v 更详细一点,在名称后显示远程url。注意:这个必须放在remote和subcommand之间。

不需要联系存储库来获取名称,文件夹名称也不一定反映远程名称。

我发现这是获得当前存储库名称的最准确和有效的方法:

basename -s .git `git config --get remote.origin.url`

这应该在Git 1.8.1.5起作用。在此之前,现在已弃用的Git -repo-config命令可以工作(早在Git 1.7.5时)。

git ls-remote --get-url | xargs basename -s .git # bar

# zsh
git ls-remote --get-url | read
print $REPLY:t:r # bar

在git v2.7.0+中,为git-remote command引入了一个子命令get-url。

POSIX壳:

basename $(git remote get-url origin)

PowerShell:

Split-Path -Leaf (git remote get-url origin)

我认为这是明确识别存储库克隆的更好方法。

Git配置——获取remote.origin.url并检查以确保origin与ssh://your/repo匹配。