How to delete HEAD branch on remote Git repository?

Pablo picture Pablo · Jul 13, 2014 · Viewed 16.3k times · Source

This is remote Git repository on server

[aaa@web48 proj.git]$ git ls-remote .
dfca707432eb53678b37026b160a4bdc7f1ac6c3    HEAD
dfca707432eb53678b37026b160a4bdc7f1ac6c3    refs/heads/master
1e09c37443ee758644a712e3c1a8b08b18a1f50d    refs/heads/placeholder

I want to delete HEAD/master branch. How can I do it either on server or remotely? I'm using Tower client.

Answer

user456814 picture user456814 · Jul 13, 2014

You cannot delete a remote branch if it's currently the default HEAD branch

The HEAD symbolic reference on a remote bare repo represents the default branch for that repo. Any non-bare clones of that repo will automatically checkout that branch after the clone.

Because it's the default, you can't just delete it like you normally would, Git won't let you:

$ git push origin --delete master

remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error:
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error:
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To c:/Users/Keoki/Documents/GitHub/bare
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'c:/Users/Keoki/Documents/GitHub/bare'

The error message above points out that you can bypass the safety checks to delete the current HEAD branch in the remote anyways, but I'm going to show you how to change what the default branch is, so that you can still keep a default branch, but delete master like you wanted to.

Changing the default HEAD branch from the command line

You can change what the default branch is in the remote repo if you have access to the remote. If you're using a hosting provider like GitHub or Bitbucket, they should allow you to change the default branch through their web interface.

So if you have access to the remote, use the following command to change which branch the symbolic reference HEAD points to:

git symbolic-ref HEAD refs/heads/<newDefaultBranch>

Changing the default HEAD branch on GitHub or Bitbucket

As I've already mentioned in the previous section, you can update the default HEAD branch in your remote repo through the web interface if you use a hosting provide like GitHub or Bitbucket.

GitHub

Go to your repo's Settings tab, and you'll see the default branch setting right at the top,

GitHub settings

Bitbucket

Got to your repo's Settings tab, and you'll see the default branch setting near the middle,

Bitbucket settings

Update your local clones' references to the default branch in the remote

Once you've updated the default branch in the remote bare repo, you'll need to update where your local clones of that repo think that the default HEAD branch in the remote points to. You can do that with

git remote set-head <remote> --auto

# Or shorter
git remote set-head <remote> -a

You can confirm that the local repo has been properly updated using

$ git branch -r
  origin/HEAD -> origin/foo
  origin/foo
  origin/master

Now you can delete the master branch on the remote

Now that you've changed the default HEAD branch on the remote to be something other than the master branch, you'll be able to delete it on the remote,

$ git push origin --delete master

To c:/Users/Keoki/Documents/GitHub/bare
 - [deleted]         master

# Older syntax
$ git push origin :master

Additional References and Documentation