我目前正在寻找一种解决方法来检索远程存储库 HEAD 指向的分支名称。我不想克隆存储库。
我直接需要一些 git 命令来检索 HEAD 分支名称。我试过了 下面的命令,但它只提供 SHA 提交哈希 HEAD,但我想要分支名称。
git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
最佳答案
使用 git ls-remote --symref
您无需克隆远程仓库即可(以编程方式)识别该远程仓库中的当前分支。 ls-remote
有一个名为 --symref
的方便选项:
In addition to the object pointed by it, show the underlying ref pointed by it when showing a symbolic ref. Currently, upload-pack only shows the symref HEAD, so it will be the only one shown by ls-remote.
有了这些知识,你可以简单地运行
git ls-remote --symref <remote_repo_url> HEAD
并使用一些 sed
-foo 提取当前分支的名称;因为 ls-remote
被归类为“管道”Git 命令,您可以安全地在脚本中使用它。
例子
$ git ls-remote --symref https://github.com/Jubobs/gitdags HEAD | \
head -1 | \
awk '{print $2}'
refs/heads/master
关于git - 在不克隆远程的情况下获取远程存储库中 HEAD 指向的分支名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41924904/