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 :(
Solution 1:
$ git ls-remote --heads [email protected]:user/repo.git branch-name
In case branch-name
is found you will get the following output:
b523c9000c4df1afbd8371324083fef218669108 refs/heads/branch-name
Otherwise no output will be sent.
So piping it to wc
will give you 1
or 0
:
$ git ls-remote --heads [email protected]:user/repo.git branch-name | 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. The result can be checked directly in a shell test or by checking the status variable $?
.
$ git ls-remote --exit-code --heads [email protected]:user/repo.git branch-name
Solution 2:
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
Solution 3:
You can also use this:
git show-branch remotes/origin/<<remote-branch-name>>
returns latest commit and value of $? is 0 otherwise returns "fatal: bad sha1 reference remotes/origin/<>" and value of $? is 128
Solution 4:
All of the answers here are Linux shell-specific, which doesn't help very much if you're in an environment that doesn't support those sort of operations - for example, Windows' command prompt.
Fortunately git ls-remote
accepts an --exit-code
argument that returns 0 or 2 depending on whether the branch exists or not, respectively. So:
git ls-remote --exit-code --heads origin <branch-that-exists-in-origin>
will return 0, and
git ls-remote --exit-code --heads origin <branch-that-only-exists-locally>
will return 2.
For PowerShell, you can simply use the built-in truthiness handling semantics:
if (git ls-remote --heads origin <branch-that-exists-in-origin>) { $true } else
{ $false }
yields $true
, while:
if (git ls-remote --heads origin <branch-that-only-exists-locally>) { $true } else
{ $false }
yields $false
.
Solution 5:
Another way you can use in the current folder if it is a git repo to run
git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'