Herr Bischoff

Git: Cannot Delete Remote Branch After Renaming Default Branch

Usually, changing the default branch name is done like this, as outlined in the Git documentation:

git branch --move master main
git push --set-upstream origin main
git push origin --delete master

When using bare remote repositories, like with cgit or manually via SSH, following the above instructions may not work. Likely, you will run into the following error message on the last step:

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

This briefly confused me. The solution is nevertheless simple. The HEAD reference (“current branch” as described in the error message above) needs to be updated. Otherwise, automatic checkout while cloning would fail. All data would still be there and safe but not checked out. The checkout would appear to be empty, probably giving you quite a shock.

To avoid this, update the default reference:

cd /path/to/bare/remote/repository
git symbolic-ref HEAD refs/heads/main

Now, deleting the former default branch immediately succeeds.