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

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

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

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根目录名(包含.git目录的根目录名),你可以这样运行:

basename `git rev-parse --show-toplevel`

git rev-parse——show-toplevel部分提供了该目录的路径,basename删除了路径的第一部分。

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

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

例子