How to Delete Master Branch in Git

John Wachira Feb 02, 2024
How to Delete Master Branch in Git

This article illustrates how you can delete your master branch in Git. Suppose, for some reason, you want to remove the master branch in the remote repository and start on a clean slate; how do you go about it?

Delete Master Branch in Git

Assuming our remote repo is hosted on GitHub, we can run the command below to delete the master branch from the remote repository.

$ git push origin --delete master

However, running the command above will get an error message. This is because you cannot delete the default branch in GitHub.

You must create a placeholder for your master branch and make it the default branch in GitHub.

We will run the command below to create a placeholder for our master branch.

$ git checkout -b placeholder

We can then push the branch to the remote repository.

$ git push -u origin placeholder

Once that is done, go to your personal GitHub account and navigate to the repository at hand. Tap on the Settings icon, and select Branches from the menu on your left.

Change the default branch to the placeholder and return to the command line. You can now delete the master branch.

$ git push origin --delete master

The command will delete the master branch in the remote repository. It will not delete it from the local repository.

If you want to delete it from the local repo, run the below command.

$ git branch -d master

You can delete your master branch in the remote repo on the command line. However, you will have to make another branch the default branch.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Delete