当您在某个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的方法对我来说很有效,适用于HTTPS遥控器:

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

例子

其他回答

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

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

例子

另外,我刚刚发现在.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是存储库的名称

下面是一个bash函数,它将打印存储库名称(如果它已经正确设置):

__get_reponame ()
{
    local gitdir=$(git rev-parse --git-dir)

    if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
        cat ${gitdir}/description
    else
        echo "Unnamed repository!"
    fi
}

解释:

local gitdir=$(git rev-parse --git-dir)

这将执行git rev-parse——git-dir,打印当前存储库的.git目录的完整路径。它将路径存储在$gitdir中。

if [ $(cat ${gitdir}/description) != "..." ]; then

这将执行cat ${gitdir}/description,打印当前存储库的.git/description的内容。如果您正确地命名了存储库,它将打印一个名称。否则,它将打印未命名的存储库;编辑这个文件'description'来命名存储库。

cat ${gitdir}/description

如果正确命名了repo,则打印内容。

else

否则……

echo "Unnamed repository!"

告诉用户回购是未命名的。


这个脚本中实现了类似的功能。

回购全称:

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

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

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

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

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