275

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 ?

Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 2
    `git branch -a` will list the branches in your local and not from your remote. Right? –  Mar 11 '16 at 13:34
  • I think all of them. I am not sure. – code-8 Mar 11 '16 at 13:36
  • 3
    It will show the remote branches within your local. It will not list all the remote branches. –  Mar 11 '16 at 13:38
  • Possible duplicate of [How do I delete a Git branch both locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely) – peterh Jun 02 '17 at 10:07
  • 4
    @peterh 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 title – Andrew Spencer Mar 02 '18 at 09:15
  • 1
    I made an edit to the title, hope this is closer to the question's intent – Andrew Spencer Mar 02 '18 at 09:16
  • 4
    `git fetch --prune` – hassanzadeh.sd Mar 11 '20 at 09:33

15 Answers15

570

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    Also `git branch -r | grep "origin"` will help with big projects that use fork – Seraf Oct 23 '17 at 15:52
  • 3
    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. – Andrew Spencer Mar 02 '18 at 09:21
  • 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. – 2540625 Jun 28 '21 at 18:02
  • @2540625 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](https://stackoverflow.com/q/12399002/216074). This completely depends on your shell though. – poke Jun 29 '21 at 11:38
  • Thanks for the reminder. I have employed such a solution, but apparently this is failing edge case of some sort. – 2540625 Jun 29 '21 at 15:52
  • A great reminder of how to best access to confusing topics in git! All of your suggestions worked and I learned something, [@poke](https://stackoverflow.com/users/216074/poke). – DataSci-IOPsy May 09 '22 at 13:33
  • 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. – Nick Morhun Jan 06 '23 at 09:10
  • @poke May I ask If I want to push the "local remote branch" to remote instead of delete it? – SCKU Mar 22 '23 at 08:30
  • 1
    @SCKU Just skip the `--delete` then to actually push your local branch to the remote. – poke Mar 23 '23 at 12:25
  • NB: If your branch is in some other remote (not `origin`), the command becomes `git fetch --prune ` – aidandeno Jun 14 '23 at 12:54
159

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
drosam
  • 2,866
  • 3
  • 16
  • 18
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>

Ricardo Magalhães
  • 1,843
  • 15
  • 10
16
  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
MovGP0
  • 7,267
  • 3
  • 49
  • 42
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.

Max Wong
  • 694
  • 10
  • 18
  • 4
    +1 for the comment re: the branch name, I was trying to delete `origin/branch` when it should just be `branch`. – ken May 27 '20 at 17:51
  • 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. – Mote Zart Apr 09 '22 at 20:19
  • git fetch --prune flag solved it for me. Apparently only git fetch doesn't give an updated remote branch list. – maxorcist Apr 20 '22 at 10:08
11

git push origin --delete yourBranch

11

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
Symeon Mattes
  • 1,169
  • 16
  • 40
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 'git@10.102.100.38: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

Buggy B
  • 623
  • 7
  • 18
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!

eddiewould
  • 1,555
  • 16
  • 36
  • thanks for this code! this is the only git code I tested working on deleting all remote branches except master. – Alvin Mar 18 '19 at 10:19
2

This should help:

  1. git fetch
  2. git push origin --delete branchName
kk.
  • 3,747
  • 12
  • 36
  • 67
Aman Singh
  • 347
  • 2
  • 4
2

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

melo
  • 61
  • 3
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.

  • 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" worked – user10293779 Nov 07 '19 at 02:41
0

For windows

git branch --remotes| %{ $_.Trim().Split("/")[1] }| ?{ $_ -ne 'master' } | | ?{ $_ -ne 'otherBranch' } | %{ git push origin --delete $_ }
Moumit
  • 8,314
  • 9
  • 55
  • 59
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.

Homer
  • 7,594
  • 14
  • 69
  • 109
-2
git push origin --delete origin/test 

should work as well