worktree-cleanup
Skill1 starsChanged 3 months agoApache-2.0View on GitHub
Tear down finished git worktrees safely - verify each branch is merged and its tree is clean, then remove the worktree, delete the branch, and prune. Sweeps every merged-and-clean worktree at once, or targets one branch. Use when the user wants to clean up, tear down, close out, or remove a worktree or workstream, says "worktree cleanup", "sweep merged worktrees", "remove finished worktrees", or asks to delete a finished branch's directory. Delegates to the repo's own scripts/rm-worktree.sh when one exists, and otherwise runs the equivalent gated steps directly.
- Reads credentials
- Deletes or force-pushes
10 other projects use a version of this.
Adopted by
Most recently taken up by
- anotherben/claude-harnesstheir own version, first seen Nov 8, 2026
- perber/leafwikitheir own version, first seen Oct 17, 2026
- my-chiefmind/ai-maestrotheir own version, first seen Oct 13, 2026
- TentacleOpera/switchboardtheir own version, first seen Oct 8, 2026
- dstotijn/agent-skillstheir own version, first seen Sep 21, 2026
- syanga/astacktheir own version, first seen Sep 20, 2026
Other versions10 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 10
- perber/leafwiki★ 1.2ksame name
- TentacleOpera/switchboard★ 215same name
- anotherben/claude-harness★ 7same name
- my-chiefmind/ai-maestro★ 6same name
- dstotijn/agent-skills★ 1same name
- Jador/skills★ 1same name
- luw2007/skills★ 1same name
- EdenGibson/claude-skills★ 0same name
- Enverge-Labs/skills★ 0same name
- syanga/astack★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository24 other files it gives its agents.
- audit-fixSkill
- auditSkill
- commitSkill
- deps-auditSkill
- digestSkill
- flowchartSkill
- git-undoSkill
- handoffSkill
- hone-skillSkill
- kickoffSkill
- math-researchSkill
- new-skillSkill
- next-stepsSkill
- prompt-engineerSkill
- prune-test-suiteSkill
- release-notesSkill
- repo-adoptSkill
- repo-issue-scoutSkill
- resolve-tester-bugsSkill
- scaffold-auditSkill
- session-reportSkill
- sidebarSkill
- sync-profile-readmeSkill
- rename-to-match-folderSkill
The file
We split this skill into the rules it gives an agent
3 rules. 0 are common, 2 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 10 edited versions we read keep these.
2
Distinctive
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 25
Resolve targets: If $ARGUMENTS names a branch, that is the only candidate.
- Distinctive: line 51
Never remove anything before the user confirms.
- Distinctive: line 75
Never tear down the main checkout, and never delete a branch that is not merged — flag unmerged or dirty workstreams for the user instead.
“Common” is measured: kept by at least 6 in 10 of the 10 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
worktree-cleanup
Close out finished workstreams: remove the worktree directory, delete the local branch, and prune worktree metadata — only for branches that are merged and trees that are clean. Decide what to tear down, confirm with the user, then do it.
Instructions
- Orient. From the repo's main checkout, determine the default branch
(
git symbolic-ref refs/remotes/origin/HEAD, falling back to whichever ofmain/masterexists — the symref is often unset on a fresh clone even when a remote is configured) and whether the repo provides a teardown script atscripts/rm-worktree.sh. If it does, that script owns the safety gates and every removal goes through it; if not, run the equivalent steps yourself in step 4. - Resolve targets. If
$ARGUMENTSnames a branch, that is the only candidate. Otherwise (or on "sweep"), walkgit worktree list --porcelainand skip entries with nobranchline (detached HEAD or bare) plus the main checkout itself. For each remaining worktree, check both:- Merged —
git merge-base --is-ancestor refs/heads/{branch} refs/heads/{default-branch}(fully-qualified: a bare name lets a same-named tag win the lookup), falling back togh pr list --state merged --head {branch}to catch squash-merges, which the ancestor check cannot see. For the manual path in step 4 this check is the sole authority on "merged" — the delete acts on its verdict and nothing later re-asks. (The script path re-checks via the script's own gate, which is why gh-verified squash-merges need--forcethere.) - Clean —
git -C {dir} status --porcelainsucceeds AND prints nothing. Capture the output and the exit code separately: a status command that FAILS also prints nothing, and reading that as "clean" skips the gate. A failing status is a refusal, not a pass.
- Merged —
- Present the plan and get confirmation. List candidates with branch,
directory, and merge evidence (ancestor vs merged PR number); then list
every worktree SKIPPED with the reason — unmerged, dirty, or it is the
worktree this session is running in. Git refuses to remove the directory
you are standing in; hand that one to a session in another checkout or
run from the main checkout. State plainly that removal destroys anything
untracked-and-ignored inside the directory (virtualenvs, copied
.envfiles, build output). Symlinks are removed but their targets are not. Never remove anything before the user confirms. - Tear down each confirmed branch from the main checkout:
- With a teardown script:
scripts/rm-worktree.sh {branch}. Add--forceonly for a squash-merge you verified via PR state in step 2, and--delete-remoteonly if the user asked — GitHub's "delete branch after merge" setting usually handled it already. - Without one:
git worktree remove {dir}, thengit branch -D {branch}, thengit worktree prune.-D, not-d: the removal is irreversible by the time the branch delete runs, so nothing after it may veto — the merged check in step 2 is the sole authority, and it already accepted both ancestor-merged and PR-verified squash-merged branches.-dasks a DIFFERENT question (reachable from HEAD or upstream) and, when the two disagree — which a squash-merge admitted viagh pr listtypically produces, always once the remote branch is deleted and pruned — it refuses only after the worktree is already gone, leaving the branch orphaned and prune never run. Run one branch at a time and stop on the first failure rather than escalating to--force.
- With a teardown script:
- Report. What was removed, what remains (
git worktree list), and every candidate skipped or refused by a gate.
Notes
- Never tear down the main checkout, and never delete a branch that is not merged — flag unmerged or dirty workstreams for the user instead.
- A failing gate is information, not an obstacle. Report it; do not work
around it with
--forceorrm -rfunless the user explicitly asks after seeing the reason. (-Dafter the step-2 gates have passed is the standard delete, not an escalation;-Dto override a gate that REFUSED is the thing that needs an explicit user ask.)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
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.

