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

当前回答

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

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.origin.url并检查以确保origin与ssh://your/repo匹配。

回购全称:

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

你可以使用: Git remote -v

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

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

当目录名称与远程存储库名称不对应时,其他答案仍然不起作用(它可以)。 你可以通过这样的方式获取存储库的真实名称:

git远程显示起源- n | grep获取URL: | sed - e” #^.*/(.*)$#\ 1 #“| sed”# . # #美元”

基本上,你调用git remote show origin,从“Fetch URL:”字段中获取存储库URL,并正则化它以获得带有名称的部分: https://github.com/dragn/neat-vimrc.git

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

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