tearing-down-a-merged-worktree
Skill0 starsChanged 2 months agoMITView on GitHub
Use after a PR has merged and its worktree should be cleaned up (checkout, branch, Herdr workspace, setup log, project data). Triggers on tear down the worktree, clean up the worktree, remove the worktree, teardown after merge, and on an auto-merge poll seeing MERGED. Works in any git repository.
In this repository10 other files it gives its agents.
- create-pull-requestSkill
- deep-researchSkill
- herdrSkill
- pr-review-loopSkill
- readme-writerSkill
- remixSkill
- sandi-metz-rulesSkill
- skill-creatorSkill
- test-auditSkill
- worktree-spaceSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are 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.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 21
If the repo has its own tearing-down-a-merged-worktree skill under .claude/skills/, follow that one instead.
- Very specific: line 34
If the tree is dirty the script stops and shows the changes.
- Very specific: line 41
If the report ends with a session: line, call ExitWorktree with action: "keep" now.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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
Tearing down a merged worktree
git worktree remove cleans up only the checkout. The branch, the Herdr
workspace holding the pane, the setup log, and any per-worktree data
(databases, indexes) outlive it. The bundled script handles all of it except
moving the Claude session, which only ExitWorktree can do.
Script: ~/.claude/skills/tearing-down-a-merged-worktree/scripts/worktree-teardown.sh
(header comment documents the flags).
Runs when the user asks after a merge, or automatically once auto-merge
polling sees MERGED. In the automatic case do not ask, but report.
Steps
- If the repo has its own
tearing-down-a-merged-worktreeskill under.claude/skills/, follow that one instead. It knows the project's data. Run:
~/.claude/skills/tearing-down-a-merged-worktree/scripts/worktree-teardown.sh <slug><slug>is the dir name under.claude/worktrees/, or any registered worktree path. The script refuses unless the branch's PR isMERGED(--pr <n>to name it,--no-pronly when the user confirms there is no PR).CLOSEDis notMERGED: closed-unmerged work exists only in that checkout. Never bypass this for an open PR.- If the tree is dirty the script stops and shows the changes. Inspect with
the user before re-running with
--force. Unmerged work is the one thing here that cannot be recovered. - The script moves the pane to the repo's home workspace first, creating one
if none exists, then removes checkout and branch, runs the project's
.claude/worktree-teardown.sh <slug> <branch>hook if present, and deletes the setup log. - If the report ends with a
session:line, callExitWorktreewithaction: "keep"now. The session is sitting in a deleted directory. - Relay the report verbatim: branch, worktree, herdr, data hook, log. A teardown that quietly skipped a step reads exactly like one that worked.
Project hook
<repo>/.claude/worktree-teardown.sh <slug> <branch> (executable) runs from
the main checkout after the git removal. Put database drops, index deletes and
container cleanup there, and make it verify by counting what the server
reports, not by exit code: a drop that matched nothing still exits 0.
Notes
branch -D, not-d: a squash merge leaves the branch looking unmerged.- Pane and session are two movements.
herdr pane moveleaves the session's cwd behind;ExitWorktreeleaves the pane behind. The script does the first, you do the second. - The worktree's workspace closes itself when its last pane leaves. The script closes it explicitly if extra tabs kept it alive.
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.

