Sub topic 9
What Worktrees Do Not Isolate
This chapter assumes you can create and remove worktrees with git worktree add and git worktree remove. It also assumes you know that each linked worktree has its own working directory, index, and HEAD. Here you will learn which state worktrees still share. That shared state is where parallel agents most often interfere with each other.
The boundary of isolation
A linked worktree isolates the files Git tracks, plus the index and HEAD for that checkout. Everything else is shared, either through the repository or through the machine. Shared state falls into three groups:
- Repository state stored in the common
.gitdirectory. - Machine state outside the repository, such as ports, caches, and databases.
- Absolute paths and global tool configuration.
Knowing which group a problem belongs to tells you where to fix it.
Repository state shared through .git
Each linked worktree has a small private directory at .git/worktrees/<name>. It holds that worktree's HEAD, index, and a few other per-worktree files. All other repository data lives in the main .git directory and is visible to every worktree.
Branches and refs
Branches, tags, and remote-tracking refs are shared. If an agent runs git branch -D agent/search from its own worktree, the branch disappears for everyone. If one agent runs git fetch, the updated remote-tracking refs appear in every worktree at once. A tag created in one worktree is immediately visible in the others.
This matters most for commands that move or delete refs. Tell agents in their instructions not to touch branches other than their own. Do not let them run git branch -D, git push --force, or git tag -d on anything they did not create.
The object database
Commits, trees, and blobs are stored once and shared. This is useful, because creating a worktree is cheap. A commit made in one worktree can be seen at once from another by its hash, even before any branch points at it.
The shared object store also means garbage collection is global. git gc and git prune run from any worktree consider every worktree's refs and index, so they do not delete objects that another worktree needs. Git is designed for this, but do not delete files inside .git by hand.
Stash
The stash is a ref (refs/stash) in the shared repository. It is not per-worktree. If agent A runs git stash and agent B runs git stash pop, B applies A's changes to B's files.
This is a common and confusing failure. Agents often stash to get a clean tree before switching context. Tell agents to commit work in progress to their own branch instead. A commit on agent/auth cannot be picked up by accident by another worktree.
# In an agent's instructions: commit instead of stashing
git add -A
git commit -m "wip: partial auth changes"Config, hooks and attributes
The repository's .git/config is shared, so git config user.name ... without --worktree changes the setting for every worktree. Hooks in .git/hooks run in every worktree. A pre-commit hook that assumes it runs in the main checkout can fail or behave differently in a linked one.
If you need a setting that differs per worktree, enable the extensions.worktreeConfig setting and write with git config --worktree. Use this sparingly. Most workflows are simpler if all agents share one configuration.
Locks on the repository
Git uses lock files, such as .git/index.lock (per worktree) and .git/packed-refs.lock or ref lock files (shared). When several agents commit at the same moment, one may fail with an error like Unable to create '.../refs/heads/...lock': File exists. This usually clears when the other command finishes. If an agent crashes and leaves a stale lock, remove it only after you confirm no Git process is running.
Make agents retry once after a short wait instead of deleting lock files themselves.
Machine state outside the repository
These resources are not part of Git at all. Worktrees do nothing to separate them.
Ports
Two agents that each start a dev server on port 3000 will conflict. The second fails to bind, or, worse, the agent talks to the first agent's server and reports results from the wrong code.
Assign each worktree its own port through an environment variable.
#!/usr/bin/env bash
# Start a dev server on a port unique to this task.
set -euo pipefail
task="$1"
case "$task" in
auth) PORT=3001 ;;
billing) PORT=3002 ;;
search) PORT=3003 ;;
*) echo "unknown task: $task" >&2; exit 1 ;;
esac
export PORT
echo "Starting $task on port $PORT"The server in your project must read PORT for this to work. If it hard-codes a port, the agent must change that, or you must use a different approach such as a container.
Databases and other services
A local database is a single shared service. If every agent runs migrations against myapp_dev, one agent's schema change breaks another agent's tests. Test data written by one run shows up in another.
Give each worktree its own database name, or use a throwaway database per test run.
# Example for PostgreSQL: one database per task
createdb "myapp_auth"
export DATABASE_URL="postgres://localhost/myapp_auth"The same applies to Redis databases, message queues, local object stores, and any other service on localhost.
Caches and global tool state
Package managers and build tools keep caches in your home directory, such as ~/.npm, ~/.cache, or ~/.cargo. Most of these tools handle concurrent use safely, but not all. Some tools use a single lock for their global cache, so parallel installs wait for each other. Others can corrupt a cache when two processes write at once. Check the documentation for the tools you use.
Build output is different. If the build directory is inside the worktree (node_modules, target, dist), it is separate for each worktree, because it is ordinary untracked files. If a tool is configured to write to a fixed absolute path, such as a shared CARGO_TARGET_DIR, all worktrees will write to the same place and overwrite each other.
Untracked and ignored files
A new worktree contains only tracked files. Files ignored by Git, such as .env, node_modules, or local certificates, are not copied. This is isolation in one sense, because agents do not overwrite each other's .env. But it also means each worktree starts without the local setup it needs. Copy or generate those files for each worktree, as you did in the per-agent setup chapter.
Be careful with symlinks. Linking node_modules from the main checkout into every worktree looks like a time saver, but then all agents share one dependency tree, and one agent's npm install changes what the others run.
Absolute paths
Some files record absolute paths. A virtual environment created with python -m venv stores its location, and moving or copying it breaks it. Editor settings, build caches, and generated config may do the same. Create these inside each worktree, and do not copy them from another.
Shared and isolated state at a glance
| State | Isolated per worktree? | Fix if it causes trouble |
|---|---|---|
| Tracked files, index, `HEAD` | Yes | None needed |
| Branches, tags, remote refs | No | Rules for agents; one branch per agent |
| Stash | No | Commit instead of stashing |
| `.git/config` and hooks | No | `--worktree` config; hooks that work anywhere |
| Ports | No | Per-worktree `PORT` |
| Local databases and services | No | One database name per worktree |
| Global caches | No | Check tool concurrency behavior |
| Ignored files such as `.env` | Yes, but absent | Copy or generate on setup |
| Absolute-path artifacts | Yes, but fragile | Create inside each worktree |
Detecting interference
Interference often looks like a bug in the agent's code. Before you debug the code, check shared state.
- Run
git worktree listand confirm each agent is in the expected directory on the expected branch. - Run
git stash listand see whether an unexpected stash exists. - Run
git branch --list "agent/*"and check no branch is missing. - Run a check on listening ports, for example
lsof -i -P -n | grep LISTEN, to see which process holds a port. - Inspect
git reflogfor the branch in question. The reflog is shared per ref and shows who moved it and when.
git worktree list
git stash list
git branch --list "agent/*"
lsof -i -P -n | grep LISTENReducing the risk with agent rules
You cannot make the shared state private, but you can limit how agents touch it. A short rules file in each worktree helps:
- Work only on your own branch, and never delete or rename other branches.
- Do not use
git stash. Commit work in progress. - Do not run
git gc,git prune, orgit worktree prune. - Use the port and database name given in your environment variables.
- Do not edit files outside your worktree directory.
These rules are guidance, not enforcement. An agent can still break them, so verify the results during review.
Exercise
Two agents run in separate worktrees. Agent A runs git stash to clear its working tree, and agent B later runs git stash pop and finds unfamiliar changes in its files. Explain why this happens, and give one change to the agent instructions that prevents it.
Show answer
The stash is stored in the shared repository as the ref refs/stash, not in each worktree. Agent B popped the entry that agent A created and applied it to B's files. To prevent it, tell agents not to use git stash and to commit work in progress to their own branch instead.
Exercise
Three worktrees each run the test suite against the same local database myapp_dev. Tests pass when run one at a time but fail intermittently when run together. Which kind of shared state is the likely cause, and how do you fix it?
Show answer
The shared database, which is machine state that worktrees do not isolate. Concurrent runs change the same schema and rows. Fix it by giving each worktree its own database, for example by setting a different DATABASE_URL (such as myapp_auth, myapp_billing, myapp_search) per worktree.