If you are running more than one AI coding agent at a time, git worktrees are the foundation. Here is the setup and the one problem nobody warns you about.

Why worktrees

A worktree is a second (or third, or fifth) checkout of the same repository, sharing history but with its own working directory and branch. Each agent gets its own:

git worktree add ../feature-auth -b auth-refactor git worktree add ../feature-reports -b weekly-reports git worktree add ../feature-billing -b billing-migration 

Now three agents can work at once without stepping on each other's uncommitted changes. Each one sees its own files, runs its own tests, and commits to its own branch.

The setup that works

Here is a step-by-step for running Claude Code, Cursor, or Codex in parallel worktrees:

1. Create the worktrees from your main checkout:

# From your main repo directory git worktree add ../task-auth -b agent/auth git worktree add ../task-reports -b agent/reports 

2. Open each worktree in a separate editor or terminal. Claude Code and Codex work from the terminal; Cursor opens the worktree as a folder.

3. Each agent works independently. It edits files, runs tests, commits, all in its own directory. The agents do not share uncommitted state.

4. When an agent is done, merge its branch:

git checkout main git merge agent/auth git merge agent/reports 

5. Clean up:

git worktree remove ../task-auth git worktree remove ../task-reports 

The problem: they can still break each other

Worktrees solve the "two agents editing the same file at the same time" problem. They do not solve the "two agents making incompatible changes to different files" problem.

Classic example:

  • Agent A in task-auth changes login() to require an options object.
  • Agent B in task-reports calls login(email, password) — the old signature.
  • Both branches pass their own tests. Git merges them without a conflict.
  • Main is broken.

Git does not know that these two changes are related. It compares lines, not function signatures. CI tests each branch alone. Neither agent can see the other's worktree.

How to catch this

Manual approach: before merging, do a trial merge:

git merge-tree $(git merge-base agent/auth agent/reports) agent/auth agent/reports 

This shows what git would produce without actually merging. If it reports conflicts, you have text-level collisions. But it will not catch semantic conflicts like the signature break above.

Automated approach: Lanekeeper watches all your worktrees and compares the changes as code, not text. When one agent changes a function's signature while another calls the old form, Lanekeeper tells that agent during the edit — before either branch merges.

npm i -g lanekeeper cd your-repo lk init 

After that, every worktree is a "lane" and the daemon compares them in real time. Claude Code gets clash warnings through hooks; Cursor and Codex get them through hooks and an MCP server.

Tips for the worktree workflow

  1. Name branches by task, not by agent. agent/auth-refactor is better than claude-1. When you have 5 worktrees, you need to know what each one is doing.

  2. Keep worktrees short-lived. A worktree that runs for a week accumulates drift from main. Merge frequently or rebase.

  3. Do not share the git index. Each worktree has its own index, but they share refs. Never run git stash from a worktree — it goes to the shared stash stack. Use commits instead.

  4. Watch for lockfile conflicts. If two agents run npm install with different additions, the lockfiles will conflict on merge. This is R8 in Lanekeeper's rules — it warns you before you get there.

  5. Test the merge, not just each branch. Either trial-merge by hand or let Lanekeeper's GitHub Action test PR pairs together (R10).

The real workflow

The developers who run the most agents in parallel tend to settle on this:

  1. Plan the tasks so they touch different areas of the codebase.
  2. Create a worktree per task.
  3. Run a tool like Lanekeeper to watch for collisions.
  4. Merge the smallest, most contained branches first.
  5. Rebase the remaining branches onto the updated main.

This works for 2 agents and for 10. The bottleneck is never git — it is knowing what the other agents changed.

Lanekeeper is free and open source. Install it in 30 seconds with npm i -g lanekeeper && lk init. It runs locally and your code never leaves your machine.

Originally published at lanekeeper.dev.