Git worktrees and AI agents: the workflow that scales
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-authchangeslogin()to require an options object. - Agent B in
task-reportscallslogin(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
Name branches by task, not by agent.
agent/auth-refactoris better thanclaude-1. When you have 5 worktrees, you need to know what each one is doing.Keep worktrees short-lived. A worktree that runs for a week accumulates drift from main. Merge frequently or rebase.
Do not share the git index. Each worktree has its own index, but they share refs. Never run
git stashfrom a worktree — it goes to the shared stash stack. Use commits instead.Watch for lockfile conflicts. If two agents run
npm installwith different additions, the lockfiles will conflict on merge. This is R8 in Lanekeeper's rules — it warns you before you get there.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:
- Plan the tasks so they touch different areas of the codebase.
- Create a worktree per task.
- Run a tool like Lanekeeper to watch for collisions.
- Merge the smallest, most contained branches first.
- 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.
原文:https://www.a8q9.cn/en/article/git-worktrees-and-ai-agents-the-workflow-that-scales-f4a131.html