110

I need to delete a master branch, but it's proving to be hard. I just want to clean that branch out and start new. I am deleting from the dev branch. I want master on GitHub to be clean.

 # git push origin --delete master

> To https://github.com/mymasterb.git  ! [remote rejected] master
> (deletion of the current branch prohibited) error: failed to push some
> refs to 'https://github.com/mymaster.git'

How do I quite simply start my master with a fresh slate?

James Ko
  • 32,215
  • 30
  • 128
  • 239
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • What do you mean by "fresh slate"? You want your master branch to be completely empty but you want all your other branches to still be there? – Jon Lin Aug 31 '12 at 04:17
  • Possible cross site duplicate of: http://superuser.com/questions/294407/cant-remove-remote-branch-in-git – Ciro Santilli OurBigBook.com Feb 01 '14 at 21:24
  • possible duplicate of [How do I change a Git remote HEAD to point to something besides "master"](http://stackoverflow.com/questions/1485578/how-do-i-change-a-git-remote-head-to-point-to-something-besides-master). Duplicate because since it says `deletion of the current branch prohibited` it is obvious that the current branch must be changed, which is what the accepted answer explains. That is exactly what is explained on the suggested duplicate, including the github solution because of the tag. – Ciro Santilli OurBigBook.com Feb 02 '14 at 20:57

3 Answers3

175

As explained in "Deleting your master branch" by Matthew Brett, you need to change your GitHub repo default branch.

You need to go to the GitHub page for your forked repository, and click on the “Settings” button.

Click on the "Branches" tab on the left hand side. There’s a “Default branch” dropdown list near the top of the screen.

From there, select placeholder (where placeholder is the dummy name for your new default branch).

Confirm that you want to change your default branch.

Now you can do (from the command line):

git push origin :master

Or, since 2012, you can delete that same branch directly on GitHub:

GitHub deletion

That was announced in Sept. 2013, a year after I initially wrote that answer.

For small changes like documentation fixes, typos, or if you’re just a walking software compiler, you can get a lot done in your browser without needing to clone the entire repository to your computer.


Note: for BitBucket, Tum reports in the comments:

About the same for Bitbucket

Repo -> Settings -> Repository details -> Main branch
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note: "placeholder" is an example of any other branch than master. The idea is to define *another* branch as the Github repo default branch. – VonC Aug 31 '12 at 04:54
  • @Tampa I have edited my answer to reflect the fact there is no longer a green tick. – VonC Aug 31 '12 at 05:49
  • 1
    @VonC, is this something that can be done from the command line? I am trying to write a script to do the same. To simulate the bare remote repository, I have a bare remote repository on my Workstation, but it wont let me switch branches either.. – alpha_989 Apr 11 '18 at 16:18
  • 2
    @alpha_989 In a bare repo, to change the default branch, set the symbolic-ref: https://stackoverflow.com/a/3302018/6309 – VonC Apr 11 '18 at 21:35
  • 3
    Thanks! About the same for Bitbucket. Repo -> Settings -> Repository details -> Main branch – Tum Feb 07 '19 at 14:50
  • 2
    @Tum Thank you. I have included your comment in the answer for more visibility. – VonC Feb 07 '19 at 15:21
20

To answer the question literally (since GitHub is not in the question title), also be aware of this post over on superuser. EDIT: Answer copied here in relevant part, slightly modified for clarity in square brackets:

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 [in the bare repo] 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.

dave_k_smith
  • 655
  • 1
  • 7
  • 22
  • Would you be open to posting this as a comment than an answer? The link to superuser is nice. – zedfoxus Mar 07 '20 at 18:49
  • 1
    @zedfoxus, I'm willing to consider - but the add comment box below the main post says "Avoid answering questions in comments." I think mine is an answer to the literal question posed. But maybe I'm missing your angle? – dave_k_smith Mar 07 '20 at 20:25
  • 2
    I was thinking that if this was a comment, some of the links would stay together under comments while explanations would stay as an answer. But it’s your thought that matters. I’m fine with your thought process also that this can stay as an answer. – zedfoxus Mar 08 '20 at 13:17
  • 3
    This should be the accepted answer. Maybe, @dave_k_smith, you would be willing to actually expand your answer with the information of the post on superuser? – esrehmki May 23 '20 at 13:37
8

The quickest way is to switch default branch from master to another and you can remove master branch from the web interface.

Jackkobec
  • 5,889
  • 34
  • 34