当您在某个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-2.18.2中运行得非常好,并且可以从git目标库外部启动:

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

其他回答

你可以使用: Git remote -v

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

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

另外,我刚刚发现在.git目录中有一些回购信息。所以你可以在终端中查看FETCH_HEAD文件来查看repo的名称:

例子:

cd your_project_folder/.git
more FETCH_HEAD

输出:

672e38391557a192ab23a632d160ef37449c56ac        https://bitbucket.org/fonjeekay/some_repo

https://bitbucket.org/somebituser/some_repo.git是存储库的名称

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

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

例子

回购全称:

git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"

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

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