33

I know this has been covered before, but I have tried the following and can't seem to delete the remote branch.

aly@neon:~/workspace/3DOD_VARIANCE$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/multi_gauss_at_nodes
  remotes/origin/old-state-with-mean-deviation-from-centre
  remotes/origin/variance-branch


aly@neon:~/workspace/3DOD_VARIANCE$ git branch -r -d origin/old-state-with-mean-deviation-from-centre Deleted remote branch origin/old-state-with-mean-deviation-from-centre (was 0ed90b2).


Fetching origin
From https://bitbucket.org/alykhantejani/3dobjectdetection
 * [new branch]      old-state-with-mean-deviation-from-centre -> origin/old-state-with-mean-deviation-from-centre

As you can see the branch has been fetched again. Any idea what I'm doing wrong?

Also, as a side note, is there a way for me to check if this branch has already been merged back into master before I delete?

Sam
  • 7,252
  • 16
  • 46
  • 65
Aly
  • 15,865
  • 47
  • 119
  • 191
  • 1
    possible duplicate of [How do I delete a Git branch both locally and in GitHub?](http://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-in-github) – Livius Jul 09 '13 at 10:47
  • @Livius Hi, yes I referred to this question first, but with no luck – Aly Jul 09 '13 at 10:48
  • “No luck” is not a helpful error report. Also why does your question not state that you tried the solution given there? – Chronial Jul 09 '13 at 10:54
  • @Chronial, Well "no luck" in this case is well documented in the question as I give the full command line history... – Aly Jul 09 '13 at 10:57
  • The linked answers explain to use `git push`. I do not see one `git push` in your question. Am I missing something? – Chronial Jul 09 '13 at 11:25

4 Answers4

53

To delete a remote branch run following:

git push origin :branch-to-delete

The trick is in colon

n1_
  • 4,227
  • 4
  • 34
  • 35
  • any idea on how to check that this branch has been merged into master before I delete? – Aly Jul 09 '13 at 10:47
  • git branch --contains that-branch. It will give u list of branches, that contain that branch, If ur branch is in the list, u can delete it. – n1_ Jul 09 '13 at 10:48
  • Tried the above command and get the following: unable to push to unqualified destination: origin/old-state-with-mean-deviation-from-centre The destination refspec neither matches an existing ref on the remote nor begins with refs/ – Aly Jul 09 '13 at 10:51
  • it seems like that branch doesnt exist on the server anymore. U should update ur local branch list. See: http://stackoverflow.com/questions/3184555/cleaning-up-old-remote-git-branches run this: git remote prune origin – n1_ Jul 09 '13 at 10:54
  • Found the solution, I was typing :origin/old-state... the origin is not needed. – Aly Jul 09 '13 at 10:55
38

The full push command is the following

git push <remote name> <local branch>:<remote branch>

Just send "no branch at all" to the remote server that way:

git push origin :old-state-with-mean-deviation-from-centre

For the sidenote : git prevents you to delete branch that has not been merged when you use "git branch -d " (and tells you to use -D if you are really sure to delete it anyway).

Also notice the git branch -d -r <branch name> delete the references in your .git folder (and not the real branch located on the remote server), that's why a new fetch will re create it

Asenar
  • 6,732
  • 3
  • 36
  • 49
  • Thanks, this has solved the problem. Please can you emphasize that the origin/ qualifier is not needed on the remote branch name as this was making it fail for me. – Aly Jul 09 '13 at 10:58
  • 2
    all what you can do with "branch" command concerns your local repository, i.e. the .git folder. `git branch -d -r origin/master` concerns the file .git/refs/remotes/origin/master (which contains a sha1) will be deleted, but not the one on the server (because no branch are named "origin/master" which stands for part of the branch name). if you type `git push :origin/old-branch` it will try to remove the branch "origin/old-branch" (taking origin as a namespace) which does not exists. – Asenar Jul 09 '13 at 11:53
  • There is now a `--delete` parameter. See [my answer](https://stackoverflow.com/a/71649399/4653485). – Jérôme Mar 29 '22 at 07:22
12

From @Matthew Rankin's answer:

As of Git v1.7.0, you can delete a remote branch using

$ git push <remote_name> --delete <branch_name>
Jérôme
  • 13,328
  • 7
  • 56
  • 106
7

try

git push origin :remote_branch_to_be_deleted
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90