← ClaudeAtlas

safe-bulk-worktree-branch-cleanuplisted

Safely bulk-clean accumulated git worktrees and branches without losing progress. Use when: (1) user says the repo is "messy with worktrees", "too many branches", "clean up the repo", "review the worktrees"; (2) `git worktree list` / `git branch -a` shows dozens of stale entries; (3) you must delete many branches/worktrees but the user said "without losing progress" / "keep <X>". Centres on the non-obvious trap that `git branch --merged` and `git merge-base --is-ancestor` report false "NOT merged" for squash-merged branches — so safety must be gated on PR state, not ancestry. Covers the verify-before-delete gate, salvaging untracked files before `git worktree remove`, and a SHA recovery manifest.
wan-huiyan/agent-traffic-control · ★ 2 · AI & Automation · score 79
Install: claude install-skill wan-huiyan/agent-traffic-control
# Safe Bulk Worktree & Branch Cleanup ## Problem A repo accumulates dozens of git worktrees and branches over many sessions. The user wants them cleaned up "without losing progress." Bulk-deleting is risky in two non-obvious ways: 1. **Ancestry tests lie about squash-merged branches.** `git branch --merged` and `git merge-base --is-ancestor` only detect merge-commit / fast-forward merges. A squash merge creates a *new* commit on the trunk — the original branch tip is never an ancestor. In a squash-merge repo (PR titles ending `(#NN)`), these tools flag *almost every* merged branch as "NOT merged." Trusting them either blocks the cleanup or, worse, makes you think real work would be lost. 2. **`git worktree remove` silently discards untracked files.** Removing a worktree never deletes commits (the branch + history persist), but any *uncommitted / untracked* files in that worktree's directory are gone. ## Context / Trigger Conditions - User: "this repo is messy with all the worktrees", "clean up the branches", "review the rest and help me clean up without losing progress". - `git worktree list` shows many `.claude/worktrees/*` or `/tmp/*` entries. - `git branch -a` shows dozens of stale `feature/*`, `claude/*`, `docs/*` branches. - A bulk branch/worktree deletion where the user named something to keep. ## Solution ### 1. Inventory (read-only) ```bash git worktree list # prunable = directory gone git branch -a --sort