29

I'm trying to remove a remote branch with git:

rpinson@rpinson:~/dev/charismanie$ git remote show origin 
* remote origin
  Fetch URL: git@github.com:raphink/Charismanie.git
  Push  URL: git@github.com:raphink/Charismanie.git
  HEAD branch: xetex
  Remote branches:
    master tracked
    xetex  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
rpinson@rpinson:~/dev/charismanie$ git push origin :xetex
remote: error: refusing to delete the current branch: refs/heads/xetex
To git@github.com:raphink/Charismanie.git
 ! [remote rejected] xetex (deletion of the current branch prohibited)
error: failed to push some refs to 'git@github.com:raphink/Charismanie.git'

Is there a way I can remove this branch from the list of "Remote branches" so I can remove it?

3

3 Answers 3

50

The branch was set as default on github, preventing it from being deleted.

On github, I went to "Administration" in the project, reset the default branch to "master" and I was able to remove the xetex branch.

6
  • 1
    Awesome! was looking how to do that on GitHub for 20 minutes before I searched here.
    – rlemon
    Jan 20, 2012 at 20:55
  • 1
    This also happens with BitBucket, and is fixed in a similar fashion.
    – John B
    Mar 8, 2013 at 14:35
  • Is there no pure git way of doing it? Feb 1, 2014 at 21:19
  • github default branch is not a git feature, so no.
    – raphink
    Feb 2, 2014 at 20:55
  • I also encounter with this problem. I think it's better to have some tips showing that it's default?? I am stuck with it all day....
    – WesternGun
    Aug 31, 2016 at 11:16
1

For what it's worth in BitBucket:

  • click on Settings in the left menu
  • under 'Repository Details' find 'Main Branch' drop down menu
  • select a different branch from the one you want to delete

There's also an option to prevent branch deletion under the 'Branch management' section, but you have to actively set that yourself first.

0

If you want to do this programmatically (it is impractical to reconfigure a large number of repositories by visiting each in the browser), the default_branch configuration can be manipulated via GitHub's REST API (source).

You can use both GitHub's official gh CLI as well as the older inofficial hub CLI; they both support an api subcommand (you could even use curl, but then have to handle authentication yourself):

$ gh api --method PATCH -f default_branch=NEW_NAME 'repos/{owner}/{repo}'

Of course, for single repositories it's easier to do via the browser. Here are some detailed instructions for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.