gitgit-tower

How can I delete the HEAD branch on a remote Git repository?


This is a remote Git repository on the server

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

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


Solution

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

    The HEAD symbolic reference on a remote bare repository represents the default branch for that repository. Any non-bare clones of that repository 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
    

    Output:

    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 anyway, 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 repository 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 repository through the web interface if you use a hosting provide like GitHub or Bitbucket.

    GitHub

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

    GitHub settings

    Bitbucket

    Got to your repository'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 repository, you'll need to update where your local clones of that repository 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 repository has been properly updated using

    git branch -r
    

    Output:

      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
    

    Output:

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

    And:

    # Older syntax
    git push origin :master
    

    Additional references and documentation