ot-worktree
Skill2 starsChanged 2 months agoApache-2.0View on GitHub
Create, heal and destroy git worktrees in any repo — the portable worktree machinery behind `worktree:new`, `worktree:heal` and `worktree:remove`. Use when creating a worktree, provisioning or self-healing a worktree, removing/deleting/tearing down a worktree, or when OPENTHROTTLE_WORKTREE_ROOT, `.worktree/provision.sh` or `.worktree/teardown.sh` are involved.
- Reads credentials
- Commits and pushes
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- applications/AGENTS.mdAGENTS.md
- databases/AGENTS.mdAGENTS.md
- infra/AGENTS.mdAGENTS.md
- packages/AGENTS.mdAGENTS.md
- skills/AGENTS.mdAGENTS.md
- tools/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/nx-rules.mdcCursor rule
- frontend-designSkill
- grillingSkill
- improveSkill
- link-workspace-packagesSkill
- monitor-ciSkill
- nx-workspaceSkill
- agents-ralphSkill
- github-commitSkill
- github-pull-requestSkill
- github-squashSkill
- ot-foldersSkill
- ot-generatorsSkill
- ot-loopSkill
- ot-onboardingSkill
- ot-plansSkill
- ot-postgresSkill
- ot-skill-syncSkill
- ot-stackSkill
The file
Files
ot-worktree
Git worktrees are how you run several branches of one repo side by side. Creating one is
git worktree add; making it usable — dependencies, .env, ports, containers — is repo-specific
work git has no hook for. This skill owns the generic half and delegates the repo-specific half.
Because openthrottle skills are synced into any repo (skills/ → .agents/skills/ → .claude/skills/
via ot-skill-sync), every repo that has this skill creates, heals and
destroys worktrees the same way.
Three composable actions
Each verb is usable standalone and callable from the others.
| Action | Script | Entrypoints | Job |
| create | scripts/create.sh | pnpm worktree:new <name>, Claude WorktreeCreate hook, Cursor | Create the worktree, then provision it |
| heal | scripts/heal.sh | pnpm worktree:heal, nx monorepo:ensure-worktree | Lazily provision a worktree created outside the skill |
| destroy | scripts/destroy.sh | pnpm worktree:remove [name|path] | Run teardown, remove the worktree, prune admin dirs |
Shared internals: scripts/common.sh (logging, linked-worktree detection, name sanitization, target
validation), scripts/root.sh (the root ladder), scripts/provision.sh (repo-hook delegation).
create — three invocation modes, auto-detected
- CLI — name as
$1(pnpm worktree:new <name>). Never reads stdin. - Hook payload — no name arg and stdin is a pipe: the Claude Code
WorktreeCreateJSON payload. The name comes from.name // .worktree_name // .worktreeName // .worktree // .branch // .slug, falling back towt-<first 8 of session_id>. The raw payload is logged to$TMPDIR. - Provision in place — no name, no payload, run from inside a linked worktree (Cursor creates
the worktree itself, then runs a setup command with
cwd=$WORKTREE_PATH). Provisions the current worktree; creates nothing.
Branch name is ${OPENTHROTTLE_WORKTREE_BRANCH_PREFIX:-openthrottle/}<name>; an existing branch is checked out
rather than failing.
What the new branch forks from
A worktree you make to test something should not quietly inherit whatever you were mid-way through on your primary checkout. So the base is resolved on its own ladder, highest rung first:
--base <ref>orOPENTHROTTLE_WORKTREE_BASE— explicit.--base HEADopts back into forking from the primary checkout's current branch, which is what you want when stacking on your own work. A ref that does not resolve is a hard failure, never a silent fallback.- The remote's default branch —
refs/remotes/<remote>/HEAD(remote fromOPENTHROTTLE_WORKTREE_REMOTE, defaultorigin, matching the org segment inroot.sh). Clones made before the remote had commits have no such symref, so<remote>/mainthen<remote>/masterare tried as a purely local fallback. HEAD— no remote, or no nameable default. The old behaviour, now only a last resort.
No fetch happens. Rung 2 is your last-fetched ref, so create stays fast, works offline, and can
never fail on a network hiccup — the Claude WorktreeCreate hook blocks on this script. git pull
inside the worktree if you need newer than your last fetch.
The branch is created with --no-track: cut from origin/main, it would otherwise take main as
its upstream and aim a later git push at the wrong branch. --base is ignored, with a warning,
when the branch already exists or when provisioning in place — in both cases there is nothing to fork.
Stdout contract: create.sh prints only the worktree's absolute path. Everything else is stderr.
Claude's WorktreeCreate hook depends on this — any extra stdout line breaks worktree creation.
Where worktrees live
Two parts: a root holding every repo's worktrees, and the <org>/<repo> OpenThrottle always
organizes beneath it.
~/.openthrottle/worktrees/acme/monorepo/feature-x
└──────── root ─────────┘└─ org/repo ─┘└─ worktree ─┘The root, highest rung wins:
OPENTHROTTLE_WORKTREE_ROOTin the environment.OPENTHROTTLE_WORKTREE_ROOTin the target repo's.env— how a repo customizes where its worktrees go. The same variable, read from the repo instead of the shell.- Default:
~/.openthrottle/worktrees, documented in.env.default.
One variable, two places it can come from, one default. There is deliberately no settings file and no third channel: a second source of truth for the same answer is what lets a CLI and a server disagree about where they put things.
The root is a root, not a final destination. OT appends <org>/<repo> whatever supplied it —
configured or default — the same shape OPENTHROTTLE_CHECKOUT_ROOT uses for clones. Organizing
unconditionally is what keeps the layout predictable; a root used verbatim would pile every repo's
worktrees into one directory.
The org comes from the repo's git remote, not its directory name, so two checkouts of different
orgs' monorepo cannot collide. By the time this runs git is unambiguously present — every action
here shells out to it. A repo with no remote falls back to just <repo>, its directory name. Remote
segments are sanitized to [A-Za-z0-9._-], and ./.. are refused, so a malformed remote cannot
walk out of the root.
A leading ~/~/ is expanded, trailing slashes stripped, and a non-absolute root is rejected
rather than guessed at.
Worktrees created before this changed stay where they are — they are registered in
git worktree list, so every action here still finds them by path.
The repo hook contract
The skill never knows how to set your repo up. It looks for a provisioner, and runs it if found:
$OPENTHROTTLE_WORKTREE_PROVISION(absolute, or relative to the worktree)<worktree>/.worktree/provision.sh<worktree>/scripts/setup_worktree.sh
Teardown mirrors it: $OPENTHROTTLE_WORKTREE_TEARDOWN, then <worktree>/.worktree/teardown.sh (no scripts/
fallback). Nothing found → log and no-op; a plain git worktree add/remove is a perfectly valid
outcome in a repo with no setup needs.
Hooks run with cwd = the worktree, stdin closed, stdout redirected to stderr, and
OPENTHROTTLE_SOURCE_REPO / OPENTHROTTLE_WORKTREE_NAME / OPENTHROTTLE_WORKTREE_PATH exported. A non-zero exit is a failure:
provisioning aborts creation, teardown aborts removal (that is where a repo stops a container or
releases a port lease — removing anyway would leak it).
Full details, including the provisioned-marker scheme heal reads: references/contract.md.
destroy — safety defaults
Destroying a worktree is the one action here that can eat real work, so the defaults are conservative:
- Refuses the primary checkout. The target must be a registered linked worktree of this repo.
- Refuses a dirty tree — prints what is dirty and requires an explicit
--force. - Branch-preserving.
--delete-branchdeletes it only if merged;--delete-branch --forceotherwise. --dry-runprints exactly what would happen and removes nothing.- Runs the teardown hook before removal, then
git worktree prune.
With no argument, run from inside a linked worktree, it removes itself (chdir'ing to the primary checkout first).
Adopting this in another repo
- Sync the skills (
./skills/ot-skill-sync/scripts/sync.sh, or/ot-skill-sync). - Optionally add
.worktree/provision.sh(and.worktree/teardown.sh) — anything the repo needs to make a fresh checkout usable. Skip it entirely ifgit worktree addis already enough. - Optionally add the human entrypoints to
package.json. Copy these verbatim — they resolve the skill wherever it lives, so there is nothing to adjust per repo:
Why a ladder and not a path. The skill lives at"worktree:new": "sh -c 'd=skills/ot-worktree/scripts; [ -x \"$d/create.sh\" ] || d=.agents/skills/ot-worktree/scripts; exec \"$d/create.sh\" \"$@\"' --", "worktree:heal": "sh -c 'd=skills/ot-worktree/scripts; [ -x \"$d/heal.sh\" ] || d=.agents/skills/ot-worktree/scripts; exec \"$d/heal.sh\" \"$@\"' --", "worktree:remove": "sh -c 'd=skills/ot-worktree/scripts; [ -x \"$d/destroy.sh\" ] || d=.agents/skills/ot-worktree/scripts; exec \"$d/destroy.sh\" \"$@\"' --"skills/in the repo that authors it (OpenThrottle) and at.agents/skills/in a repo that installed it withnpx skills. Neither path is right everywhere, and in OpenThrottle.agents/skills/*is gitignored — the authored entries there are symlinks that a fresh clone does not have — so the installed-repo path is not a safe universal default. The ladder prefers the authoring path, falls back to the installed one, andexecs, so no extra process sits between the caller and the script's stdout. That matters: theWorktreeCreatehook readscreate.sh's stdout as the worktree path. - Optionally wire the Claude
WorktreeCreatehook in.claude/settings.jsonand Cursor's.cursor/worktrees.jsonprovision-in-place command at the same scripts, using the same ladder.
OpenThrottle itself needs no .worktree/provision.sh: its existing scripts/setup_worktree.sh is
discovered by rung 3. It does ship .worktree/teardown.sh (teardown has no scripts/ rung), which
stops the worktree's own docker compose project before removal.
Note the hook is read from the worktree's checkout, not the primary one — so a worktree on a branch predating the hook will not run it.
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.

