Check if local branch exists on remote git device

I would like to see if one of my new local branches exists on the remote computer with a single line. Like creating an alias or function that would let me type

git remoteExists <branchName> 

Right now, I manually write off each branch on the remote control and check if my local branch exists. This is not as easy as we would like, because our remote has hundreds of branches, many of which have similar names.

I switch branches, often working on different things, maybe 6-7 at a time, and it's hard to remember if I finished and clicked on a branch, or I still need to finish it.

I searched and found ways to do something similar to this, but many seemed unnecessarily complicated, is there an easier way to do this?

EDIT

To understand what I am doing. I start with the remote branch and disable the local branch. I make changes to my local branch and push. I do not want to set the branch upstream, since I will no longer use this branch; the person working with the remote branch will view my changes and integrate them into the remote version.

It works:

 git diff <branchName> remotes/origin/<branchName> 

fatal: ambiguous argument 'remotes / origin / TestReadyBranch': unknown version or path not in the working tree. Use '-' to separate paths from revisions, for example: 'git [...] - [...]'

Having seen this error, I would know that the branch does not exist on the remote control. Is there a cleaner way to do this?

+7
git branch
source share
4 answers
 git fetch origin git branch -r --contains $mybranch 

and if you are sure that any attempts got out of this particular repo, you can omit the selection.

+5
source share

To check if your local branch has changes in the upstream tracking branch, you can run:

 git diff @{u} 

Where @{u} refers to the name of the upstream branch. On the git-rev-parse(1) man page:

@ {upstream}, for example. master @ {upstream}, @ {u}

The suffix @ {upstream} in branchname (short form @ {u}) refers to the branch that the specified branchname branch is set to (configured using the branch. Branch..merge branch). Invalid branchname defaults to current.

This will display the typical git diff output, displaying the changes between your local branch and the upstream tracking branch. If you want to use this as part of a shell command (for example, to set a tooltip or something else), you can add --quiet :

 git diff --quiet @{u} 

This will return a nonzero exit code if there are differences. For example:.

 git diff --quiet @{u} || echo "You need to push your changes!" 
+2
source share

This will set the exit code to 2 if the current branch does not exist on the remote control:

 git ls-remote --heads --exit-code origin "$(git symbolic-ref --short HEAD)" 

Then you can check the shell:

 if [ $? -eq 2 ] 

Or in ruby:

 if $?.exitstatus == 2 

Or similarly in any other language.

0
source share

In local local git folder you can do

 git checkout 

If there is no corresponding remote branch, there is no output. Otherwise, it will print the connection between the local and remote branch (front, back, diverging, etc.)

Note: this does not work for 1.8.3.1, but works for 2.16.2

0
source share

All Articles