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?
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.
git branch -r | grep -w <branchName>
should do the job.
If remote branch exists, there is an output, else, there isn't.
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.
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!"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With