Git says remote ref does not exist when I delete remote branch
Asked Answered
H

15

295

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 ?

Heidy answered 11/3, 2016 at 13:31 Comment(7)
git branch -a will list the branches in your local and not from your remote. Right?Lipo
I think all of them. I am not sure.Heidy
It will show the remote branches within your local. It will not list all the remote branches.Lipo
Possible duplicate of How do I delete a Git branch both locally and remotely?Posthorse
@Posthorse Looks to me like it's really asking "why can't I delete a remote branch" (probably when the branch has been deleted off the remote but the remote ref is still present locally) and therefore, not a duplicate but could do with a clearer titlePipes
I made an edit to the title, hope this is closer to the question's intentPipes
git fetch --pruneChewy
S
609

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.

Saul answered 11/3, 2016 at 13:35 Comment(12)
Also git branch -r | grep "origin" will help with big projects that use forkLonganimity
So there are 2 reasons for getting this message from git: either the branch was already deleted on the server, or you wrongly prefixed the branch name with remotes/origin/. Or both.Pipes
I wonder why Git doesn't auto-complete the branch name upon keypress of tab when I type git push origin -d followed by the first letter of the branch name.Roddie
@Roddie Because auto completion isn’t something that Git the command line program can do. There are integrations that allow you to do this though, for example explained in this question. This completely depends on your shell though.Saul
Thanks for the reminder. I have employed such a solution, but apparently this is failing edge case of some sort.Roddie
A great reminder of how to best access to confusing topics in git! All of your suggestions worked and I learned something, @poke.Soricine
I am getting this error even though I can see that the branches exist on GitHub. As soon as I deleted them using GitHub and fetched, their references were correctly removed.Forerun
@Saul May I ask If I want to push the "local remote branch" to remote instead of delete it?Monocyte
@Monocyte Just skip the --delete then to actually push your local branch to the remote.Saul
NB: If your branch is in some other remote (not origin), the command becomes git fetch --prune <remote name>Sleek
This doesn't seem to be true. git branch -a has a lot of stuff in it, but I called the prune several times, (and again to be true), but it didn't affect the contents of git branch -a.Melville
This one does the trick with the prune attribute.Pointing
G
160

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
Goldoni answered 11/3, 2016 at 13:33 Comment(0)
G
34

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>

Germin answered 11/7, 2017 at 10:20 Comment(0)
T
15
  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
Themselves answered 28/7, 2017 at 21:4 Comment(1)
Nope, this gives the same error: remote ref does not existNathannathanael
L
15

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.

Lisp answered 22/12, 2019 at 5:22 Comment(4)
+1 for the comment re: the branch name, I was trying to delete origin/branch when it should just be branch.Bouquet
Yeah, mine was also not remotes/origin/branch even though branch -a cleary said the path was exactly that. It ONLY worked when I used just the branch name alone, as you said. So thx for posting as it would have taken awhile before I tried that.Actinia
git fetch --prune flag solved it for me. Apparently only git fetch doesn't give an updated remote branch list.Unblushing
Didn't work, I got the same error.Melville
M
12

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
Milkfish answered 27/11, 2017 at 14:51 Comment(0)
C
10

git push origin --delete yourBranch

Colo answered 11/3, 2016 at 13:34 Comment(0)
M
7

Quick Answer

You just need to prune the list (clean the list) since that branch does not exist anymore.
Run the following command

git fetch --prune

to verify run

git branch -r

You should see a new list with only existing remote repos

Midnight answered 23/1, 2023 at 9:55 Comment(2)
Doesn't help, git fetch --prune just removes the local ones.Melville
Hey @kovarex, this solution is for someone whose remote repo is deleted but still persists locally. If you want to delete a remote repo run git push origin -d branch-name Midnight
C
4

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

Cookshop answered 3/6, 2019 at 16:50 Comment(0)
P
3

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!

Pretrice answered 25/7, 2017 at 23:16 Comment(1)
thanks for this code! this is the only git code I tested working on deleting all remote branches except master.Lilli
L
1

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.

Lipo answered 11/3, 2016 at 13:36 Comment(1)
this is helpful. it solved my delete problem. i am wondering why 'git push --delete origin/test' did not work, while "git push --delete test" workedAzobenzene
K
1

This should help:

  1. git fetch
  2. git push origin --delete branchName
Keystone answered 11/9, 2020 at 5:52 Comment(1)
This command doesn't work, the same error.Melville
Q
0

For windows

git branch --remotes| %{ $_.Trim().Split("/")[1] }| ?{ $_ -ne 'master' } | | ?{ $_ -ne 'otherBranch' } | %{ git push origin --delete $_ }
Quiberon answered 18/8, 2020 at 11:27 Comment(0)
L
0

I tried all of these answers. None of them worked for me. I think I had somehow criss-crossed some repos and branches. I fixed it in Visual Studio by going to Options -> Source Control -> Git Repository Settings -> Remotes. There I saw the thing I was trying to delete. I just selected it and clicked the remove button.

Larkin answered 13/7, 2023 at 22:53 Comment(0)
M
-3
git push origin --delete origin/test 

should work as well

Marcusmarcy answered 11/3, 2016 at 17:3 Comment(1)
It should be git push origin --delete testMacdonald

© 2022 - 2024 — McMap. All rights reserved.