Deleting remote master branch, refused due to being the current branch

GitGithubBitbucket

Git Problem Overview


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://[email protected]/user/reponame.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'ssh://[email protected]/user/reponame.git' 

Also I tried

git push bb :master

But all this does not work.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomilov AnatoliyView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitWouter JView Answer on Stackoverflow
Solution 3 - GitleanneView Answer on Stackoverflow