Branches are part of the daily development process and one of Git’s most powerful features. Once a branch is merged, it serves no purpose other than historical research. It is common and recommended to delete the branch after a successful merge.

How to Delete Local and Remote Git Branch
This guide explains how to delete local and remote Git branch.
Delete a local Git branch
To delete a local Git branch use the command git branch
with option -d
( --delete
):
git branch -d branch_name
Deleted branch branch_name (was 17d9aa0).
error: The branch ‘branch_name’ is not fully merged. If you are sure you want to delete it, run ‘git branch -D branch_name’.
As the message above says, you can force the deletion using the option -D
which is a shortcut for --delete --force
:
git branch -D branch_name
Note that if you delete an unmerged branch, you will lose all changes on that branch.
To list all branches that contain non-changes git branch --no-merged
, use the command git branch --no-merged
.
If you try to remove the current branch, the following message will be displayed:
error: Cannot delete branch ‘branch_name’ checked out at ‘/path/to/repository’
You cannot delete the branch you are currently in. First, switch to another branch and then delete branch_name
:
git checkout master
git branch -d branch_name
Delete a remote Git branch
In Git, local and remote branches are separate objects. Deleting a local branch does not remove the remote branch.
To delete a remote branch, use the command git push
with option -d
( --delete
):
git push remote_name --delete branch_name
Where remote_name
is it usually origin
:
git push origin --delete branch_name
... - branch_name
There is also an alternative command to delete a remote branch, which is, for me at least, more difficult to remember:
git push origin remote_name :branch_name
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 ‘[email protected]:/my_repo’
In situations like this, you will need to sync your branch list with:
git fetch -p
The option -p
tells Git to remove all remote trace references that no longer exist in the remote repository before fetching.
Conclusion
In this tutorial, you learned how to delete local and remote Git branches. Branches are basically a reference to a snapshot of the changes and have a short life cycle. Once the branch is merged into the master (or another main branch), it is no longer needed and must be removed.
With the command git branch
, you can also rename, create and list local and remote Git branches.