Run Two AI Agents at Once with Git Worktrees
Point two AI coding agents at the same project folder and watch them destroy each other's work: agent A edits a file, agent B overwrites it mid-task, one of them switches branches and yanks the floor out from under the other. The fix isn't a second clone — it's a git feature most developers have never used: worktrees.
What a worktree is
A worktree is an extra working directory attached to a repository you already have. One repo, multiple directories, each checked out on its own branch — and they all share the same .git object store underneath. It's been built into git since version 2.5, released in 2015, so it's already on your machine.
Compare that to a second clone: a clone duplicates the entire history on disk, and the two copies only see each other's commits after a push and a fetch. Worktrees share everything instantly because there's only one repository. A commit made in any worktree is immediately visible from every other one. The single rule git enforces: no two worktrees can have the same branch checked out at once — which is exactly the isolation you want.
The two-agent setup
The pattern that pays off immediately: agent 1 builds the feature in your main worktree, on the feature branch, in your normal project directory. Agent 2 writes the tests in a second worktree, on its own branch, in a sibling directory.
Each agent has its own files, its own checkout, its own branch — they physically cannot clobber each other. But because both worktrees hang off the same repo, the moment agent 1 commits, agent 2 can see that commit and write tests against the real implementation. No push, no pull, no remote round-trip. When both are done, you merge two branches like any other day.
How to run it
# from your main project directory: create a sibling
# worktree on a new branch called "tests"
git worktree add -b tests ../myapp-tests
# point agent 2 at ../myapp-tests, agent 1 stays here
# when the work is merged, clean up
git worktree remove ../myapp-tests
That's the entire lifecycle. git worktree list shows every active worktree if you lose track, and removing a worktree never deletes the branch or its commits — only the extra directory.
Power tricks
- Disposable experiment worktrees. Want an agent to try a risky refactor? Give it a throwaway worktree on a scratch branch. If the experiment fails,
git worktree removeand the mess never touched your working directory. - Adversarial pairing. Have one agent write failing tests for a bug in one worktree, and the other agent make them pass in the main worktree. Because commits are shared instantly, the fix-verify loop is tight — and neither agent can "fix" the test to avoid fixing the bug.
Resources
Building an AI feature? Yeda AI designs, audits, and ships production LLM systems.