Worktree-based development — folder layout, setup, creating/opening/removing worktrees, and what is shared (pnpm store, Nx cache) vs per-worktree (node_modules)
9 stars
0 votes
0 copies
3 views
Added October 5, 2026
devopsrustgoshellnodedockergitapidatabase
Works with
claude code
vscode
terminal
cli
api
Security analysis
C67/100
criticalExfiltrates credentials via HTTP — exact pattern from Snyk ToxicSkills study
mediumInstalls packages at runtime which could introduce malicious dependencies
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Worktrees?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jacobley-worktrees)
---
name: worktrees
description: Worktree-based development — folder layout, setup, creating/opening/removing worktrees, and what is shared (pnpm store, Nx cache) vs per-worktree (node_modules)
---
# Skill: Worktrees
**Do all development in a git worktree.** Each worktree opens in its own devcontainer. The main checkout still works normally, but it's best left on `main` and used as the place where new worktrees start.
Each worktree has its own `node_modules`, `.nx/` and devcontainer. Downloaded packages and build outputs are shared through `.shared/`.
---
## Layout
The main checkout stays wherever you cloned it. Worktrees go in a **dedicated sibling folder named after it**, `<repo>-worktrees/`, so they never collide with your other projects. For leyman, that is `leyman/` and `leyman-worktrees/`:
```
~/code/
some-other-project/ <- untouched, never mounted
<repo>/ <- main checkout, owns .git (never delete it)
<repo>-worktrees/ <- created automatically
feat-x/ <- worktree for branch feat/x
.shared/
pnpm-store/ <- pnpm content-addressed store
pnpm-cache/ <- pnpm registry metadata
go-build/, go-mod/ <- Go build and module caches
nx-cache/ <- Nx task outputs (served by the nx-cache sidecar)
claude-config/ <- CLAUDE_CONFIG_DIR (auth, memory; settings.json and CLAUDE.md link to .claude-shared/)
gh-config/ <- GH_CONFIG_DIR (GitHub CLI login)
plans/ <- plans shared by every worktree (`<worktree>/plans` symlinks here)
.claude-shared/ <- Claude settings.json and CLAUDE.md, shared with your other repos' devcontainers
```
Every devcontainer, including the one for the main checkout, mounts **`<repo>/` and `<repo>-worktrees/`**, plus `.claude-shared/`. Each one is mounted at the same absolute path it has on the host. This has three effects:
- Git stores absolute paths in its worktree links, and those paths resolve both on the host and in the container.
- Every container can reach `.shared/`.
- `docker` bind mounts started from inside the container (docker-outside-of-docker) use paths that are valid on the host.
The container's workspace folder is therefore the host path (for example `/Users/you/code/<repo>-worktrees/feat-x`), not `/workspace`.
[`.devcontainer/initialize.sh`](../../../.devcontainer/initialize.sh) runs on the host before the container starts (`initializeCommand`). It does the following:
- Fails if the folder being opened is neither the main checkout nor a direct child of `<main>-worktrees/`.
- Creates `.shared/`. The first time, it moves `<main>/.pnpm-store` into `.shared/` and copies `<main>/.claude-config` there.
- Creates `.claude-shared/` beside the main checkout, and links `claude-config/settings.json` and `claude-config/CLAUDE.md` to it (absolute links, which resolve because it's mounted at the same path). Another repo using this setup links the same files, so settings and user-level instructions are shared; memory (`claude-config/projects/`) stays per repo. The first time, the repo's own file moves there (or an empty one is created). If both exist, it only warns: merge the repo's file into the shared one by hand and delete it, and the next start links it.
- Links `<worktree>/plans` to `.shared/plans/`. Plans written there are visible from every worktree and never committed (`/plans` is gitignored).
- Writes the paths and a namespaced `COMPOSE_PROJECT_NAME` (`<repo>`, `<repo>_feat-x`) to `.devcontainer/.env`, which is gitignored and read by `docker-compose.yml`. Container names therefore never clash with same-named worktrees from other repos. It also writes `REPO_NAME` and `WORKTREE_NAME` (`main` for the main checkout), as DNS labels, for the worktree's URL.
- Starts the machine's shared HTTPS ingress, [`tools/local-ingress`](../../../tools/local-ingress/README.md), if it isn't running. A failure only warns: the container still starts, without its URL.
---
## URLs
Each worktree's devcontainer serves port 5173 at `https://<worktree>.<repo>.localhost`, e.g. `https://feat-x.leyman.localhost` for leyman (the main checkout is `main.leyman.localhost`). Browsers resolve `*.localhost` to this machine themselves. One Caddy per machine ([`tools/local-ingress`](../../../tools/local-ingress/README.md)) routes by host name to the container labeled with it, so worktrees never compete for a port. `scripts/worktree up` prints the URL.
Once per machine, trust its CA: open `http://ingress.localhost` for the root certificate and the steps for your OS. Until then, browsers warn about the certificate.
Bind dev servers to `0.0.0.0:5173` inside the container, so the ingress can reach them. `serve [project]` runs a project's `dev` target and prints the URL.
For a phone or anyone off this machine, bring a tunnel (cloudflared, ngrok). To have the app see the tunnel's own host, set `TUNNEL_HOST` in the worktree's `.devcontainer/.env` and rebuild: recipes in [the ingress's tunnels section](../../../tools/local-ingress/README.md#tunnels).
---
## Daily workflow
Run these from a VS Code window opened **locally** on the main checkout (dismiss "Reopen in Container"), or from a host terminal:
| Action | VS Code task | CLI |
|--------|--------------|-----|
| Start work | `Worktree: new` | `scripts/worktree new feat/x [--from main]` |
| Reopen later | `Worktree: open` | `scripts/worktree open feat/x [--local] [--print]` |
| Clean up | `Worktree: remove` | `scripts/worktree rm feat/x [--delete-branch] [--force]` |
| Container without an editor | | `scripts/worktree up feat/x` / `scripts/worktree shell feat/x [cmd...]` |
| List | | `scripts/worktree list` |
- `new` creates `<repo>-worktrees/feat-x` (`/` in a branch name becomes `-`). It checks out `feat/x` if that branch exists. Otherwise it creates the branch from `--from`, which defaults to the current `HEAD`. It then opens the folder **directly in its devcontainer**.
- With the default editor (`vscode`), `new` and `open` print a Cmd+clickable `vscode://` link and hand it to the OS (macOS `open`, Linux `xdg-open`). VS Code asks for confirmation ("An external application wants to open…"). Keep that prompt: turning it off lets any app or webpage open a dev container for any folder, which runs that folder's `initializeCommand` on the host. `--no-open` (for `new`) and `--print` (for `open`) only print the link. If the link opens the folder but not the container, use `open --local` and click **Reopen in Container**.
- `rm` runs `docker compose down` on the worktree's devcontainer, which would otherwise keep running, and then `git worktree remove`. It refuses to remove the main checkout or the worktree you're running it from.
- Git won't check out the same branch in two worktrees. With the main checkout on `main`, worktrees branch from `main` rather than checking it out.
The tasks in `.vscode/tasks.json` are meant for a local window. In a container window, nothing launches automatically, but the printed links should still work when clicked.
### Other editors
Only the step that launches the editor is editor-specific. `initialize.sh`, the compose file and `devcontainer.json` follow the open [Dev Containers spec](https://containers.dev). Set `WORKTREE_EDITOR` in your shell profile:
| `WORKTREE_EDITOR` | Behavior |
|---|---|
| `vscode` (default) | Print a `vscode://` link and open it via the OS, as described above |
| `none` | Print only the path. Then use `up`/`shell`, or open the worktree with your IDE's dev container support (for example JetBrains: open `.devcontainer/devcontainer.json`) |
`up` and `shell` use the reference [devcontainer CLI](https://github.com/devcontainers/cli) (`npm install -g @devcontainers/cli`). It runs `initialize.sh`, compose and `postCreateCommand` the same way VS Code does. That makes it the right choice for terminal-only work, for example running Claude Code with `scripts/worktree shell feat/x claude`.
To support another editor, add a case to `open_worktree` in `scripts/worktree` that builds the editor's URL. Only add an editor after its URL format has been confirmed to work.
---
## What's shared vs. per-worktree
| Thing | Scope | Mechanism |
|-------|-------|-----------|
| `node_modules` | per worktree | Normal `pnpm install` (via `postCreateCommand`) |
| pnpm store | shared | `pnpm_config_store_dir=<worktrees>/.shared/pnpm-store` in `docker-compose.yml` |
| pnpm metadata cache | shared | `pnpm_config_cache_dir=<worktrees>/.shared/pnpm-cache` in `docker-compose.yml` |
| Go build and module caches | shared | `GOCACHE` and `GOMODCACHE` under `<worktrees>/.shared/` in `docker-compose.yml`, kept out of the container so rebuilds stay fast (see the [devcontainer skill](../devcontainer/SKILL.md#rebuilding)) |
| Nx local cache and project graph (`.nx/`) | per worktree | Nx default |
| Nx task outputs | shared | `nx-cache` sidecar ([`nx-cache-server.mjs`](../../../.devcontainer/nx-cache-server.mjs)) via `NX_SELF_HOSTED_REMOTE_CACHE_SERVER` |
| Nx task outputs built by `dagger-test` | shared | `dagger-nx-cache` sidecar, the same server on `.shared/nx-cache-dagger`, with no upstream (see the [dagger-ci skill](../dagger-ci/SKILL.md#caching)) |
| `nx`, `biome`, `eslint`, `scripts/commands` on `PATH` | per worktree | `remoteEnv` in `devcontainer.json` uses `${containerWorkspaceFolder}` |
| Claude config | shared | `CLAUDE_CONFIG_DIR=<worktrees>/.shared/claude-config` |
| Claude `settings.json` and `CLAUDE.md` | shared with other repos | Links from `claude-config/` into `.claude-shared/`, beside the main checkout |
| GitHub CLI login | shared | `GH_CONFIG_DIR=<worktrees>/.shared/gh-config` |
| Git push/pull credentials | host | VS Code forwards the host's SSH agent (`SSH_AUTH_SOCK`); keys never enter a container |
| Dagger Go bindings (`dagger/dagger.gen.go`, `dagger/internal/`) | per worktree | Gitignored, generated by `dagger-develop` (via `postCreateCommand`) |
| Services for tests (Valkey, Postgres) | per worktree | Sidecars in `docker-compose.yml`, so test runs in different worktrees never share state |
Why Nx needs a server: Nx restores a local result only if its cache database has a row for it, and setting `NX_CACHE_DIRECTORY` leaves that database in each checkout's `.nx/workspace-data` ([`cache-directory.ts`](https://github.com/nrwl/nx/blob/19a9e5f4e525b517faa4b1ab086781c9d158957d/packages/nx/src/utils/cache-directory.ts#L310-L326), [`NxCache::get`](https://github.com/nrwl/nx/blob/19a9e5f4e525b517faa4b1ab086781c9d158957d/packages/nx/src/native/cache/cache.rs#L275-L303)). Pointing it at a shared folder therefore still produces cache misses on Nx 24.0.5 (checked 2026-11-19 with two clones: the same hashes, no hits across). Without that setting, Nx 24 shares cache and database across checkouts of one repository under `~/.nx/<workspace id>/`, but each container has its own home, so that sharing stops at the container. The sidecar implements Nx's self-hosted remote cache API, and hits show as `[remote cache]` in Nx output. Each container runs its own sidecar, but they all read and write `.shared/nx-cache`. Results expire 7 days after they were stored (`NX_CACHE_SERVER_MAX_AGE_DAYS`): the server never serves an expired one, and deletes them at startup and hourly. Once `NX_CACHE_SERVER_UPSTREAM` is set in `.devcontainer/docker-compose.yml`, a result the sidecar lacks is also read from the hosted cache that CI on `main` writes to ([`infra/nx-cache`](../../../infra/nx-cache/README.md)), so a fresh worktree starts with `main`'s results. Devcontainers never write there.
Dagger sets `pnpm_config_store_dir=.pnpm-store` and mounts its own cache volume at that path. Never set `storeDir` in `pnpm-workspace.yaml`: it takes precedence over the env var and would split the store.
---
## Sharing agent state across repos
One person working in several repos built this way can share two things between them: agent instructions and settings, and plans. Both work with a single repo too, and neither needs setting up: `initialize.sh` creates the folders on every start.
### Instructions, settings and memory
Every repo's `claude-config/settings.json` and `claude-config/CLAUDE.md` link into the same `.claude-shared/` folder, as long as the repos' main checkouts sit side by side (`~/code/<repo-a>/`, `~/code/<repo-b>/`). A change in one repo, including one made through `/config`, is seen by all of them. Claude's auto-memory can't be shared this way: it lives in `claude-config/projects/<path>/memory`, one per repo.
So decide where a note goes by who it's about:
- **Your workflow, in every repo** (how you commit, when to push, how you like reviews): the shared `CLAUDE.md`. When a memory turns out to hold in another repo too, move it there and delete the memory.
- **One repo's decisions** (its packages, its history): that repo's memory.
- **What any contributor needs**: the repo's own `AGENTS.md` or skills, committed. Neither of the above.
### Plans
`plans/` holds work that isn't done yet: checklists, scoped designs, follow-ups. It is gitignored, and every worktree of a repo links to the same `.shared/plans/`, so a plan written in one worktree is read in the next. On its own it's a plain local folder.
To share plans across repos and machines, make it a git repo:
1. Create a **private** repository for your plans, with one folder per repo (`<repo-a>/`, `<repo-b>/`) and a `README.md` with your rules.
2. In one repo, clone it as that repo's `.shared/plans` (`git clone <url> <repo>-worktrees/.shared/plans`, before the folder exists, or into the empty one).
3. In each other repo, either clone it again into its own `.shared/plans`, or replace that folder with a link to the first clone. A link needs the clone to be mounted, so it must sit inside a folder the devcontainer mounts.
Then:
- **A plan goes in the folder of the repo that will do the work.** That makes the plans repo the way repos send each other work: a session in one repo that finds something another repo should do writes it into that repo's folder.
- **Sync by hand, every time:** `git -C plans pull --rebase` before reading or editing a plan, and commit, pull and push right after changing one. Sessions in other worktrees and repos push to it too. Nothing syncs on its own.
- **Plans hold only undone work.** Mark an item done while it's on a branch, and delete it once it's on the default branch.
---
## Agents
If you are already in a worktree, do the work there, on its branch. Don't create another worktree: `scripts/worktree new` is only for starting a branch from the main checkout.
Never `rm` the worktree you are running in; `scripts/worktree` refuses to do it.
---
## Maintenance and troubleshooting
- **Container fails with "must live in …-worktrees"**: the worktree was created somewhere else, for example nested under `.claude/worktrees/`. Recreate it with `scripts/worktree new`.
- **No `[remote cache]` hits**: from inside the container, run `curl -s -o /dev/null -w '%{http_code}' http://nx-cache:3000/v1/cache/x`. A `404` means the server is up. Hits only happen when task inputs match, and uncommitted changes alter the hashes.
- **The shared Nx cache is too big**: each of its servers deletes results older than 7 days, then the ones read longest ago until it is under `NX_CACHE_SERVER_MAX_SIZE_GB` (5 by default), at startup and hourly, so it can pass that between sweeps. A full cold `verify` stores about 700 MB. Set a different size in `.devcontainer/.env`, and rebuild the container.
- **The pnpm store keeps growing**: run `pnpm store prune` in any worktree.