Delete all local Git branches
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Deleting local Git branches is easy mechanically, but dangerous if you do it without deciding which branches must survive. Git will not let you delete the branch you are currently on, and it distinguishes between safe deletion of merged branches and force deletion of branches that may still contain unique commits.
Start by Switching to a Branch You Intend to Keep
You cannot delete the branch checked out in your working tree. So the first step is to switch to a branch you want to preserve, usually main or master.
If your repository still uses older commands, git checkout main works too. The important part is being off the branches you plan to remove.
Delete Only Merged Local Branches Safely
The safer cleanup path is to delete branches that Git already knows are merged.
That gives you a review step. Once the list looks correct, delete the merged branches while excluding the main branch names you want to keep.
The -d flag refuses to delete branches with unmerged work. That refusal is a feature, not a problem.
Force Delete Only When You Mean It
If you truly want to delete all local branches regardless of merge status, use force deletion deliberately.
-D is equivalent to forcing deletion. It is appropriate only when you have confirmed the branches are disposable or recoverable elsewhere.
Understand What "All Local Branches" Usually Means
In practice, people rarely want to delete literally every local branch. They usually want one of these outcomes instead:
- remove all merged feature branches,
- remove all local branches except the current one,
- remove all local branches except a protected base set such as
mainanddevelop.
That distinction matters because the command should match the intent. A one-liner that is technically correct but deletes your only copy of unfinished work is still the wrong solution.
Check Branch State Before Large Cleanup
A good cleanup routine is:
- list local branches,
- inspect which are merged,
- preserve important base branches,
- delete merged branches safely,
- force delete only the branches you are sure you do not need.
You can always inspect branch recency first:
That often makes stale branches obvious before you delete anything.
Remote Branches Are a Separate Cleanup Step
Deleting local branches does not remove remote branches such as origin/feature-x. If the cleanup goal includes the remote repository too, that is a different operation and should be handled separately with an explicit push deletion command. Keeping those two steps separate is safer because it prevents local spring cleaning from accidentally becoming team-wide history cleanup.
Common Pitfalls
- Trying to delete the currently checked-out branch.
- Using
-Dwhen-dwould have protected unmerged work. - Forgetting to exclude
main,master, or other base branches you want to keep. - Running a copied one-liner without first listing the branches it will target.
- Assuming local deletion affects remote branches automatically, which it does not.
Summary
- Switch to a branch you want to keep before deleting others.
- Use
git branch -dfor merged branches and-Donly for deliberate force deletion. - Review the target branch list before running bulk delete commands.
- Most cleanup tasks should preserve one or more base branches.
- The safest answer is usually "delete all merged local branches," not "delete everything blindly."

