Open In App

Delete a Git Branch Locally and Remotely

Last Updated : 23 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

It is common for a git repo to have different branches. They are a good way to work on different features and fixes while isolating the new code from the main codebase. Repos often have a master branch or a main branch for the main codebase and developers create other branches with names of their choice (or as required by the organization) to work on different features or fixes.  Most of the time you might have to delete a git branch, because of some irreversible changes, security issues, or when a particular feature of the related project has been built. In most cases, it is simple to delete a git branch. In this article, I’ll guide you through the entire process of how to delete a git branch securely. To get started I have already created a new branch in my repo using:

git checkout -b <branch-name> 

Here <branch-name> is test. To check which is the current working branch you can use the git branch command.

we can see that we are in the test branch

and the following is a snapshot of my Github repository with the test branch in the remote.

Delete a Branch Locally 

Git won’t allow you to delete the branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:

git checkout <branch-name>

Here we will check out our main branch from my test branch.

Now in order to delete the test branch locally, we use the command :

git branch -d <branch-name>

We will delete my test branch as an example.

Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with –delete –force. This will forcefully delete the branch even if it hasn’t been pushed or merged with the remote. the full command is:

git branch -D <branch-name>

With this, we can successfully delete a local branch.

Delete a Branch Remotely

You can’t use the git branch command to delete a remote branch. Instead, you have to use the git push command with the –delete flag, followed by the name of the branch that we want to delete. You also need to specify the remote name (origin in this case) after “git push”. The command is as follows:

git push <remote-name> --delete <branch-name>

Here I will delete my test branch in my remote repository as shown below.

This command will delete the branch remotely. You can also use the shorthand:

git push <remote-name> :<branch-name>

As you can see my remote branch is no more in my GitHub repo:

With this, we have successfully deleted our remote branch. A common error faced by many in this step is:

error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to ‘git@repository_name’

This means that someone has already deleted the branch that you want to delete. If this happens you can use the following command to synchronize your branch list in the local environment:

git fetch -p

The -p flag here means “prune”. After fetching the branches which no longer exist in remote will be deleted in your local working environment. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads