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
You can do something like this in the Bash terminal. Just replace the echos with the commands you want to execute.
if git ls-remote https://username:password@github.com/project-name/project-name.git | grep -sw "remote_branch_name" 2>&1>/dev/null; then echo "IT EXISTS..START MERGE" ; else echo "NOT FOUND" ; fi
Hope it helps.
$ git ls-remote --heads origin <branch> | wc -l
works most of the time.
But will not work if branch matches partially as below,
$ git branch -a
creative/dev
qa/dev
$ git ls-remote --heads origin dev | wc -l
2
Use
git ls-remote --heads origin <branch> | \
cut -d$'\t' -f2 | \
sed 's,refs/heads/,,' | \
grep ^<branch>$ | wc -l
if you want a reliable way.
If you want to use in script and do not want to assume origin
as default remote then
git ls-remote --heads $(git remote | head -1) "$branch" | \
cut -d$'\t' -f2 | \
sed 's,refs/heads/,,' | \
grep ^"$branch"$ | wc -l
should work.
Note that git branch -a | grep ...
is not reliable as it may be a while since the last fetch
was run.
Another way you can use in the current folder if it is a git repo to run
git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'
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 <branch-that-exists-in-origin>
will return 0, and
git ls-remote --exit-code --heads origin <branch-that-only-exists-locally>
will return 2.
For PowerShell, you can simply use the built-in truthiness handling semantics:
if (git ls-remote --heads origin <branch-that-exists-in-origin>) { $true } else
{ $false }
yields $true
, while:
if (git ls-remote --heads origin <branch-that-only-exists-locally>) { $true } else
{ $false }
yields $false
.
$ 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. This is the most idiomatic solution. 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
You can add the repository you have as a remote using git remote add something https://github.com/project-name/project-name.git
and then do a git remote show something
to get all information about the remote. This requires a network connection and is useful for human use.
Alternatively, do a git fetch something
. This will fetch all branches on the remote called something
and keep them in your local repository. You can then merge them into your local branch as you please. I recommend this path since if you finally decide that you have to merge, this is what you need to do.
OT: Your use of "checked out locally" indicates that you're approaching this from a centralised version control system standpoint. That's usually a dead end when you're dealing with git. It uses words like "checkout" etc. differently from how older systems did.