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
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
yields $true, while:
if (git ls-remote --heads origin
yields $false.