Topic 1 of 5 · 5 min read
Git worktrees for parallel agents
Goal: Set up a separate worktree, branch and local services for each agent and remove them after merging.
Give every agent its own working folder, its own branch and its own local services, so no two agents ever touch the same files or the same running process.
Check yourself · 1 of 3
What does a git worktree give a parallel agent?
Why it matters
Two agents in one checkout share one set of files. When one agent switches branches, the other agent's files change under it. When both run the tests, they fight over the same port and the same database. The result is odd failures that no one caused on purpose. A git worktree solves the first half of this: it is a second working folder that shares the same repository history but has its own checked-out branch.
How to do it
- Create one worktree per task, each on a new branch, as a sibling folder outside the main checkout.
- Install what the folder needs. A worktree has tracked files only. Untracked files such as
.envandnode_modulesmust be created again in each one. - Separate the services. Give each worktree its own port and its own database name, set through its own
.env. Otherwise two dev servers will try to bind the same port, and two test runs will wipe each other's rows. - Start the agent inside that folder, so its file paths and git commands only see its own branch.
- Clean up after the merge. Remove the worktree, delete the merged branch, drop the throwaway database and prune stale records.
git worktree add ../shop-feature-search -b agent/search
git worktree add ../shop-feature-export -b agent/export
cp .env ../shop-feature-search/.env
echo "PORT=3101" >> ../shop-feature-search/.env
echo "DB_NAME=shop_search" >> ../shop-feature-search/.env
Before and after
Weak: "I started Claude Code and Codex in the same repo folder, one on search and one on export." Within minutes one agent runs a branch switch and the other reports files that "disappeared". Fixed: two worktrees, two branches, ports 3101 and 3102, databases shop_search and shop_export. Each agent sees only its own work.
Cleanup is just as short:
git worktree remove ../shop-feature-search
git branch -d agent/search
git worktree prune
What goes wrong
- Shared ports and databases. Worktrees isolate files, not processes or data. If both use the same dev database, one agent's migration breaks the other's tests.
- Missing untracked files. The new folder has no
.envand no installed dependencies, so the agent's first test run fails and it may "fix" the wrong thing. - Same branch twice. Git refuses to check out one branch in two worktrees. Do not force it; create a new branch.
- Leftovers. Dozens of old folders and branches pile up, each holding disk and sometimes a running server.
Examples
Two dev servers, one port
Agent A starts the app on port 3000. Agent B, in a second worktree, starts it on 3000 too and exits with an address-in-use error. B then edits the start script to "fix" it. The real fix is a PORT value per worktree, set before either agent starts.
The shared test database
Both agents run integration tests against the database shop_test. A's test truncates the orders table while B's test is reading it, so B sees random failures. A separate DB_NAME per worktree removes the clash.
Key points
- A worktree gives an agent its own folder and branch while sharing the repository history.
- Worktrees isolate files only: ports, databases and other local services need their own settings per worktree.
- Untracked files such as
.envand installed dependencies must be set up again in each new worktree. - Start each agent inside its own worktree so its paths and git commands see only its own branch.
- After the branch merges, remove the worktree, delete the branch, drop the test database and prune.
Common mistakes
- Running two agents in one checkout and hoping they stay out of each other's way.
- Creating worktrees but leaving every service on the same port and database.
- Forgetting that
.envandnode_modulesare not copied into a new worktree. - Never removing finished worktrees and branches.
Check yourself · 2 of 3
Two agents run in separate worktrees, but both use the dev database app_dev. What is the likely problem?
Check yourself · 3 of 3
An agent's merged branch is done. What is the right cleanup?
Practice items, not counted towards your mastery.
Start here
- git-worktree documentation (opens in a new tab)DocsFree
Official reference for add, list, remove and prune: one repository, several working folders.
- Common workflows (Claude Code docs) (opens in a new tab)DocsFree
A vendor's own section on running parallel sessions with worktrees.
- Embracing the parallel coding agent lifestyle (Simon Willison) (opens in a new tab)ArticleFree
A practitioner's account of why review speed, not agent speed, is the limit.