How to delete the master branch

If you want to remove the default branch named master from your git repository this is how it works:

  • after initializing your project create a new branch based on master and push it to origin

git branch -m master main

git push —-set-upstream origin main

  • delete your local master branch

git branch -D master

You have to be the owner of the repository in order to change these settings.

gitlab has set that the master branch as default and protected, which means you can not just remove it from your local machine. You first need to remove the protection and set main as your new default.

Overview of branches. Master is default and protected

So in order to be able to remove the master, we have to first remove the default and the protected flag. You can do this as a project maintainer in Settings / Repository

gitlab Settings Repository Default Branch

Choose your new main branch as default branch in the dropdown. And then move over to Protected Branches

Settings Repository Protected Branches

Choose main branch as protected and set rules for allowance of merging, pushing and owner approval and save your changes. Press the Unprotect Button for master.

Now you are able to delete the master branch.

You have to be owner of the repository in order to change these settings.

Depending on your repositories settings the master branch can be protected, but is always set to default. First we have to choose the main branch as our new default branch.

Branches overview

On the branches overview master is marked as default. Click on the Change default branch Button.

Settings Branches

On the dropdown, choose main instead of master and click update. On the Branch protection rules for master click edit.

Settings Branch protection rules

Change the branch name pattern to main and save changes. Back on the Branch Overview, delete master.

Thank you for deleting master.

Frontend Engineer, aiming for a better world.