Git & Version Controlbeginner
Find and delete merged/stale local and remote branches
Branch Cleanup
Find and delete merged/stale local and remote branches
Find and safely delete merged and stale git branches.
Step-by-Step
- List all local branches with tracking info:
git branch -vv
- Find local branches already merged into main:
git branch --merged main | grep -v "\* \|main\|master\|develop"
- Find stale remote branches (no commits in 30+ days):
git for-each-ref --sort=committerdate refs/remotes/ \
--format='%(committerdate:short) %(refname:short)' | head -30
-
Show the list to the user and ask for confirmation before deleting.
-
Delete confirmed branches:
git branch -d branch-name # local (safe — refuses unmerged)
git push origin --delete branch-name # remote
git remote prune origin # clean stale tracking refs
Protected Branches (never delete)
main, master, develop, staging, production, release/*
Safety
- Always use
-d(not-D) — it refuses to delete unmerged branches - Show
git log main..branch-name --onelinefor any branch the user is unsure about