Check If Local Branch Exists On Remote Git


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?


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?


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.

Answered By – jthill

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published