In this article, we have presented the technical details why deleting a master branch is not straight-forward and presented the commands to delete the master branch of any Git repository (local or remote) and also, covered the case when the repository is hosted at GitHub, GitLab or BitBucket.
Table of contents:
- What is master branch?
- Delete master branch in local or remote repository
- Delete master branch in GitHub repository
- Delete master branch in GitLab / BitBucket repository
Deleting a local branch is one thing and Deleting a master branch is another. It may seem unreasonable that one wants to delete a master branch but in reality, it is reasonable as master branch is just another branch with some special properties which can be transferred to other branches.
In short, the command to delete a master branch is:
git symbolic-ref HEAD refs/heads/<branch2> git push origin -d master
What is master branch?
Every Git repository has an active branch which by default is the master branch. This is the branch the repository is checked in. The HEAD of the Git repository points to this master branch.
The problem in deleting a master branch is:
- If master branch is deleted, there needs to be another active branch.
- If master branch is deleted, what will HEAD point to?
The process of deleting a master branch either in local or remote repository will require to address these two points.
Delete master branch in local or remote repository
First, we will check the references in your repository, check if HEAD reference is master and if so, we update it to another reference branch.
Find the references in your Git repository using the following command:
Use the following command to check the reference for HEAD:
git symbolic-ref HEAD
If the HEAD reference is the master branch, update the HEAD in your Git repository using the following command:
git symbolic-ref HEAD refs/heads/<branch2>
Replace branch2 with the branch you want to be pointed to by HEAD.
With this, we have made the master branch just a normal branch which you can delete as usual. You can delete the master branch now using the following commands:
git push origin -d master
Check the detailed commands to delete a remote branch here.
Delete master branch in GitHub repository
When dealing with a Git repository hosted at GitHub, the commands in the previous section will not work directly. This is because in GitHub, the master branch is set as the default branch and hence, cannot be deleted.
If you want to delete the master branch in a GitHub repository, you need to first change the default branch in the concerned repository and then, delete the master branch. Following are the commands to change the default branch in GitHub:
- Go to your repository in GitHub using a web browser
- Click on "Settings".
- Then, click on "Branches" (on the left side vertical bar).
- You will set a dropdown menu for "Default branch". Using this, change the default branch to any other branch or select placeholder (if you do not have other branches and you need a dummy branch for now).
- Save the changes.
Once done, delete the master branch by:
- Using the GitHub UI in the browser
- OR, clone the repository locally, delete the master branch locally and push the changes
Delete master branch in GitLab / BitBucket repository
Similar to GitHub, other services like GitLab and BitBucket also set master branch as the default branch and hence, if you need to delete it:
- First, change the default branch in your repository settings in the service using web browser
- Delete the master branch using the same commands as in previous section
With this article at OpenGenus, you must have the complete idea of how to delete the master branch of a Git repository.