sweeping-merged-worktrees
Skill2 starsChanged 4 months agoMITView on GitHub
Use when a repo has accumulated worktrees under .worktrees/ from finished PRs, when asked to clean up or prune worktrees whose branches were merged or deleted on the remote, or when `git worktree list` shows stale/prunable entries
In this repository33 other files it gives its agents.
- AGENTS.mdAGENTS.md
- covidence-full-text-reviewSkill
- covidence-full-text-retrievalSkill
- covidence-screeningSkill
- ilearning-autoadvanceSkill
- oh-my-piSkill
- auditing-worktreesSkill
- hermes-deepseek-cacheSkill
- i-have-adhdSkill
- memory-uiSkill
- oh-my-ghosttySkill
- pbcopy-word-deliverySkill
- session-handoffSkill
- summarizing-weekly-workSkill
- tldrSkill
- writing-reportsSkill
- sci-hub-accessSkill
- slack-scanSkill
- superpowers-brainstormingSkill
- superpowers-dispatching-parallel-agentsSkill
- superpowers-executing-plansSkill
- superpowers-finishing-a-development-branchSkill
- superpowers-receiving-code-reviewSkill
- superpowers-requesting-code-reviewSkill
- superpowers-slash-commandsSkill
- superpowers-subagent-driven-developmentSkill
- superpowers-systematic-debuggingSkill
- superpowers-test-driven-developmentSkill
- superpowers-using-git-worktreesSkill
- superpowers-using-superpowersSkill
- superpowers-verification-before-completionSkill
- superpowers-writing-plansSkill
- superpowers-writing-skillsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 43
Remove worktrees before deleting branches.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Sweeping Merged Worktrees
Overview
Survey, salvage, and remove worktrees whose branches are merged or gone from the remote.
Core principle: git worktree remove does not touch the branch ref, so committed work always survives. The only unrecoverable loss is uncommitted work. Salvage it, then remove — do not leave a merged worktree standing just because it holds a stray untracked file.
Usage
~/.agents/skills/sweeping-merged-worktrees/sweep.py <repo>... # dry run, prints verdicts
~/.agents/skills/sweeping-merged-worktrees/sweep.py <repo>... --apply # salvage + prune + removeDry run first. Read the table, then --apply. Multi-repo in one shot: sweep.py storybook nextportal e2e.
Baseline is origin/<branch the main checkout sits on> — no config needed.
Verdicts
| Verdict | Meaning | Action taken by --apply |
merged | Ancestor of the baseline | Salvage untracked, remove worktree |
gone | Upstream deleted (squash-merged PR, or abandoned) | Salvage untracked, remove worktree |
prunable | Registration points at a missing directory | git worktree prune |
blocked | Merged/gone but has uncommitted edits to tracked files | Nothing — reported for you to review |
keep | Neither merged nor gone | Nothing |
Non-ignored untracked files are copied into the main checkout at the same relative path before removal (.salvaged suffix on content conflict). Ignored files — build output, node_modules — are regenerable and left to die.
Order Matters
Remove worktrees before deleting branches. git branch -d/-D refuses a branch that is checked out in any worktree, so a branch-first cleanup silently skips exactly the branches you meant to remove.
Branch deletion is a separate step with its own precondition. A git cleanup-style alias (fetch -p, branch -D every [gone], branch -d every merged) runs unconditionally, so clear both footguns below before invoking one. sweep.py prints an [AT RISK] line for every [gone] branch whose patches are absent from the baseline — if any appear, that alias will orphan those commits.
Footguns
Stale local baseline. git branch --merged compares against the current local HEAD. If local dev is behind origin/dev, merged branches report as unmerged and cleanup quietly under-deletes. Always compare against origin/<base> — the tool does. Fast-forward the baseline before running a --merged-based alias.
Aliases that delete merged branches eat main. git branch --merged | xargs git branch -d deletes any merged branch, including a local main that is an ancestor of dev. Check before running one:
git merge-base --is-ancestor main dev && echo "WARNING: alias would delete local main"[gone] is not proof of merge. A deleted upstream usually means a squash-merged PR, but it also covers abandoned branches whose commits exist nowhere else. Removing the worktree is always safe (the ref survives); branch -D is what destroys work. Verified on the fixture: a blanket -D over [gone] branches left an abandoned commit reachable from nothing. git cherry <base> <branch> is the check — - means the patch is already upstream (safe, catches squash-merges), + means it only exists here.
Verifying Changes
sweep.py --self-testBuilds a throwaway fixture covering every case — real merge, squash-merge, gone upstream, stale registration, untracked file, tracked modification, stale local baseline — and asserts the end state.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

