且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-25 23:35:40

实际上,您还可以指定要搜索的分支的名称:

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.