如何检查给定远程存储库上是否存在远程分支? [英] How to check if remote branch exists on a given remote repository?
问题描述
如果特定分支存在于给定的远程存储库上,我需要为该分支进行子树合并。问题是远程仓库没有在本地签出,所以我不能使用 git branch -r
。我所拥有的只是一个远程地址,类似于 https://github.com/project-name/project-name.git
。有没有办法只通过远程地址列出远程分支?我找不到任何有用的东西:(
I need to do a subtree merge for a specific branch, if it exists on a given remote repository. The problem is that the remote repository is not checked out locally, so I can't use git branch -r
. All I have is a remote address, something like this https://github.com/project-name/project-name.git
. Is there a way to list remote branches just by a remote address? I couldn't find anything usefull :(
推荐答案
实际上,您还可以指定要搜索的分支的名称:
In fact you can also specify the name of the branch you are searching for:
$ git ls-remote --heads git@github.com:user/repo.git branch
如果发现名为分支
的分支,您将得到以下输出:
In case branch with name branch
is found you will get the following output:
b523c9000c4df1afbd8371324083fef218669108 refs/heads/branch
否则不会发送任何输出。
Otherwise no output will be sent.
因此,将它输送到 wc
会给你 1
或 0
:
So piping it to wc
will give you 1
or 0
:
$ git ls-remote --heads git@github.com:user/repo.git branch | wc -l
或者你可以在 git ls-remote
上设置 - exit-code
>如果找不到匹配的参考文献,它将返回退出代码 2
。
Alternatively you can set --exit-code
flag on git ls-remote
which will return exit code 2
in case no matching refs are found.
这篇关于如何检查给定远程存储库上是否存在远程分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!