Git says remote ref does not exist when I delete remote branch

Git

Git Problem Overview


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 ?

Git Solutions


Solution 1 - Git

The command git branch -a shows remote branches that exist in your local repository. This may sound a bit confusing but to understand it, you have to understand that there is a difference between a remote branch, and a branch that exists in a remote repository. Remote branches are local branches that map to branches of the remote repository. So the set of remote branches represent the state of the remote repository.

The usual way to update the list of remote branches is to use git fetch. This automatically gets an updated list of branches from the remote and sets up remote branches in the local repository, also fetching any commit objects you may be missing.

However, by default, git fetch does not remove remote branches that no longer have a counterpart branch on the remote. In order to do that, you explicitly need to prune the list of remote branches:

git fetch --prune

This will automatically get rid of remote branches that no longer exist on the remote. Afterwards, git branch -r will show you an updated list of branches that really exist on the remote: And those you can delete using git push.

That being said, in order to use git push --delete, you need to specify the name of the branch on the remote repository; not the name of your remote branch. So to delete the branch test (represented by your remote branch origin/test), you would use git push origin --delete test.

Solution 2 - Git

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

Solution 3 - Git

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>

Solution 4 - Git

  1. get the list of remote branches

git fetch # synchronize with the server
git branch --remote # list remote branches

2. you should get a list of the remote branches:

origin/HEAD -> origin/master
origin/develop
origin/master
origin/deleteme

3. now, we can delete the branch:

git push origin --delete deleteme

Solution 5 - Git

git push origin --delete yourBranch

Solution 6 - Git

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.

Solution 7 - Git

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

Solution 8 - Git

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

Solution 9 - Git

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!

Solution 10 - Git

This should help:

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

Solution 11 - Git

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.

Solution 12 - Git

For windows

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

Solution 13 - Git

git push origin --delete origin/test 

should work as well

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncode-8View Question on Stackoverflow
Solution 1 - GitpokeView Answer on Stackoverflow
Solution 2 - GitdrosamView Answer on Stackoverflow
Solution 3 - GitRicardo MagalhãesView Answer on Stackoverflow
Solution 4 - GitMovGP0View Answer on Stackoverflow
Solution 5 - GitMatheus MarsiglioView Answer on Stackoverflow
Solution 6 - GitMax WongView Answer on Stackoverflow
Solution 7 - GitSymeon MattesView Answer on Stackoverflow
Solution 8 - GitBuggy BView Answer on Stackoverflow
Solution 9 - GiteddiewouldView Answer on Stackoverflow
Solution 10 - GitAman SinghView Answer on Stackoverflow
Solution 11 - Gituser1846747View Answer on Stackoverflow
Solution 12 - GitMoumitView Answer on Stackoverflow
Solution 13 - GitSajal ChoukseView Answer on Stackoverflow