iCode Workbench · Guide · Isolation
Git worktrees for coding agents, explained
A Git worktree is a separate working directory for the same repository. It shares the history and object store, but it has its own checkout, its own branch, and its own uncommitted changes. For coding agents, that is the isolation layer that makes parallel work possible.
This guide covers what a worktree is, why a branch alone is not enough, what a fresh worktree is missing, and the commands and cleanup you need before you run more than one agent.
This guide is currently available in English only.
What a Git worktree is
A normal Git checkout has one working directory and one .git directory. A worktree adds another working directory that points at the same repository. Git keeps one index and one HEAD per worktree, so each worktree can be on a different branch without copying the object store.
git worktree add ../project-auth -b feature-auth git worktree add ../project-billing -b feature-billing git worktree list
The two new directories are full checkouts. Each has its own branch, its own uncommitted changes, and its own working tree. The repository history is shared, so creating a worktree is cheap: no clone, no second object store, no network round trip.
Git will refuse to check out the same branch in two worktrees at once. That is a feature: it prevents two agents from committing to the same branch from different directories.
Why coding agents need one
An agent edits files. If two agents edit the same checkout at the same time, they can overwrite each other, read stale state, or both report success while the last write wins. A worktree gives each agent its own filesystem view, so the edits cannot collide.
A branch alone does not solve this. Two agents can check out different branches in the same directory, and they still share the working tree. Switching branches under a running agent is worse: the files change while the agent is reading them.
The worktree also makes review cleaner. The diff, the tests, and the surrounding code are all in the checkout where the agent worked. You do not have to reconstruct the state after the fact.
For the full workflow, see how to run multiple coding agents in parallel.
Worktree vs branch vs clone
| Branch | Worktree | Clone | |
|---|---|---|---|
| Working directory | Shared | Separate | Separate |
| Git history | Shared | Shared | Duplicated |
| File isolation | No | Yes | Yes |
| Setup cost | Lowest | Low | High |
| Best for | Switching tasks in one checkout | Parallel agents in one repository | A separate repository or machine |
Use a branch when you are working alone and switching context. Use a worktree when two agents need to work at the same time. Use a clone when you need a separate object store, a separate remote, or a separate machine. For the full comparison, read worktrees vs branches vs clones.
What a fresh worktree is missing
A worktree contains tracked files. It does not contain everything your development environment needs:
- Gitignored files.
.env, local config, and generated credentials are not in a fresh checkout. Copy them with a setup script, or use your harness's worktree include mechanism if it has one. - Dependencies.
node_modules, virtualenvs, and build caches are per-directory. Install them in each worktree. Do not symlink unless you know the toolchain tolerates it. - Ports. Two dev servers both want 3000. Assign a fixed port per worktree and make the server fail rather than silently pick another.
- Databases and caches. Migrations and seeds from one agent can corrupt another's test data. Use a separate database or schema per worktree.
- Git hooks, stash, and config. These are repository-level, not worktree-level. Do not stash in one worktree and expect another not to see it.
The right column of that list is where most “parallel agents” attempts fail. The worktree isolates files; it does not isolate the rest of your machine.
Commands and cleanup
The core commands are small:
# create a worktree and branch git worktree add ../project-auth -b feature-auth # list every worktree git worktree list # remove a worktree after the branch is merged git worktree remove ../project-auth # prune metadata for worktrees deleted by hand git worktree prune
Two rules matter more than the commands:
- Do not remove a worktree with uncommitted changes. Removing it deletes the working tree. Keep the checkout and the branch until the change is verified in main.
- Do not reuse a branch across worktrees. Git will refuse, and forcing it creates the exact collision the worktree was meant to prevent.
If you use Claude Code, claude --worktree feature-auth creates the worktree and starts the session in one step, and .worktreeinclude copies gitignored files into new worktrees. Codex and other directory-based agents work with plain git worktree add. For the full Claude Code workflow, read how to run Claude Code in parallel.
A concrete setup
Two tasks, two worktrees. The auth fix owns src/auth/** and the billing refactor owns src/billing/**. Neither touches the other's files.
git worktree add ../project-auth -b feature-auth git worktree add ../project-billing -b feature-billing cd ../project-auth pnpm install cp ../project/.env .env.local cd ../project-billing pnpm install cp ../project/.env .env.local
Then assign the shared resources before either agent starts:
- Auth worktree: port 3001, database
app_auth. - Billing worktree: port 3002, database
app_billing. - Both: the same branch protection and test command, so the review is comparable.
Put that setup in a script. The third worktree should not require you to remember the port, the database name, and the env copy from the first time. A setup script is also the place to initialize submodules and install dependencies.
When the auth branch merges, remove its worktree and prune. Keep the billing worktree until its branch is verified in main. Do not remove a worktree with uncommitted changes. If the merge conflicts, read parallel agents without merge conflicts.
Worktrees or containers?
Worktrees isolate files. They do not isolate processes, network access, credentials, or the rest of your machine. An agent running in a worktree can still read your SSH keys and reach the network.
Use worktrees when the agents are trusted, the main risk is file collisions, and you want a fast local setup. Use containers when you need process, network, or database isolation, or when the agent runs untrusted commands. The two can be combined: a container per worktree is a common setup for unsupervised runs.
The decision is about the threat model, not the number of agents. Two trusted agents in worktrees are safer than two agents sharing one checkout; one untrusted agent in a container is safer than either.
How many worktrees?
Worktrees are cheap to create, but they are not free: every checkout needs its own dependencies, env files, ports, and database. The practical limit is review capacity plus machine resources, not Git.
Start with two. Add a third only when the first two are merging cleanly and the review queue is consistently empty. Clean up merged worktrees promptly; a long-lived worktree drifts from main and turns the eventual merge into a design decision.
Pitfalls
- A worktree is not a sandbox. Agents still share your machine, your SSH keys, your network, and your credentials. Use containers when you need process or network isolation.
- Long-lived branches drift. Rebase each branch on main after every merge. The longer a worktree lives, the more the merge becomes a design decision.
- Submodules need their own setup. A worktree does not initialize submodules for you. Run
git submodule update --initin the new checkout. - Worktrees are not backups. They share the object store. A corrupted repository affects every worktree.
For the queue and review side of the workflow, read durable tasks, not terminal tabs and the review gate.