Deleting remote master branch, refused due to being the current branch
Asked Answered
L

5

114

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.

Loch answered 26/12, 2012 at 12:44 Comment(5)
possible duplicate of I cant delete a remote master branch on gitPhosphorylase
@cirosantilli So what should I do?Loch
If you agree with the duplicate status, it is best to make an edit and say you agree. If you disagree, please explain on a comment why you disagree. I have already flagged, so it is already on a review list for "big rep" poeple to vote upon duplicate status. In any case, even if marked as duplicate you won't lose any rep.Phosphorylase
@cirosantilli I cannot find button, which allows me to agree.Loch
There is no specific button, I meant just edit the post. But a comment works too, so need anymore.Phosphorylase
B
170

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.

Bortz answered 26/12, 2012 at 16:1 Comment(6)
FYI now in 2020 this is only possible for the admin of the repo (which might not be you!)Masuria
@Masuria Good to know indeed. I suppose if you are the owner of the repository, you would be able to perform this operation.Bortz
This applied to Github as well. After I set default branch to something else, I was able to delete the original branch.Olette
@MarsAndBack Thank you for this feedback. I have included your comment in the answer for more visibility.Bortz
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.Brittaniebrittany
@Brittaniebrittany OK. "deselect Include .gitignore" to avoid that .gitignore to be created. Makes sense.Bortz
S
90

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
Sawyor answered 26/12, 2012 at 13:41 Comment(1)
And you have to be the administrator to be able to make such changes.Amadou
L
5

Before deleting a branch, you have to make sure that the branch being deleted is not the default branch. In Github, go to repository settings: enter image description here

Find Default branch and switch the default branch:

enter image description here

Now you can run the following to delete the remote branch.

git push origin --delete <branch-name>
Lilla answered 12/9, 2023 at 10:26 Comment(0)
C
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.

Cameroun answered 27/2, 2021 at 17:59 Comment(0)
P
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

Pazit answered 21/7, 2022 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.