Lesson 1 of 5 in Many agents at once · 12m

One worktree per agent

After this lesson you can run two or more agents on one project at the same time without their edits, builds or commits colliding, because each one works in its own checkout on its own branch.

Why one folder can't hold two agents

An agent treats the folder it works in as its own. It edits a file, runs the build, reads the failures and edits again. That loop assumes nothing else is changing the files between steps.

Put a second agent in the same folder and the assumption breaks in ways that are hard to see:

  • Edits collide. Both agents touch the same file. One rewrites a function the other is halfway through changing, and the second agent's next edit is based on text that is no longer there.

  • Builds lie. Agent A runs the tests while agent B is mid-edit. A sees failures it didn't cause and starts "fixing" B's unfinished work.

  • Commits mix. Agent A runs git add . and commits B's half-done change under A's message. Now neither branch tells a clean story.

  • Nobody can tell whose change is whose. When something breaks, you can't revert one agent's work without reverting the other's.

The fix isn't for the agents to be more careful. It's to give each one a separate checkout, so the only shared thing is the git history they both start from.

The rule: one agent, one branch, one checkout

A git worktree is a second checkout of the same repository, in its own folder, on its own branch. It shares the .git directory, so it's cheap to make. The Worktrees lesson covers the mechanics. This lesson is about using them as the unit of parallel work.

Hold to three rules:

  • Every agent gets its own worktree. The primary checkout is yours. Agents work beside it, never in it.

  • Every worktree gets one task. A branch named agent/search-filter that also fixes the login bug is two tasks, and you'll review them as one.

  • The branch is the deliverable. An agent is done when its branch is committed and ready to review, not when it says it's done.

Split the work so the branches don't fight

Separate checkouts stop agents from trampling each other while they work. They don't stop the merge from being painful later. If two agents both rewrite router.ts, you'll resolve that conflict by hand.

So split by area, not just by task. Before you start two agents, ask which files each task will touch. If the answer overlaps heavily, run them one after the other instead, or make one depend on the other. A good parallel pair looks like this:

Agent | Branch | Touches |

A | agent/search-filter | src/search/, its tests |

B | agent/export-csv | src/export/, its tests, one menu entry |

They share one menu file, and that's a one-line conflict at worst. Two agents both "improving error handling" across the codebase would collide everywhere.

Mind the things a checkout doesn't separate

A worktree separates files. It doesn't separate everything else on your Mac:

  • Ports. Two dev servers both want port 3000. Give each worktree its own port, or let only one run a server at a time.

  • Shared services. Two agents running migrations against one local database will fight over it. Use separate databases or keep one agent off it.

  • Global installs. An agent that runs brew install or edits a global config changes things for every checkout.

Put these constraints in each agent's brief. "Run the dev server on port 3101" is a one-line instruction that saves an hour of confusion.

A worked example

You want a search filter and a CSV export, and you have an afternoon.

  • You create two worktrees, agent/search-filter and agent/export-csv, both branching from main.

  • You open a tab in each and start Claude Code there, with a brief that names the folder it owns, the port to use and the test command that defines done.

  • While both run, you stay in the primary checkout, free to read code or review.

  • The search filter finishes first. You read its diff, open a pull request from its worktree and merge it.

  • The export branch now trails main by one merge. You ask its agent to rebase on main and rerun the tests before you review it.

  • You merge the second pull request and remove both worktrees.

Neither agent ever saw the other's half-finished files.

Go further
  • Worktrees — creating, switching and cleaning up worktrees
  • Claude Code, Codex and Gemini — running a coding CLI in a pane and giving it TRMNLY's tools
  • Worktrees reference — tab scoping, the worktree_* tools and worktrees over SSH
  • MCP server — how a client's folder picks its project and worktree
  • Race and pick — several agents, one task, a worktree each

Keep going with Many agents at once

This is one of 5 lessons in the course. Enroll to get the rest of the course, with an AI tutor that knows the material, flashcards and a certificate when you finish.

Enroll for free

Next free lesson: Race and pick

More free lessons from this course