How to check if remote branch exists on a given remote repository?

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.

So piping it to wc will give you 1 or 0 :

$ git ls-remote --heads git@github.com:user/repo.git branch | wc -l

Alternatively you can set --exit-code flag on git ls-remote which will return exit code 2 in case no matching refs are found.


git ls-remote --heads https://github.com/rails/rails.git
5b3f7563ae1b4a7160fda7fe34240d40c5777dcd    refs/heads/1-2-stable
81d828a14c82b882e31612431a56f830bdc1076f    refs/heads/2-0-stable
b5d759fd2848146f7ee7a4c1b1a4be39e2f1a2bc    refs/heads/2-1-stable
c6cb5a5ab00ac9e857e5b2757d2bce6a5ad14b32    refs/heads/2-2-stable
e0774e47302a907319ed974ccf59b8b54d32bbde    refs/heads/2-3-stable
13ad87971cc16ebc5c286b484821e2cb0fc3e3b1    refs/heads/3-0-stable
3df6c73f9edb3a99f0d51d827ef13a439f31743a    refs/heads/3-1-stable
f4db3d72ea564c77d5a689b850751ce510500585    refs/heads/compressor
c5a809e29e9213102351def7e791c3a8a67d7371    refs/heads/deps_refactor
821e15e5f2d9ef2aa43918a16cbd00f40c221e95    refs/heads/encoding
8f57bf207ff4f28fa8da4544ebc573007b65439d    refs/heads/master
c796d695909c8632b4074b7af69a1ef46c68289a    refs/heads/sass-cleanup
afd7140b66e7cb32e1be58d9e44489e6bcbde0dc    refs/heads/serializers

您可以在当前文件夹中使用的另一种方式,如果它是运行git repo

git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'
链接地址: http://www.djcxy.com/p/90390.html

上一篇: 在bitbucket中重命名合并的分支名称

下一篇: 如何检查给定远程存储库上是否存在远程分支?