How To Delete a Local and Remote Git Branch
Updated on
•6 min read

Branches are part of the everyday development process and one of the most powerful features in Git. Once a branch is merged, it serves no purpose except for historical research. It is common and recommended practice to delete the branch after a successful merge.
This guide covers how to delete local and remote Git branches.
Delete a Local Git Branch
The git branch command allows you to list, create
, rename
, and delete branches.
To delete a local Git branch, invoke the git branch command with the -d (--delete) option followed by the branch name:
git branch -d branch_nameDeleted branch branch_name (was 17d9aa0).
If you try to delete a branch that has unmerged changes, you’ll receive the following error message:
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 you can see from the message above, to force the deletion of a branch,use the -D option which is a shortcut for --delete --force:
git branch -D branch_namePlease note, if you delete an unmerged branch, you will lose all the changes on that branch.
git branch --no-merged command.If you try to remove the current branch, you’ll get the following message:
error: Cannot delete branch 'branch_name' checked out at '/path/to/repository'
You can’t delete the branch you’re currently on. First, switch to another branch and then delete the branch_name:
git checkout mastergit branch -d branch_name
Delete a Remote Git Branch
In Git, local and remote branches are separate objects. Deleting a local branch doesn’t remove the remote branch.
To delete a remote branch, use the git push command with the -d (--delete) option:
git push remote_name --delete branch_nameWhere remote_name is usually origin:
git push origin --delete branch_name...
- [deleted] branch_name
There is also an alternative command to delete a remote branch, that is, at least for me, harder to remember:
git push origin remote_name :branch_nameIf you are working on a project with a group of people and try to delete a remote branch that is already removed by someone else, you will receive the following error message:
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’ll need to synchronize your branch list with:
git fetch -pThe -p option tells Git to remove any remote-tracking references that no longer exist on the remote repository before fetching.
Conclusion
We’ve shown you how to delete local and remote Git branches. Branches are basically a reference to a snapshot of your 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 should be removed.
If you hit a problem or have feedback, leave a comment below.


