Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 :(

like image 854
Keen Avatar asked Sep 26 '22 21:09

Keen


People also ask

How do I find my remote repository branch?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands. If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge .

How check if git exists?

To check whether or not you have git installed, simply open a terminal window and type "git --version". If you've already followed the video Installing Git for Windows on a Windows Machine you'll see a message like "git version 1.9. 5. msysgit.


1 Answers

$ 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
like image 169
user487772 Avatar answered Oct 16 '22 11:10

user487772