108

How do I delete a remote master branch from GitHub/Bitbucket?

I'm trying:

# git push bb --delete master
remote: bb/acl: user is allowed. accepted payload.[K
remote: error: refusing to delete the current branch: refs/heads/master[K
To ssh://git@bitbucket.org/user/reponame.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'ssh://git@bitbucket.org/user/reponame.git' 

Also I tried

git push bb :master

But all this does not work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169

4 Answers4

166

Note: for Bitbucket, you would change the default branch by accessing the settings of your repository, and changing the branch at the "Main branch" combo box.

Main branch on Bitbucket

Once the main branch is no longer master, then you can push and remove master.

MarsAndBack confirms in the comments this applies to GitHub as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • FYI now in 2020 this is only possible for the admin of the repo (which might not be you!) – Sliq Nov 06 '20 at 17:05
  • 1
    @Sliq Good to know indeed. I suppose if you are the owner of the repository, you would be able to perform this operation. – VonC Nov 06 '20 at 17:10
  • This applied to Github as well. After I set default branch to something else, I was able to delete the original branch. – Kalnode Jul 03 '21 at 14:17
  • @MarsAndBack Thank you for this feedback. I have included your comment in the answer for more visibility. – VonC Jul 03 '21 at 15:44
  • This requires to push another branch to remove the master and `bitbucket` automatically creates `master` branch with `.gitignore` file as a first commit upon a repo creation. You have to deselect `Include '.gitignore'` to avoid that in first place. – Andry Nov 03 '21 at 14:13
  • @Andry OK. "deselect Include .gitignore" to avoid that `.gitignore` to be created. Makes sense. – VonC Nov 03 '21 at 14:19
88

In the settings block on the options tap in the settings page (well, just click on the Settings tab on your GitHub repository page), you are able to change the default branch (you need to have the other default branch on GitHub as well).

Change default branch on GitHub

After you have done that, you are able to remove it:

$ git push bb :master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wouter J
  • 41,455
  • 15
  • 107
  • 112
3

In 2021:

  • go to Repository Settings
    Repository Settings menu item image
  • scroll down to Advanced
  • scroll down to Main Branch
  • choose the new branch from the dropdown

enter image description here

Then, run your git change command, eg git push bb --delete master

or, in my case, git push origin :master main

Note: You'll have to be the repository's administrator to make changes like this.

leanne
  • 7,940
  • 48
  • 77
1

I did it :

  1. create a new branch.

  2. change it to main branch ( need refresh )

  3. remove master

in BitBucket goto repository details then Advanced then change main branch

enter image description here

then you can delete master from branches in main menu

then you can create master again from your one of branches then reconfigure Master branch as master

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58