如果特定分支存在于给定的远程存储库中,我需要对其进行子树 merge 。问题是远程仓库没有在本地 checkout ,所以无法使用git branch -r
。我只有一个远程地址,类似于 https://github.com/project-name/project-name.git
。有没有办法仅通过远程地址列出远程分支?我找不到任何有用的东西:(
最佳答案
$ git ls-remote --heads git@github.com:user/repo.git branch-name
如果找到 branch-name
,您将得到以下输出:
b523c9000c4df1afbd8371324083fef218669108 refs/heads/branch-name
否则不会发送任何输出。
所以通过管道传输到 wc
会给你 1
或 0
:
$ git ls-remote --heads git@github.com:user/repo.git branch-name | wc -l
或者,您可以在 git ls-remote
上设置 --exit-code
标志,如果没有匹配的 refs,它将返回退出代码 2
被发现。可以直接在 shell 测试中或通过检查状态变量 $?
来检查结果。
$ git ls-remote --exit-code --heads git@github.com:user/repo.git branch-name
关于git - 如何检查给定的远程存储库上是否存在远程分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223906/