The first time I ran three AI coding agents on the same repo, they all edited src/main.rs within 30 seconds of each other. Two hours of merge conflict resolution later, I discovered a git feature I'd never used: worktrees.
They solve the multi-agent conflict problem completely, and they've been in git since 2015.
The Problem
If you've ever run more than one AI coding agent on the same project — two Claude Code sessions, or a mix of Claude Code, Codex, and Aider — you've hit this wall:
Agent A is editing src/auth.rs. Agent B is also editing src/auth.rs. Someone loses.
It doesn't matter how smart the agents are. If two processes write to the same file at the same time, one overwrites the other. You end up with broken code, lost work, or a merge conflict that takes longer to resolve than the task itself.
The naive fix is telling agents to work on different files. But you can't always guarantee that. A refactoring that touches the module interface will affect every file that imports it. An agent adding a new API endpoint might need to update the same router file as another agent adding a different endpoint.
You need filesystem-level isolation. Each agent needs its own complete copy of the repo, on its own branch, where it can read, write, and run tests without affecting anything else.
That's exactly what git worktrees are.
What Git Worktrees Are
A git worktree is a linked working copy of your repository. Same .git directory, different filesystem path, different branch.
# Your main repo
~/my-project/ # on branch: main
# Create a worktree for agent-1
git worktree add ./agent-1 -b agent-1/task-42
# Now you have:
~/my-project/ # on branch: main
~/my-project/agent-1/ # on branch: agent-1/task-42
agent-1/ is a complete working copy. It has every file, every directory, the full project structure. But it's on its own branch. Changes in agent-1/ don't appear in the main directory, and vice versa.
The key insight: worktrees share the .git directory. There's no cloning, no duplicate object database, no extra remote connections. Git just checks out a different branch into a different directory. It's fast, and it's cheap on disk.
Basic Commands
Everything you need fits in four commands:
# Create a worktree on a new branch
git worktree add ./agent-1 -b agent-1/task-42
# Create a worktree on an existing branch
git worktree add ./agent-2 feature/api-endpoints
# List all worktrees
git worktree list
# /Users/me/my-project abc1234 [main]
# /Users/me/my-project/agent-1 def5678 [agent-1/task-42]
# /Users/me/my-project/agent-2 ghi9012 [feature/api-endpoints]
# Remove a worktree when you're done
git worktree remove ./agent-1
Each worktree is independent. You can cd into it, run your editor, run tests, make commits — all without touching the other worktrees or the main directory.
Setting Up Worktrees for Multiple Agents
Here's the setup I use for three parallel agents:
#!/bin/bash
# setup-agents.sh — create isolated worktrees for parallel agents
PROJECT_DIR=$(pwd)
WORKTREE_DIR="$PROJECT_DIR/.agents"
mkdir -p "$WORKTREE_DIR"
for i in 1 2 3; do
BRANCH="eng-$i/task-ready"
# Create worktree if it doesn't exist
if [ ! -d "$WORKTREE_DIR/eng-$i" ]; then
git worktree add "$WORKTREE_DIR/eng-$i" -b "$BRANCH"
echo "Created worktree: eng-$i on branch $BRANCH"
else
echo "Worktree eng-$i already exists"
fi
done
echo ""
git worktree list
After running this, your project looks like:
my-project/
├── src/ # main branch
├── .agents/
│ ├── eng-1/ # eng-1/task-ready branch
│ │ └── src/ # complete copy
│ ├── eng-2/ # eng-2/task-ready branch
│ │ └── src/ # complete copy
│ └── eng-3/ # eng-3/task-ready branch
│ └── src/ # complete copy
└── .git/ # shared by all worktrees
Point each agent at its worktree directory. Agent 1 works in .agents/eng-1/, Agent 2 in .agents/eng-2/, Agent 3 in .agents/eng-3/. They can all edit src/main.rs simultaneously — they're editing different copies on different branches.
The Performance Trick Nobody Mentions
The obvious approach is to create a fresh worktree for every task and destroy it when the task is done:
# The obvious (slow) approach
git worktree add ./agent-1 -b agent-1/task-42
# ... agent works ...
git worktree remove ./agent-1
# Next task
git worktree add ./agent-1 -b agent-1/task-43
# ... agent works ...
git worktree remove ./agent-1
Don't do this. Worktree creation copies the entire working tree. On a large repo with node_modules/ or target/ directories, this is slow and wastes disk space. And destruction loses any build cache, compiled artifacts, or debugging context.
Instead: keep persistent worktrees per agent and rotate branches.
# One-time setup (do this once)
git worktree add .agents/eng-1 -b
Tags:
#0
Want to run a more efficient business?
Mewayz gives you CRM, HR, Accounting, Projects & eCommerce — all in one workspace. 14-day free trial, no credit card needed.
Try Mewayz Free →