Cleanup to Main
Cleans up local git branches that have been deleted from the remote repository, with safety checks and detailed reporting.
Workflow
- Pre-flight safety checks → Abort if uncommitted changes or branch not synced
- Switch to default branch → Detect and checkout
mainormaster - Fetch and prune → Update remote references with
git fetch -p - Identify deletion candidates → Local branches whose remote no longer exists
- Safe delete → Use
git branch -dfor each candidate - Generate report → Summarize deleted, skipped, and kept branches
Execution
Run the cleanup script directly:
./scripts/cleanup_branches.sh
Or execute step-by-step using git commands following the workflow above.
Safety Guarantees
- Aborts on uncommitted changes - Working directory must be clean
- Aborts on unsynced branch - Current branch must match origin
- Preserves local-only branches - Branches never pushed are kept
- Safe delete only - Uses
git branch -d(fails if not merged) - Never deletes main/master - Protected branches always kept
Branch Categories
| Category | Action | Reason |
|---|---|---|
| Deleted from remote | Delete | Tracking branch gone, safe to remove |
| Not fully merged | Skip | May contain unmerged work |
| Local-only (never pushed) | Skip | User's local work in progress |
| main/master | Keep | Protected default branches |
Exit Codes
0- Success (branches cleaned or nothing to clean)1- Safety abort (uncommitted changes or unsynced branch)2- Partial failure (some branches couldn't be deleted)