Delete All Local Untracked Branches from Git
A quick one-liner to clean up all merged local branches that no longer exist on your remote Git server.
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
git branch -rlists all remote branches- The
awkandegreppipeline compares them against your local branches that track an origin - Any local branch that no longer has a corresponding remote gets piped to
git branch -dfor 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!