How To Delete a Local and Remote Git Branch

When you work with Git branches every day, finished feature branches and hotfix branches pile up quickly. Once a branch is merged, it serves no purpose except for historical research. Deleting it keeps the branch list clean and makes navigating the repository easier for everyone on the team.
This guide explains how to delete local and remote Git branches, clean up stale references, and recover a branch if you remove one by mistake.
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 will 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 the message suggests, you can force the deletion of a branch with the -D option, which is a shortcut for --delete --force:
git branch -D branch_nameKeep in mind that 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 branch you are currently on, Git will refuse:
error: Cannot delete branch 'branch_name' checked out at '/path/to/repository'Switch to another branch first, then delete the target branch:
git switch other_branch
git branch -d branch_nameDelete 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 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_nameAn older, alternative syntax for the same operation uses a colon prefix before the branch name:
git push origin :branch_nameIf someone else already deleted the remote branch, Git usually returns an error like this:
error: unable to delete 'branch_name': remote ref does not exist
error: failed to push some refs to 'git@example.com:/my_repo'In that case, refresh your remote-tracking references by running:
git fetch -pThe -p option tells Git to remove any remote-tracking references that no longer exist on the remote repository before fetching. You can also use git remote prune origin for the same result without fetching new data.
Delete Multiple Branches
You can pass several branch names to git branch -d in one command:
git branch -d branch1 branch2 branch3A common cleanup task is removing every local branch that has already been merged into main. You can do this by piping the output of git branch --merged to xargs:
git branch --merged main | grep -v '^\*' | grep -v ' main$' | xargs -r git branch -dThis excludes the current branch and main itself. Review the list first by running git branch --merged main on its own before piping it to xargs.
Recover a Deleted Branch
If you delete a branch by mistake, Git does not immediately erase the commits. You can recover it using git reflog, which keeps a log of where HEAD has pointed recently:
git reflogFind the commit hash at the tip of the deleted branch, then recreate the branch from that commit:
git branch branch_name commit_hashThe reflog entries are kept for 90 days by default, so act sooner rather than later.
Quick Reference
For a printable quick reference, see the Git cheatsheet .
| Task | Command |
|---|---|
| Delete a local branch (safe) | git branch -d branch_name |
| Delete a local branch (force) | git branch -D branch_name |
| Delete a remote branch | git push origin --delete branch_name |
| Delete multiple local branches | git branch -d branch1 branch2 branch3 |
| Delete all merged local branches | git branch --merged main | grep -v '^\*' | grep -v ' main$' | xargs -r git branch -d |
| Remove stale remote-tracking refs | git fetch -p |
| Recover a deleted branch | git reflog then git branch branch_name commit_hash |
FAQ
Can I undo a branch deletion?
Yes. Local branches can be recovered from the reflog as long as the commits still exist (typically within 90 days). Remote branches can be restored by pushing the recovered local branch back to the remote with git push origin branch_name.
Should I delete branches after merging?
It is recommended. Merged branches add clutter to the branch list and make it harder to find active work. Most teams delete the source branch immediately after a merge, and platforms like GitHub and GitLab offer an option to do this automatically.
Conclusion
Once a branch is merged, delete it locally with git branch -d and remotely with git push origin --delete. For more on managing remotes
and branch names
, check the linked guides.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page