Run Multiple AI Coding Agents on One Repo Without Collisions (git worktree)
DEV Community

Run Multiple AI Coding Agents on One Repo Without Collisions (git worktree)

Running two AI coding agents on the same repository? Then they're probably mixing up each other's changes.

The fix is a git feature that has existed since Git 2.5 (2015) and that few people used until AI agents arrived: git worktree. Everything below was tested with git 2.50, on a small demo repo and on the real express repository.

Video version:

The problem: one folder = one working directory

Agent A is halfway through a login fix in index.js. Agent B starts its own task in the same folder:

# agent A has an uncommitted change in index.js…
git checkout -b agentB
echo "// B: search" >> index.js
git commit -am "B: search"
git show HEAD -- index.js
module.exports = require('./lib/express');
+// A: login fix← agent A's unfinished work!
+// B: search

Switching branches keeps uncommitted changes, so agent B's commit now contains agent A's half-done work.

It gets worse when both agents edit the same file at the same moment, or when one runs git stash or git checkout . and wipes out the other's changes.

The fix: git worktree add

git worktree add ../shop-login -b feat/login
git worktree add ../shop-search -b feat/search

Each command creates a new folder with its own files and its own branch, connected to the same repository:

$ git worktree list
~/code/shop 71d6cb4 [main]
~/code/shop-login 71d6cb4 [feat/login]
~/code/shop-search 71d6cb4 [feat/search]

Open one agent in each folder. They can edit, run tests and commit at the same time without touching each other's files.

cd ../shop-login && claude # or Codex, Copilot CLI, Aider…

Inside a worktree, .git is just a small file pointing back to the main repository:

$ cat ../shop-login/.git
gitdir: ~/code/shop/.git/worktrees/shop-login

Why not just clone the repo again?

Because worktrees share the repository: history, branches and objects. On express (6,186 commits):

Disk
Second git clone 13 MB
git worktree add 1.3 MB

And because it's one repository, a commit made in one worktree is instantly visible from all the others. I committed in the agent's worktree, and git log agent1 in the main folder showed it right away, with no push or pull between copies.

Git's built-in safety checks

A branch can only be checked out in one worktree at a time:

$ git checkout feat/login
fatal: 'feat/login' is already used by worktree at '~/code/shop-login'

And git won't delete a worktree with unsaved work:

$ git worktree remove ../shop-login
fatal: '../shop-login' contains modified or untracked files, use --force to delete it

Gotchas

  • Untracked files aren't copied. A new worktree has only tracked files, so .env, node_modules and build output are missing. Run npm install and copy your .env into each worktree.
  • Ports: if each agent starts a dev server, give each one a different port.
  • Merge conflicts don't disappear. If two tasks change the same files, you'll resolve conflicts when you merge, just later.
  • Review still matters. Worktrees isolate the work; they don't check it. Review each branch before merging.

Cheat sheet

Command What it does
git worktree add ../dir -b branch New folder + new branch
git worktree add ../dir branch New folder for an existing branch
git worktree list Show all worktrees
git worktree remove ../dir Delete a worktree (after merging)
git worktree prune Clean up worktrees whose folders you deleted by hand

Some AI tools can create worktrees for you. For example, Claude Code has claude --worktree [name], which starts a session in a fresh worktree.

How many AI agents do you run at once? πŸ‘‡ I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.