[git] Git says remote ref does not exist when I delete remote branch

I ran git branch -a

* master
  remotes/origin/test
  remotes/origin/master

I want to delete my remote branch

I've tried

git push origin --delete remotes/origin/test

I got

error: unable to delete 'remotes/origin/test': remote ref does not exist

How is it not exist ?

I did a git branch -a, and I saw it listed.

Did I miss anything ?

This question is related to git

The answer is


This should help:

  1. git fetch
  2. git push origin --delete branchName

A handy one-liner to delete branches other than 'master' from origin:

git branch --remotes | grep -v 'origin/master' | sed "s/origin\///" | xargs -i{foo} git push origin --delete {foo}

Be sure you understand the implications of running this before doing so!


  1. get the list of remote branches
git fetch # synchronize with the server
git branch --remote # list remote branches
  1. you should get a list of the remote branches:
origin/HEAD -> origin/master
origin/develop
origin/master
origin/deleteme
  1. now, we can delete the branch:
git push origin --delete deleteme

Given that the remote branch is remotes/origin/test you can use two ways:

git push origin --delete test

and

git branch -D -r origin/test

For me this worked $ ? git branch -D -r origin/mybranch

Details

$ ? git branch -a | grep mybranch remotes/origin/mybranch

$ ? git branch -r | grep mybranch origin/mybranch

$ ? git branch develop * feature/pre-deployment

$ ? git push origin --delete mybranch error: unable to delete 'mybranch': remote ref does not exist error: failed to push some refs to '[email protected]:config/myrepo.git'

$ ? git branch -D -r origin/mybranch Deleted remote branch origin/mybranch (was 62c7421).

$ ? git branch -a | grep mybranch

$ ? git branch -r | grep mybranch


For windows

git branch --remotes| %{ $_.Trim().Split("/")[1] }| ?{ $_ -ne 'master' } | | ?{ $_ -ne 'otherBranch' } | %{ git push origin --delete $_ }

git push origin --delete yourBranch


git push origin --delete origin/test 

should work as well


git branch -a will list the branches in your local and not the branches in your remote.

And the error error: unable to delete 'remotes/origin/test': remote ref does not exist means you don't have a branch in that name in your remote but the branch exists in your local.


I followed the solution by poke with a minor adjustment in the end. My steps follow
- git fetch --prune;
- git branch -a printing the following
    master
    branch
    remotes/origin/HEAD -> origin/master
    remotes/origin/master
    remotes/origin/branch (remote branch to remove)
- git push origin --delete branch.
Here, the branch to remove is not named as remotes/origin/branch but simply branch. And the branch is removed.


There's a shortcut to delete the branch in the origin:

git push origin :<branch_name>

Which is the same as doing git push origin --delete <branch_name>


The meaning of remotes/origin/test is that you have a branch called test in the remote server origin. So the command would be

git push origin --delete test