Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If Local Branch Exists On Remote Git

Tags:

git

branch

I would like to see if one of my newly created local branches exists on the remote with a one line command. Like making an alias or function that would allow me to type

git remoteExists <branchName>

Right now I manually list off every branch on the remote and check to see if my local branch is there. This is not as easy as I would like since our remote has hundreds of branches, many with similar names.

I'm switching branches often working on different things, maybe 6-7 at a time, and it's difficult to remember if I've completed and pushed a branch or if I still need to finish it up.

I've searched and found some ways to do things similar to this, but many seemed unnecessarily complicated, is there a simpler way to do this?

EDIT

To be clear on what I'm doing. I'm starting from a remote branch and making a local branch off that. I'm making changes to my local branch and pushing. I do not want to set an upstream branch, since I will not be using this branch again; the person handling the remote branch will look at my changes and integrate them into the remote version.

This works:

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

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

Upon seeing this error I would know that the branch doesn't exist on the remote. Is there a cleaner way to do this?

like image 928
Alex Avatar asked Feb 27 '15 15:02

Alex


5 Answers

git fetch origin
git branch -r --contains $mybranch

and if you're sure any pushes came from this particular repo you can omit the fetch.

like image 136
jthill Avatar answered Oct 12 '22 23:10

jthill


git branch -r | grep -w <branchName>

should do the job.

If remote branch exists, there is an output, else, there isn't.

like image 45
user16644204 Avatar answered Sep 22 '22 12:09

user16644204


Inside your local git folder, you can do

git checkout

If there is no corresponding remote branch, there is no output. Otherwise it will print out the relationship between the local and remote branch (ahead, behind, diverged, etc)

Note: this doesn't work for 1.8.3.1, but works for 2.16.2

I actually wrote a small tool to see the status of all my repos. You can find it on github. If the color of the branch is shown as white, it means there is no remote branch.

  • white: local has no remote
  • green: local is the same as remote
  • red: local has diverged from remote
  • purple: local is ahead of remote (good for push)
  • yellow: local is behind remote (good for merge)

enter image description here

like image 4
nos Avatar answered Oct 12 '22 22:10

nos


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

git diff @{u}

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

@{upstream}, e.g. master@{upstream}, @{u}

The suffix @{upstream} to a branchname (short form @{u}) refers to the branch that the branch specified by branchname is set to build on top of (configured with branch..remote and branch..merge). A missing branchname defaults to the current one.

This will output typical git diff output showing changes between your local branch and the upstream tracking branch. If you want to use this as part of a shell command (e.g., for setting your prompt or something), you can add --quiet:

git diff --quiet @{u}

This will return a non-zero exit code if there are differences. E.g.:

git diff --quiet @{u} || echo "You need to push your changes!"
like image 2
larsks Avatar answered Oct 12 '22 21:10

larsks


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

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

Then you can check in shell:

if [ $? -eq 2 ]

Or in ruby:

if $?.exitstatus == 2

Or similarly in any other language.

like image 1
medik Avatar answered Oct 12 '22 23:10

medik