DEV Community

Cover image for Git Worktrees for AI Agents
Uray Febri
Uray Febri

Posted on Originally published at raylabs.app

Git Worktrees for AI Agents

Running multiple AI coding agents against a single repository checkout creates file race conditions. Even when you instruct each agent to check out a separate branch, they often write to the same working directory if you rely on a single local clone. Copying entire repository folders avoids these collisions, but it wastes disk space and breaks Git-native coordination.

Using Git worktrees solves this problem by allowing multiple working directories to attach to a single underlying repository database. Each agent gets its own directory and branch while sharing the same local history and object store.

Why branches alone do not isolate agents

Git branches are simply pointers to specific commits. When you change branches in a standard Git repository, you switch the active state of the working directory. If you run two automated coding agents in the same repository folder simultaneously, Agent A might overwrite a file that Agent B is currently inspecting or modifying.

This leads to silent overwrites, corrupted build states, and failed test runs that have nothing to do with logical code conflicts. Separate branch names only protect commit history, not active filesystem state.

What Git worktrees actually share

A Git worktree links an additional working directory to your main repository without duplicating the entire object database. The main repository and all associated worktrees share the same .git metadata store, object database, and reference logs.

Consider an Android development project where you want to run two agents on different features. Instead of cloning the repository twice, you create dedicated worktrees using the command line:

git worktree add ../agent-alpha-dir feature/alpha-branch
git worktree add ../agent-beta-dir feature/beta-branch
Enter fullscreen mode Exit fullscreen mode

Each agent now operates inside its own folder. Agent Alpha can modify app/src/main/java/MainActivity.kt without altering the files visible to Agent Beta in the beta directory.

One agent per worktree

To maintain strict isolation, establish a rule where every concurrent agent owns exactly one worktree and one branch. No two processes should ever share a working tree path.

When Agent Alpha finishes its task, it commits its changes inside its assigned directory. Because the worktree shares the repository object database, those commits are immediately available to your main repository or to a review pipeline without requiring file copying or network pushes.

Merging results safely

Isolation protects the filesystem during execution, but integration still requires explicit coordination. Once your parallel agents finish their tasks, bring their branches back together through standard merge or rebase workflows.

git checkout main
git merge feature/alpha-branch
git merge feature/beta-branch
Enter fullscreen mode Exit fullscreen mode

If both agents modified overlapping sections of the same file, Git will flag a normal merge conflict. This gives you a clear checkpoint to resolve differences manually or pass the conflict details back to an automated review routine.

Cleanup and recovery

Once an agent finishes its work and its branch is merged, leaving dead worktrees on disk clutters storage and confuses future automation scripts. Prune and remove worktrees cleanly after agent termination:

git worktree remove ../agent-alpha-dir
git worktree prune
Enter fullscreen mode Exit fullscreen mode

This command safely deletes the extra working directory while preserving all committed history in the main repository database. By keeping your execution environment modular, you can scale automated development tasks without risking filesystem corruption.

Top comments (1)

Collapse
 
aifrontierpost profile image
AI Frontier Post •

The claim that "separate branch names only protect commit history, not active filesystem state" cuts both ways: because worktrees share the object database and reflogs, an agent running git branch -D or a hard reset in its own directory mutates state every sibling worktree observes. The underrated failure mode is two agents racing to create the same branch name, since Git refuses to check out one branch in two worktrees simultaneously — so branch naming needs the same agent-id prefixing discipline as the one-agent-per-worktree rule.