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

后端 未结 14 1023
独厮守ぢ
独厮守ぢ 2020-12-02 11:04

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

14条回答
  •  时光说笑
    2020-12-02 11:35

    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

    will return 0, and

    git ls-remote --exit-code --heads origin

    will return 2.

    For PowerShell, you can simply use the built-in truthiness handling semantics:

    if (git ls-remote --heads origin ) { $true } else { $false }

    yields $true, while:

    if (git ls-remote --heads origin ) { $true } else { $false }

    yields $false.

提交回复
热议问题