Loading...
Loading...
Clean up local branches after PR merges. Syncs main with origin, identifies branches with merged PRs, and proposes safe deletion. Use when the user asks to 'clean up branches', 'delete merged branches', 'sync branches', or mentions branch cleanup.
npx skill4agent add fxstein/ai-todo branch-cleanupgit fetch origingit branch --show-currentgit checkout maingit pull origin main --ff-onlygit branch --format='%(refname:short)'mainmasterdevelopstagingproductionarchive*gh pr list --head {branch} --state merged --json number,title,urlgit branch -d {branch}# Fetch latest remote refs
git fetch origin
# Check current branch
CURRENT_BRANCH=$(git branch --show-current){CURRENT_BRANCH}git status --porcelain# Switch to main
git checkout main
# Fast-forward merge only (no conflicts)
git pull origin main --ff-only# Get merged PR for branch
gh pr list --head {branch} --state merged --json number,title,url --limit 1| Status | Action |
|---|---|
| Merged PR found | Add to cleanup list |
| Open PR found | Skip (not merged) |
| No PR found | Skip (might be local work) |
| Protected branch | Skip (main, master, etc.) |
mainmasterdevelopstagingproductionarchivearchive-2024archive/oldorigin/*## Branches with merged PRs
| Branch | PR | Title |
|--------|-----|-------|
| fxstein/AIT-12-feature | [#45](url) | Feature implementation |
| oliver/ait-8-bugfix | [#38](url) | Fix login bug |
Total: 2 branches# Safe delete (only if fully merged)
git branch -d {branch}git branch -d-D✅ All local branches are up to date.
No merged branches found for cleanup.git branch -dgit log main..{branch}archivearchive-2024archive/backuparchive-old-featuresfxstein/AIT-12-featureorigin/fxstein/AIT-12-featuregit push origin --delete {branch}| Error | Cause | Action |
|---|---|---|
| "Not on main" | Current branch not main | Ask to switch |
| "Fast-forward failed" | Main diverged from origin | Stop, manual resolution |
| "gh command not found" | GitHub CLI not installed | Stop, ask user to install |
| "Branch not fully merged" | Branch has unmerged commits | Skip branch |
-D--ff-only🔄 Syncing main with origin...
✅ Main is up to date
📋 Found 3 branches with merged PRs:
- fxstein/AIT-12-feature (PR #45)
- oliver/ait-8-bugfix (PR #38)
- fxstein/AIT-10-docs (PR #42)
🗑️ Deleting branches...
✅ Deleted fxstein/AIT-12-feature
✅ Deleted oliver/ait-8-bugfix
✅ Deleted fxstein/AIT-10-docs
✨ Cleanup complete: 3 branches deleted🗑️ Deleting branches...
✅ Deleted fxstein/AIT-12-feature
⚠️ Skipped fxstein/AIT-15-wip: not fully merged
✅ Deleted oliver/ait-8-bugfix
✨ Cleanup complete: 2 deleted, 1 skippedgit push origin --delete {branch}git branch -Dmainmaster