你如何在一些Git存储库中获得Git存储库的名称?

gip*_*any 119 git repository

当您在某个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
Run Code Online (Sandbox Code Playgroud)

Fua*_*aud 134

好吧,如果,对于存储库名称,您指的是Git根目录名称(包含.git目录的名称),您可以运行此命令:

basename `git rev-parse --show-toplevel`
Run Code Online (Sandbox Code Playgroud)

git rev-parse --show-toplevel部分为您提供该目录的路径,并basename删除路径的第一部分.

  • 正如@mvp在下面所说,你可以改变包含`.git`子目录的目录的名称,所以"如果,对于存储库名称,你的意思是git根目录名称"是不正确的. (6认同)
  • 此命令准确地为您提供:.git目录正上方的目录名称.这是对"存储库的名称"的一种解释,它似乎是OP正在寻找的.显然还有其他有效的解释,比如远程源存储库的名称.在这种情况下没有普遍的事实,这实际上取决于你想要什么. (6认同)
  • 这不安全。尝试 cd /var/www/html `git --git-dir=&lt;some git repo&gt;/.git rev-parse --show-toplevel` ,它会返回 /var/www/html (3认同)

mvp*_*mvp 112

一般来说,你不能这样做.Git并不关心如何命名你的git存储库.例如,您可以重命名包含您的存储库的目录(一个包含.git子目录),git甚至不会注意到它 - 一切都将继续工作.

但是,如果您克隆它,则可以使用命令:

git remote show origin
Run Code Online (Sandbox Code Playgroud)

显示有关您从中克隆存储库的原始远程的大量信息,它将包含原始克隆URL.

但是,如果您删除了使用原始远程的链接git remote rm origin,或者如果您使用创建该存储库git init,则无法获取此类信息 - 它在任何地方都不存在.

  • 我认为这应该是公认的答案. (16认同)
  • 你总是可以很好地使用分支名称,`git branch` :)(活动分支将标有星号 `*`)。 (2认同)

Tas*_*eat 68

无需联系存储库即可获取名称,文件夹名称不一定反映远程名称.

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

basename -s .git `git config --get remote.origin.url`
Run Code Online (Sandbox Code Playgroud)

这应该与Git 1.8.1.5一样.在此之前,现在已弃用的git-repo-config命令将起作用(早在Git 1.7.5).

  • 同意,这是最干净的答案(我以前有$ {PWD ## * /},但正如您所说,当前文件夹名称可能与来源不同) (2认同)

vul*_*ven 21

git v2.7.0 +中,get-url引入了一个子命令来git-remote命令.

POSIX shell:

basename $(git remote get-url origin)
Run Code Online (Sandbox Code Playgroud)

电源外壳:

Split-Path -Leaf (git remote get-url origin)
Run Code Online (Sandbox Code Playgroud)

  • 这是非常简洁和干净的!您可以使用“basename -s .git $(git remote get-url origin)”删除末尾的“.git”。 (5认同)
  • 我使用这种方法在字符串末尾得到了`.git`,所以我添加了一个分割来将其剥离:`(git remote get-url origin | Split-Path -leaf).split('.')[ 0]` (2认同)

dra*_*agn 18

当您的目录名称与远程存储库名称不对应时,其他答案仍然无效(并且可能).您可以使用以下内容获取存储库的真实名称:

git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"

基本上,你叫git remote show origin,从"获取URL:"拿仓库URL字段,正则表达式,它获得与名称的部分: https://github.com/dragn/ 整齐-的vimrc的.git

  • 这个更简单的`sed`在我的Mac OSX上运行得更好:`git remote show origin -n | grep h.URL | sed's /.*://; s/.git $ //'` (5认同)

ani*_*gif 17

如果您尝试在 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
Run Code Online (Sandbox Code Playgroud)

这是所需的结果,Vydia组织名称gourami也是包名称。结合它们可以帮助形成完整的User/Repo路径

  • `git config --get remote.origin.url | git config --get remote.origin.url | sed -r 's/.*(\@|\/\/)(.*)(\:|\/)([^:\/]*)\/([^\/\.]*)\ .git/https:\/\/\2\/\4\/\5/'` 将同时转换 `git@dom.tld:User/Repo.git` 和 `https://dom.tld/User/ Repo.git` 到 `https://dom.tld/User/Repo`。感谢你的“sed”想法 (3认同)

jon*_*les 7

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

cat /path/to/repo/.git/config
Run Code Online (Sandbox Code Playgroud)

您将看到存储库的 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
Run Code Online (Sandbox Code Playgroud)


OLI*_*KOO 5

我认为这是明确标识存储库克隆的更好方法。

git config --get remote.origin.url并检查确保原点匹配ssh://your/repo