当您在某个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匹配。

其他回答

现在问这个问题有点晚了,但如果你:

cat /path/to/repo/.git/config

你将看到存储库的url,其中包括reponame:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/your_git_user_name/your_git_repo_name.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

这种使用git-remote的方法对我来说很有效,适用于HTTPS遥控器:

$ git remote -v | grep "(fetch)" | sed 's/.*\/\([^ ]*\)\/.*/\1/'
                                                |  |        | |
                                                |  |        | +---------------+
                                                |  |        | Extract capture |
                                                |  +--------------------+-----+
                                                |Repository name capture|
                                                +-----------------------+

例子

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

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

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

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

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

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

它在git-2.18.2中运行得非常好,并且可以从git目标库外部启动:

Basename -s .git $(git——git-dir=/<项目路径>/.)Git远程获取url来源)