Git
Branch Management
Version Control
Git Commands
Programming Tips

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.

bash
git switch main

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.

bash
git branch --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.

bash
git branch --merged   | grep -v '^\*'   | grep -vE '^(  )?(main|master|develop)$'   | xargs -r git branch -d

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.

bash
git for-each-ref --format='%(refname:short)' refs/heads   | grep -v '^main$'   | xargs -r git branch -D

-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 main and develop.

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:

  1. list local branches,
  2. inspect which are merged,
  3. preserve important base branches,
  4. delete merged branches safely,
  5. force delete only the branches you are sure you do not need.

You can always inspect branch recency first:

bash
git branch --sort=-committerdate

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 -D when -d would 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 -d for merged branches and -D only 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."

Course illustration
Course illustration

All Rights Reserved.