Sub topic 9
What Worktrees Do Not Isolate
This chapter assumes you can create a worktree with git worktree add and know that each worktree has its own branch and working files. Worktrees isolate files in the working tree. They do not isolate anything else on the machine, and parallel agents can still interfere with each other through that shared state.
What is shared
A linked worktree has its own checked-out files, its own HEAD and its own index. Everything else is shared, either through the common .git directory or through the operating system.
| Shared resource | Where it lives | How agents can collide |
|---|---|---|
| Object database, refs, tags | The main repository's `.git` directory | One agent deletes a branch or tag, or rewrites a ref, that another agent relies on. |
| Git config | `.git/config` | A `git config` change made by one agent applies to all worktrees. |
| Stashes | `refs/stash` | There is one stash list. An agent running `git stash pop` can pick up another agent's stash. |
| Hooks | `.git/hooks` | A hook that runs tests or formatters fires for every worktree. |
| Network ports | The operating system | Two dev servers cannot listen on port 3000 at once. |
| Global caches and home directory | `~/.npm`, `~/.cache`, `~/.config` | Concurrent package installs or tools that write one state file can corrupt it. |
| Databases and services | Local Postgres, Redis, Docker | Agents run migrations or write test data into the same database. |
Shared Git state
Branches are shared, so a worktree sees every branch in the repository. The main danger is one agent running a command against a ref that belongs to another. Examples are git branch -D agent/search or git push --force. Tell each agent in its instructions to touch only its own branch.
Stashes are the least obvious case. Run this in any worktree and you see entries created by every other worktree:
git stash listTell agents to commit instead of stashing. A commit on the agent's own branch cannot be picked up by another worktree by accident.
Git also locks some operations. Two agents running git fetch or git gc at once can hit a lock error such as Unable to create '.git/index.lock' or a ref lock error. Per-worktree index files make the first error rarer, but ref updates still contend. The fix is to retry. Do not delete lock files unless you have confirmed no Git process is running.
Shared machine state
Git cannot protect you from the rest of the machine. Check these before you start agents in parallel:
- Ports. Make each worktree's dev server and test server use a different port, for example by setting a
PORTvariable per worktree. - Databases. Use a separate database name or schema per worktree, so migrations from one agent do not break another's tests.
- Temporary files. A tool that writes to a fixed path such as
/tmp/build.lockis shared by all agents. PointTMPDIRat a per-worktree directory if needed. - Untracked files. Files ignored by Git, such as
.envandnode_modules, are not copied into a new worktree. They must be created in each one.
Seeing the sharing
This script shows that a stash made in one worktree appears in another. It assumes it runs inside an existing repository with at least one commit.
#!/usr/bin/env bash
set -euo pipefail
repo=$(git rev-parse --show-toplevel)
git worktree add --detach "$repo-demo" HEAD
echo "change" > "$repo-demo/note.txt"
git -C "$repo-demo" add note.txt
git -C "$repo-demo" stash push -m "from demo worktree"
echo "Stash list as seen from the main worktree:"
git -C "$repo" stash list
git worktree remove --force "$repo-demo"
git -C "$repo" stash dropThe final command drops the demo stash. If you had your own stashes before running the script, stash drop removes only the newest entry, which is the demo one.
Reducing the risk
- Tell each agent to commit rather than stash, and to work only on its own
agent/branch. - Do not give agents permission to delete branches or force-push.
- Assign each worktree its own port range and database name.
- Keep slow shared operations, such as
git gcandgit fetch, in the main worktree and run them yourself. - When isolation of ports, databases or system packages matters more than convenience, use containers or separate clones. A later chapter covers that choice.
Exercise
Two agents run in separate worktrees. Agent A runs git stash to save unfinished work, and later Agent B runs git stash pop. What happens, and what should you tell the agents to do instead?
Show answer
Stashes are stored in the shared repository, so there is one stash list for all worktrees. Agent B's git stash pop applies Agent A's stash into B's worktree and removes it from the list. Tell agents to commit work in progress on their own branch instead of stashing.
Exercise
Name two resources outside Git that two worktrees share, and one way to separate each.
Show answer
Examples: network ports (set a different PORT per worktree) and a local database (use a separate database name or schema per worktree). Fixed temporary file paths (set a per-worktree TMPDIR) are another valid answer.