How to Delete a Local and Remote Git Branch

Previously we wrote a quick guide on how you can easily rename a local and remote git branch. In this case we’ll be focusing on how you can delete a local and remote git branch in a few simple steps.

Delete a Local and Remote Git Branch

It is common to have different branches in a Git repository. They are a great way to work with different features and fixes while the new code is kept isolated from the existing code base to prevent accidental changes to the production-ready code.

Local

Git will not allow you to delete a branch you are currently located in, so you must first make sure to checkout a branch that you are not trying to delete:

git checkout local_branch

Then, to delete the unwanted branch:

git branch -d local_branch

The -d option will remove the branch only if it has been pushed and merged with the remote branch. Use -D if you want to force the removal of a branch, even if it has not yet been pushed or merged.

Remote

As local and remote branches are totally different, deleting a local branch doesn’t automatically delete the remote branch.

git push <remote> --delete branch_name
output

 - [deleted]         branch_name

If you get the following message it means that the branch has been previously deleted by someone else:

error: unable to push to unqualified destination: branch_name 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@repo . . .'

Leave a Comment