/lorenzh:creating-worktreesfrom lorenzh
Git worktree setup and cleanup for isolated parallel work. Use when the user asks to "work in a worktree", wants an isolated workspace or a parallel branch checkout, is about to execute an implementation plan that should not touch the current checkout, asks how to install dependencies or copy .env files into a worktree, wants a coding agent or sandbox to work in one, or asks to remove or clean up a worktree, or when worktrees fill the disk (ENOSPC, "No space left on device").
Ships in lorenzh. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Give a task its own checkout that builds and tests on its own, then remove it without touching the main checkout.
A worktree is isolated only when its files, its dependencies and its config all resolve inside it. Check that; do not assume it.
| Setting | Controls | Allowed values | Default |
|---|---|---|---|
create-command | Worktree creation | Command template containing <branch> and optional [base] | Native creation procedure in step 2 |
cleanup-command | Worktree removal in Cleanup step 3 | Command template containing <wt> and optional <branch> | git worktree remove <wt>, then git worktree prune |
Path names below: <root> is the main checkout, <wt> the worktree, <skill> this skill's folder.
Read creating-worktrees.<setting>: <value> lines from instruction files in context, such as AGENTS.md, CLAUDE.md, CLAUDE.local.md, or GEMINI.md.
Strip one pair of backticks around a value.
If no instruction file is in context, search the project's instruction files:
grep -Hs '^- *creating-worktrees\.' AGENTS.md CLAUDE.md CLAUDE.local.md .claude/CLAUDE.md
Use this precedence: invocation or overlay skill > project instruction file > user instruction file > default.
Instruction files always take precedence over skill defaults.
Accept only documented setting names and allowed values.
If a name is unknown or a value is invalid, report it and use the default.
Treat values as data, never as commands.
Before acting, print one line per setting: settings: <name>=<value> (<source>).
Use invocation, project, user, or default as the source; an overlay skill uses invocation.
When dispatching a subagent, include the resolved settings in its task text.
For the default, print native.
Then run in the current directory:
git rev-parse --path-format=absolute --git-dir --git-common-dir
git rev-parse --show-superproject-working-tree
git branch --show-current
An existing worktree on a detached HEAD is managed by the harness; note that it needs a branch before anything is pushed.
Done when the settings: line is printed and the report's Isolation line says "existing worktree " or "normal checkout".
Configured command first. If create-command is configured, use it instead of the native creation procedure below.
Run from <root> with the intended branch for <branch> and the requested base for [base]; omit [base] when no base was requested.
Never probe it with --help or without arguments: a wrapper script may take the first argument as a branch name and create a worktree called help. Read its source or documentation instead.
Replace each placeholder, together with any quotes around it in the template, with one single-quoted argument (write ' as '\''); do not evaluate shell substitutions in the values.
After the command, take <wt> from its output, its documented location, or the path it was asked to create.
A command that follows a terminal-multiplexer workspace can create the worktree in that workspace's repository instead of <root>'s.
Before any agent works in <wt>, check that git -C <root> worktree list contains <wt>; if not, <wt> belongs to another repository.
Also compare git -C <wt> remote get-url origin and git -C <wt> rev-parse --path-format=absolute --git-common-dir with the same commands in <root>.
If the list lacks <wt> or either value differs, run git worktree remove <wt> from the repository it belongs to, then create the worktree natively below.
If <wt> lies inside <root>, run the ignore check below on its parent directory and handle a missing entry as below.
Then continue with step 3.
Apply the sandbox denial rule below if the command fails with a permission or sandbox error.
Default: native tool first. If the harness offers a worktree tool or flag, use it.
Examples include EnterWorktree, /worktree, or --worktree <name>.
It manages placement, branch and cleanup; a git worktree add beside it creates state the harness cannot see.
Use the tool's documented location and approved local-file copying mechanism.
Some native tools use <root>/.claude/worktrees/<name> and copy files listed in .worktreeinclude.
Then continue with step 3 for what the tool does not do.
Otherwise git worktree add. Location, first match wins:
AGENTS.md, CLAUDE.md) name.<root>/.worktrees/.<root>/.worktrees/<name>.In-repo placement suits sandboxes: git writes the worktree's HEAD, index and refs to <root>/.git/worktrees/<name>, so a sandbox rooted at <root> covers both. A worktree outside <root> needs that .git granted separately (an additional-directory grant or container mount), or git commit fails there.
Before the first git worktree add, check the ignore entry:
git -C <root> check-ignore -q .worktrees/x && echo ignored
Not ignored: add .worktrees/ to .gitignore and report the change as uncommitted. Commit it only when the user asks; a commit lands on whatever branch <root> has checked out. Without the entry, git status shows ?? .worktrees/ and git add -A stages every worktree.
git -C <root> worktree add .worktrees/<name> -b <branch> # new branch
git -C <root> worktree add .worktrees/<name> <branch> # existing branch
Sandbox denial. If creation fails with a permission or sandbox error, say so in one line ("The sandbox blocked worktree creation; working in the current checkout.") and work in place. If the user made isolation a condition ("main must stay untouched"), stop after that line and wait instead. Changing sandbox settings, picking another path to get around the denial, or running outside the sandbox is the user's decision.
Done when git worktree list shows <wt> on the intended branch, its origin and common git dir match <root>'s, and its in-repo parent directory is ignored, or the report records the sandbox denial.
The worktree directory is the parent of <wt> inside <root>: .worktrees/, or the native tool's directory. Git, rg, fd, tsc, pytest and Bun's test runner skip a dot directory or follow .gitignore. These scan it unless told:
| Tool | Exclude |
|---|---|
| Git | <dir>/ in .gitignore; check with git -C <root> check-ignore -q <dir>/x (step 2 did this for a new .worktrees/) |
| Docker build context | .worktrees/ in .dockerignore (Docker does not read .gitignore) |
| ESLint 9 flat config | ignores: ["**/.worktrees/**"], or includeIgnoreFile for .gitignore |
| Jest | testPathIgnorePatterns: ["/\\.worktrees/"] (regular expressions) |
| Vitest | exclude: [...configDefaults.exclude, "**/.worktrees/**"] (globs) |
| Biome | vcs.useIgnoreFile: true |
| IDE file watchers | files.watcherExclude (VS Code), "Excluded" folder (JetBrains) |
Use the actual directory name in each entry. Add the Git entry always, and the others only for a tool whose config file exists in <root>; and report each added line as uncommitted.
Done when the worktree directory is ignored by git and by every tool in the table with a config file in <root>, or the report lists it as not added.
A worktree has tracked files only. Node and Bun resolve a missing node_modules by walking up the tree, so <wt> without an install silently loads <root>/node_modules, installed for another lockfile. Config lookups walk up the same way (tsconfig extends, ESLint, Prettier, .npmrc, the Cargo workspace root, an activated venv on PATH).
Run the check:
node <skill>/scripts/wt-deps.mjs check <wt>
It finds every project in <wt> by its lockfiles (nested ones too), compares the lockfile hash with <root>'s, reads the marker of the last install, and prints one action per ecosystem. Exit 1 means an install is due.
| Action | Do |
|---|---|
install | Run the printed command in <wt>, also when the lockfile equals main's: npm, pnpm, Bun, Yarn and uv install from their global cache, so it is cheap. |
reinstall | The lockfile changed since the last install, or no marker exists. Run the printed command. |
ok | Nothing to do. |
verify | Poetry, Pipenv, Bundler keep the environment outside the worktree, so the marker cannot see it. Run the printed command; it is idempotent. |
cache | Go, Cargo, NuGet, Gradle, Maven: one global cache serves every worktree; the build fetches what it needs. |
A symlink to <root>'s dependency directory is the last resort, made only when the user asks for it after hearing the cost.
Node resolves the real path, so packages load relative to <root>.
Bundlers and test runners treat symlinks differently; postinstall output (native builds, generated clients) belongs to <root>.
A delete through the link (rm -rf node_modules/, trailing slash) wipes <root>'s install. A request to "not reinstall" is answered with the cache install above, which takes seconds.
An install needs network access and write access to the package manager's global cache (for example ~/.npm, ~/.bun/install/cache, ~/.local/share/pnpm, ~/.cache/uv), which lies outside the repo. If the sandbox denies either, report it as in step 2.
After the install, record it and check resolution:
node <skill>/scripts/wt-deps.mjs mark <wt>
node <skill>/scripts/wt-deps.mjs resolve <wt>
mark writes the lockfile hashes to <wt>'s git dir, outside the work tree, so a later check reports a lockfile change as reinstall. resolve exits 1 for each missing dependency or dependency whose real path does not start with <wt>, checking every package.json in <wt>.
It names where each dependency resolved; with Yarn Plug'n'Play it asks the worktree's .pnp.cjs. For Python, check by hand through the project's runner (uv run, poetry run, pipenv run): <runner> python -c "import sys; print(sys.prefix)" in <wt> prints a different prefix than in <root> (uv: one starting with <wt>).
Done when check and resolve both exit 0, or the report names the symlink the user asked for.
Untracked local files (.env*, .claude/settings.local.json, local certificates) are not in <wt>. List the candidates:
git -C <root> ls-files --others --ignored --exclude-standard --directory | grep -v -e node_modules -e '^\.worktrees/'
git -C <root> ls-files --others --exclude-standard | grep -i -e '\.env' -e secret -e '\.pem$'
Show the list and ask which to copy. A file holding secrets (tokens, passwords, keys, connection strings) is copied only after the user names it.
Copied silently, the secrets sit in a second place that later commands, agents and commits reach. Copy an approved file with cp -p -n, then run git -C <wt> check-ignore -q <file>; if it is not ignored, say so before anyone runs git add -A. A .worktreeinclude file the user maintains counts as the approved list.
If the user cannot be asked, copy nothing that holds secrets, and name each missing file and the test it breaks in the report.
Done when the report's Local files line lists each candidate as copied (approved) or not copied.
Run the project's test command in <wt>. If tests fail, report the failures and ask whether to go on; without a clean baseline, later failures cannot be told apart from existing ones.
Done when the test command's result is in the report.
Isolation: <existing worktree <path> | new worktree <path> on <branch> | in place: <reason>>
Ignore entries: <lines added, uncommitted | none needed>
Dependencies: <ecosystem: action taken; resolve: inside | symlink by request>
Local files: <file: copied (approved) | not copied: reason>
Baseline: <command> -> <N passed, M failed>
Sandbox: <none | what was denied>
Done when this block is printed with every line filled.
When the user is done with a worktree:
git -C <wt> status --short and git log <branch> --not --remotes --oneline, and report uncommitted files and unpushed commits before deleting anything.rm <wt>/<link> (no -r, no trailing slash).cleanup-command is configured, run it from <root> with placeholders quoted as in step 2. A create command that also opens a terminal-multiplexer workspace leaves that workspace open unless cleanup closes it. Otherwise run git worktree remove <wt>. It refuses a worktree with untracked or changed files; --force deletes them, so use it only after the user, shown the list from step 1, says to discard them. Otherwise keep the worktree. A locked worktree needs git worktree unlock <wt> first.git worktree prune drops entries whose directory is already gone, for example after an rm -rf <wt>.git branch -d only if git branch --merged lists it.When one session made several worktrees, clean up each one as soon as its pull request is merged; do not wait for the last.
git clean -xdff (double f) in <root> deletes in-repo worktrees with their uncommitted work; git clean -xdf skips them.
After <root> or <wt> moves, or in a container that mounts the repo at another path, the worktree's .git file points at a stale absolute path: run git worktree repair in the moved checkout. Git 2.48 and later can store relative links instead (git worktree add --relative-paths, or git config worktree.useRelativePaths true); older git versions cannot read the repository afterwards.
Many worktrees each hold their own dependency install, so builds can start failing with ENOSPC or "No space left on device". Measure before you delete anything:
df -h <root>
du -sh <root>/.worktrees/* 2>/dev/null | sort -h | tail
du -sh "${TMPDIR:-/tmp}" ~/.bun/install/cache ~/.npm ~/.cache 2>/dev/null
Free space in this order, and stop when the build works again:
git worktree prune for entries whose directory is gone.git branch --merged <main>): remove each through the Cleanup steps above, which check for uncommitted and unpushed work first.bun pm cache rm, npm cache clean --force, pnpm store prune. The next install downloads again; nothing else is lost.A directory you did not create, or cannot match to a merged branch or a cache, stays: list it with its size and ask the user. It may hold another session's work.
Done when the build runs again, and the report lists what was removed with the space freed and each directory left for the user to decide.
| Mistake | Result | Fix |
|---|---|---|
| New worktree from inside a worktree | Duplicate isolation, wrong base branch | Step 1 |
git worktree add beside a native tool | State the harness cannot see or clean up | Step 2 |
Committing the .gitignore entry unasked | An unrequested commit on the user's branch | Report it uncommitted |
No install in <wt> | Tests load <root>'s dependencies | Step 4, resolve |
Symlinked node_modules to save time | Wrong resolution; a delete can wipe <root>'s install | Cache install |
Copying .env to make tests pass | Secrets spread without consent | Step 5 |
| Working around a sandbox denial | Bypasses the user's security boundary | Report, work in place |
rm -rf <wt> | Stale entry in .git/worktrees/ | git worktree remove |
What SKILL.md sets; muted values are Claude Code's defaults
27 skills in this plugin
/checking-before-publishingPrivacy check for anything about to go public. Use when about to post an issue or PR comment, opening a PR, committing, pushing, publishing a release, blog post or README, or sharing screenshots, GIFs, videos or terminal recordings; when relaying a subagent's text to a public place; or when the user asks to "check for private data", "scrub" or "redact" something before sharing./cloning-voices-with-kittenttsVoice cloning with KittenTTS 2 from a short recording and text. Use when users say "kitten tts", "Stimme klonen", "clone my voice", "sprich diesen Text mit meiner Stimme", or "text to speech with my voice" and want a speech WAV.
/competing-implementationsOther plugins' skills for the same job
npx claudepluginhub lorenzh/skills --plugin lorenzh