Over time, local Git branches pile up — especially if you’re working on a project with many feature branches. Here’s a one-liner to clean up all the branches you’ve already merged and closed on your origin server:

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

How it works

  1. git branch -r lists all remote branches
  2. The awk and egrep pipeline compares them against your local branches that track an origin
  3. Any local branch that no longer has a corresponding remote gets piped to git branch -d for deletion

When branches aren’t fully merged

Some branches may not be fully merged and will resist the -d flag. In that case, you can force-delete them individually:

git branch -D branch_name

Use this with caution — -D will delete the branch regardless of merge status.

Hope that it helps you!