Saltar al contenido

How to run multiple AI coding agents in parallel with git worktrees (without collisions)

One task, one branch, one worktree, one agent. That alone stops two agents from overwriting each other's files. What a worktree doesn't fix on its own is ports, .env files, dependencies and cleanup, and that is what you have to handle by hand or with a tool.

by 4 min readLeer en castellano

Dolphin · Board
Dolphin's board mid-drag: the card “Paginate the orders list” moves from To do to Doing, where other agents are already working (the screenshot is from the Spanish interface).

Everyone's first attempt is two terminals in the same folder and two agents. It works for ten minutes. Then one runs the tests while the other has a file half written, both edit the same package.json, git complains about an index.lock, and you end up with a single branch holding two tasks that nobody can review separately.

The agent isn't the problem: the shared working directory is. Git has had the fix since version 2.5, and it's called a worktree.

What a worktree is

A worktree is another working directory of the same repository, with its own branch checked out. It shares history and objects with the original repo (it's not a clone, .git isn't duplicated), but it has its own files and its own index. What an agent writes in one worktree doesn't exist in the other until it's merged.

terminal
# one folder and one new branch per task
git worktree add ../shop-coupons -b agent/coupons
git worktree add ../shop-csv -b agent/export-csv

# list them
git worktree list

# once the branch is merged
git worktree remove ../shop-coupons
git worktree prune

The rule that works is a single one: one task, one branch, one worktree, one agent session. Start the agent inside each folder (cd ../shop-coupons && claude) and each one works in its own world.

What a worktree doesn't solve

A worktree separates the tracked files. Everything else is still shared, and that is where the weird bugs come from:

  • Ports. Both agents start the app to test it and both want port 3000. The second one fails or, worse, tests against the first one's app and tells you it works.
  • .env files. They aren't in git, so a new worktree starts without them and the app won't boot. You have to copy them.
  • Dependencies. node_modules isn't in git either. Installing in every worktree is slow and eats disk; linking the main repo's copy is fast, but if one task adds a dependency, every task sees it.
  • The database. If two branches run migrations against the same local database, they break each other.
  • Conflicts. They don't go away; they move to merge time. That's better, because git can see them there, but if two tasks touch the same file you want to know early.
  • Cleanup. Worktrees from abandoned tasks keep taking disk space and branch names, and if you delete the folder by hand git remembers it until git worktree prune.

The manual recipe

Without any tool, this is enough for two or three agents:

terminal
git worktree add ../shop-coupons -b agent/coupons
cp .env ../shop-coupons/
cd ../shop-coupons
npm install
PORT=3101 npm run dev &     # a different port per worktree
claude                      # or codex, gemini, opencode…

Keep track of which port you gave each one, tell the agent which it is (otherwise it will test on 3000), and when it finishes review the branch with git diff main...agent/coupons before merging. Past three tasks, keeping that ledger by hand is exactly the kind of work that gets forgotten.

The same thing, without the bookkeeping

This is what Dolphin does for each task: it creates a worktree with its branch, gives it its own port and database, links dependencies and copies the .env files, so two agents can run the app at the same time without clashing. If two are about to touch the same file, it warns you before integrating.

Dolphin · A workspace
A Dolphin workspace panel: its pull request, its issue and two live ports, node on 3104 and postgres on 5434 (the screenshot is from the Spanish interface).
Each workspace shows its pull request, its issue and the ports it has open: here node on 3104 and postgres on 5434.

In the app, every issue you start with Start and every card you drop into Doing opens its own workspace. From the terminal, dolphin cola (queue) works through unrelated tasks, each on its own branch, several at once, and delivers them one at a time; if two collide, the second is re-run on top of the first.

terminal
dolphin cola --tarea "let the cart accept coupons" --tarea "export orders to CSV" --agente claude
dolphin cola --archivo tasks.txt --concurrencia 2

For cleanup, dolphin doctor finds worktrees left behind by an interrupted run, worktrees git remembers without a folder, branches already merged into yours and node_modules links pointing at nothing. It runs on its own when you open a project and only speaks up if it fixed something; --solo-mirar (look only) tells you what it would fix without touching anything.

Before merging: test every branch

Running agents in parallel multiplies the branches you have to review. What saves the most time isn't the parallelism, it's never opening a branch that doesn't compile. In Dolphin every branch goes through the gate before it reaches review: your types, tests and linter first, because they're free, and a review by another model at the end. Whatever fails goes back to the agent with the exact reason.