Git Worktrees for AI Agents: Run Multiple Coding Agents Without Conflicts

Git Worktrees for AI Agents: Run Multiple Coding Agents Without Conflicts

You ask one AI agent to rebuild the settings page and another to fix the login bug. Both edit in the same folder, one overwrites the other’s half-finished work, and you spend the evening untangling a conflict neither agent understood. Git already has the fix: a worktree gives every agent its own folder and branch, with no way to step on anyone else’s files.

Table of Contents (9)
  1. The problem: two agents, one folder, one mess
  2. What a git worktree is
  3. Step 1: Create a worktree for each agent
  4. Step 2: Point each agent at its own worktree
  5. Step 3: Merge the work back
  6. Step 4: Clean up when you are done
  7. Rules that keep worktrees safe
  8. Frequently asked questions
  9. The bottom line

The problem: two agents, one folder, one mess

An AI coding agent edits files in the folder you point it at. Two agents in the same folder share everything: the same files, the same branch, the same half-written code. When both change the same file, the second save wins and the first agent’s work silently disappears. Worse, each agent sees the other’s unfinished edits and starts “fixing” them, creating changes nobody asked for.

Running agents one after another is slow, and copying the project folder by hand breaks the Git connection. A worktree solves it properly: same repository, separate folders, separate branches.

What a git worktree is

A worktree is an extra working folder attached to the same repository. Your main folder stays on your main branch. Each linked worktree checks out a different branch in its own folder, with its own files. They all share one .git directory, so commits in any worktree belong to the same repository and the same history.

Think of one library with several desks: each desk has its own copy of the book, and an agent at one desk cannot scribble in another’s copy. One hard rule from Git’s docs: the same branch cannot be checked out in two worktrees at once. That is the protection — Git itself refuses the mistake that causes the conflict.

Step 1: Create a worktree for each agent

Make sure your working tree is clean (git status shows nothing to commit), then create one worktree per task. The simplest form creates a new branch named after the folder:

git worktree add ../settings-rebuild

This creates the folder and a new branch called settings-rebuild, and checks it out there. To name the branch yourself, use -b:

git worktree add -b fix-login ../fix-login

To put an existing branch in a new worktree instead:

git worktree add ../review-branch review-branch

Check with git worktree list — the main worktree is listed first, then each linked worktree. Keep worktrees next to your project, not inside it, or a folder could get committed by accident.

Step 2: Point each agent at its own worktree

Give each agent its own folder and job in plain words: Agent 1 works in ../settings-rebuild rebuilding the settings page; Agent 2 works in ../fix-login fixing the login bug. Tell each one not to touch any other folder.

Because the folders are separate, the agents cannot overwrite each other — even if both edit a file with the same name, they are editing different copies. Each agent sees only its own branch’s history.

Two practical notes. First, dependencies are not shared: each worktree needs its own install of node_modules or a virtual environment, so one agent’s dependency change cannot break the other agent’s work. Second, secrets are not shared either: a .env file does not appear in a new worktree, because Git does not track it — copy it in yourself, and never commit it.

Step 3: Merge the work back

When an agent finishes, review its work in its own worktree first — run the tests, read the diff — then bring it into your main branch:

git checkout main
git merge settings-rebuild

Or push the branch and open a pull request, the safer route for big work: it gives you a review page and a clear record of what the agent did. If two agents changed the same lines, you will get a merge conflict — but once, at merge time, with both pieces finished and visible.

Step 4: Clean up when you are done

A finished worktree should not linger. Remove it with:

git worktree remove ../settings-rebuild

Git refuses to remove a worktree with uncommitted changes — a safety feature. If you are sure you want to throw the work away, add --force. The main worktree cannot be removed this way. If you delete a folder by hand, clean up with git worktree prune; if a worktree lives on a portable drive, git worktree lock stops Git pruning its records while the drive is away.

Rules that keep worktrees safe

  • One agent, one worktree, one branch. Never point two agents at the same folder or the same branch.
  • Start clean. Commit or stash your work in the main folder before creating worktrees, so each agent starts from a known state.
  • Review before you merge. Run the tests in its worktree, read the diff, then merge.
  • Keep worktrees short-lived. Create it, use it, merge it, remove it — long-lived ones drift and merge badly.
  • Mind the disk. Each worktree is a full copy of your files plus its own dependencies.
  • Know the limits. Multiple checkouts are still experimental and submodule support is incomplete, so test worktrees on a small task first if your project uses submodules.

Frequently asked questions

What is a git worktree?
An extra working folder attached to the same Git repository. Each worktree checks out its own branch in its own folder, while all share one repository and one history.

Can two worktrees use the same branch?
No. Git refuses to check out the same branch in two worktrees — which is exactly the rule that stops two AI agents from overwriting each other’s work.

Do worktrees share node_modules and dependencies?
No. Each worktree has its own files, so each needs its own npm install or virtual environment, and its own copy of untracked files such as .env.

How do I delete a worktree?
Run git worktree remove <path>. It only works on a clean worktree; add --force for one with uncommitted changes. If you deleted the folder by hand, run git worktree prune.

The bottom line

Worktrees are the simplest way to run several AI coding agents without them fighting over your files. One command creates a separate folder and branch per agent, Git enforces the separation, and merging brings the finished work together for review. Start with two agents on two small tasks.

Sources:
– https://git-scm.com/docs/git-worktree/
– https://www.man7.org/linux/man-pages/man1/git-worktree.1.html
– https://git-scm.com/docs/git-branch/
– https://git-scm.com/docs/git-merge/