当您在某个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目录中有一些回购信息。所以你可以在终端中查看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远程显示起源- n | grep获取URL: | sed - e” #^.*/(.*)$#\ 1 #“| sed”# . # #美元”

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

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

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 ls-remote --get-url | xargs basename -s .git # bar

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

如果你试图获得用户名或组织名称和github上的项目或回购名称,我能够编写这个命令,至少在本地为我工作。

▶ git config --get remote.origin.url
# => https://github.com/Vydia/gourami.git

▶ git config --get remote.origin.url | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/' # Capture last 2 path segments before the dot in .git
# => Vydia/gourami

这是理想的结果,因为Vydia是组织名称,而gourami是包名称。它们组合在一起可以帮助形成完整的用户/回购路径

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