How do I delete origin/master in Git

10

5

I cannot remove origin/master from my server. No idea why.

Screenshot of my terminal

The error message is the following

remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To acanzian@elab.ecn.purdue.edu:/export/home/a/elabshare/git/ID-check.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'acanzian@elab.ecn.purdue.edu:/export/home/a/elabshare/git/ID-check.git'

No, I am not using Github.

Atcold

Posted 2014-04-30T15:56:12.587

Reputation: 247

1Why would you want to do that? Are there other branches? – Daniel B – 2014-04-30T16:14:24.673

Indeed there are other branches (check the link to the image, actually, could you display it for me, please?). We had to rebase master, and we did it on a temporal branch which now should become master. The only problem is that I don't know why I cannot kill master on the server. – Atcold – 2014-05-01T20:53:13.983

2Maybe not the answer you're looking for, but you could force-push the rebased master to origin/master, effectively replacing the old one with the new one. – SlightlyCuban – 2014-05-01T21:28:17.763

In addition to the error message, please post the command you're using. – Kyralessa – 2014-05-01T22:51:49.117

@Kyralessa, check the screenshot. – Atcold – 2014-05-03T16:58:23.050

@SlightlyCuban, I was trying to avoid forcing stuff and find instead a "clean" way :) – Atcold – 2014-05-03T18:11:45.257

You're not the only one I've seen with the notion that "force" is a terrible or messy thing to do. We're always told not to force things. Forcing a door knob might break it. Forcing a screw might strip the head. But forcing Git isn't going to do any damage, as long as you're aware of what commits it'll replace. You won't mess up Git or break it or leave it unable to function. Having to put -f is just a way of saying, "I realize that when doing this, I could lose work, but I know what I'm doing, so go ahead and do it anyway." – Kyralessa – 2014-05-04T22:55:08.827

@Atcold, I don't normally recommend forcing, but it sounded like you were going to point master to a different branch anyway. In this case, --force and delete && push would have the same end-result. The only difference: deleting would be a 2-step operation, which might be cleaner if you didn't plan to push immediately after. – SlightlyCuban – 2014-05-05T14:19:43.253

@SlightlyCuban, my first concern was understanding why my machine was not bending to my will. Now I'm her master again! :) – Atcold – 2014-05-05T23:26:33.697

Answers

7

Fun fact: even remote repositories are on a branch. You're getting rejected because you're trying to delete the branch that your origin has currently "checked out".

If you have direct access to the repo, you can just open up a shell bare repor directory and use good old git branch to see what branch origin is currently on. To change it to another branch, you have to use git symbolic-ref HEAD refs/heads/another-branch.

If you are using a service like Github or Gitorious, you're going to have to use the UI the tool provides you to make the change (see this answer for how to do that in common tools).

SlightlyCuban

Posted 2014-04-30T15:56:12.587

Reputation: 605

Awesome, it worked and it gave me the chance of learning more about refs in Git! Thank you very much! (Yeah, I knew about how to deal with it on Github, but not on our personal server.) – Atcold – 2014-05-03T18:10:10.437

12

In lieu of actually removing master from the server, you can replace it like this:

git push origin otherbranch:master -f

That will replace master with the contents of otherbranch, but it'll still be called master on the remote. And then you can check out master as master in your local.

Kyralessa

Posted 2014-04-30T15:56:12.587

Reputation: 828

1+1. This was the only way I could delete a bad first commit on remotes/origin/master. – ctn – 2015-05-26T16:28:52.243

OK, thanks! I know I can force stuff, but I was wondering exactly why I couldn't remove the master branch, and the accepted answer explains why. – Atcold – 2014-05-04T20:51:59.183