Sub topic 14
Limits of worktrees and when to use containers or clones
This chapter assumes you can create worktrees, run agents in them, and merge their branches. It covers what worktrees do not protect against and which tool to use when they are not enough.
What worktrees isolate, and what they don't
A linked worktree gives each agent its own directory, its own checked-out files, its own index, and its own HEAD. All worktrees share one repository: the same object database, the same refs, the same config, and the same hooks.
Everything outside the Git repository is shared too, because worktrees are only directories on one machine.
| Resource | Isolated per worktree? | Consequence |
|---|---|---|
| Tracked files | Yes | Agents cannot overwrite each other's edits |
| Index and `HEAD` | Yes | Staging in one worktree does not affect another |
| Branch checkout | Yes, one branch per worktree | Two worktrees cannot hold the same branch |
| Refs, tags, stashes | No | A tag or `git gc` in one worktree affects all |
| `.git/config` and hooks | No | A config change applies everywhere |
| Ports, databases, caches | No | Two dev servers can fight over port 3000 |
| Home directory and global tools | No | Agents share `~/.config`, package caches and credentials |
| Network and system packages | No | An agent that installs a tool changes it for all |
Signs that worktrees are not enough
Look for these failure modes:
- Port or database collisions. Two agents run the test suite against the same local database and corrupt each other's data.
- Unsafe commands. An agent can run
rm -rforgit push --force, and a worktree does not limit it. It can evencd ..into a sibling worktree or the main checkout. - Shared secrets. Every agent can read your home directory, SSH keys, and cloud credentials.
- Conflicting system dependencies. One task needs Python 3.9 and another needs 3.12 installed system-wide.
- Repository-level operations. An agent runs
git gc --prune=now, rewrites refs, or edits.git/config, and all worktrees feel it.
Alternatives
Containers
Run each agent inside a container with only its worktree mounted. This adds process, network, and filesystem isolation. Each container gets its own ports and can have its own database service.
docker run --rm -it \
-v "$PWD/../myrepo-auth:/work" \
-w /work \
node:22 bashThere is a catch. A linked worktree's .git is a file that points to the main repository's .git/worktrees/ directory by absolute path. Inside the container that path does not exist, so Git commands fail. You must also mount the main repository's .git directory at the same path, which weakens the isolation. Consider whether the agent needs to run Git at all, or whether you commit from the host.
Separate clones
A full clone has its own object database, refs, config, and hooks. Use git clone --local or a fresh git clone for each agent. Choose clones when you do not trust an agent with the shared repository, or when you need to isolate tools such as hooks and gc. The cost is more disk space and the need to fetch or push between clones to integrate work.
Virtual machines or remote sandboxes
Use these when agents need different operating systems, untrusted code must run, or credentials must be strictly separated.
Decision criteria
| Situation | Choice |
|---|---|
| Tasks edit different files, tests are cheap, you trust the agent | Worktrees |
| Tasks need their own ports or databases | Worktrees plus per-task environment variables, or containers |
| Agent runs with broad permissions or untrusted input | Container or VM |
| Tasks need different system toolchains | Containers |
| Repository-level isolation needed (hooks, refs, `gc`) | Separate clones |
Exercise
Two agents run in separate worktrees. Both start a dev server on port 3000 and the second one fails. Does this show a flaw in how you set up the worktrees, and what are two ways to fix it?
Show answer
No. Worktrees isolate files and branches, not network ports. Fix it by giving each agent its own port, for example through a PORT environment variable set per worktree, or by running each agent in a container so each has its own network namespace.
Exercise
You run an agent in a Docker container with only its linked worktree mounted. Git commands inside the container fail with a "not a git repository" error. Why, and what is the trade-off of fixing it?
Show answer
The worktree's .git file points by absolute path to the main repository's .git/worktrees/ directory, which is not mounted in the container. Mounting the main .git directory at the same path fixes it, but then the agent can modify shared refs, config, and hooks, which reduces isolation. Alternatively, use a separate clone, or commit from the host.