Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$ git ls-remote --heads git@github.com: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 git@github.com: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 git@github.com:user/repo.git branch-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...