Git Worktrees for AI Agents: No More Merge Conflicts
How git worktrees give AI agents isolated branches with no merge conflicts. The exact commands, Claude Code flags, and review workflow inside.
Git worktrees stop parallel AI agents from corrupting each other’s work by giving every agent its own directory and its own branch, all attached to one repository. Run git worktree add ../feature-x -b feature-x, point an agent at the new folder, and nothing it does can touch your main checkout until you decide to merge.
TL;DR: A git worktree is an extra working directory that shares your repo’s object store, so it costs almost nothing compared to a clone. Give each AI agent its own worktree on its own branch and mid-flight collisions disappear; conflicts only surface at merge time, one branch at a time. Claude Code automates the pattern with its --worktree flag, and tools like Pivio put the whole create, monitor, merge, cleanup loop in a GUI.
This guide covers the collision problem, the exact commands you need, how worktrees pair with Claude Code, and how to review and merge what your agents produce.
The Problem: Agents Share One Working Tree
A standard Git checkout gives you a single working tree, one set of files on disk tied to one branch (HEAD). That model is fine for one human typing in one editor. It breaks down the moment you run several agents in parallel.
When two AI agents operate on the same checkout:
- They both write to the same files, so one agent’s edits clobber the other’s.
- A
git checkoutorgit switchby one agent silently changes the branch the other agent thinks it’s on. - Staged changes, stashes, and the index become shared mutable state that nobody fully controls.
- Your test runner, dev server, and database all point at one mutating snapshot of the code.
You can serialize agents to dodge this, but then you’ve thrown away the speed that made parallel agents appealing in the first place. Worktrees let you keep the parallelism and remove the collisions.
What Is a Git Worktree?
A git worktree is an additional working directory attached to the same repository. Each worktree has its own checked-out branch, its own files on disk, and its own HEAD, but they all share a single .git object store, history, and remotes.
That sharing is the key benefit. You’re not cloning the repo five times and re-downloading gigabytes of history. You get five independent working directories backed by one set of objects. Commits made in any worktree are immediately visible to all of them because the underlying database is the same.
For AI agents, this is the ideal isolation boundary: each agent gets a real directory with a real branch it can edit freely, while the shared history makes reviewing and merging trivial.
Worktree vs. clone at a glance
| Extra worktree | Separate clone | |
|---|---|---|
| Disk usage | Shares one object store; only checked-out files are duplicated | Duplicates the entire repository history per copy |
| Branch visibility | Commits in any worktree are immediately visible to all of them | Commits must be pushed and fetched between copies |
| Setup speed | One local command, no network | A full clone every time |
The Core Commands
Here are the commands you’ll actually use. Run them from inside your repository.
Create a worktree on a new branch
# Create ../feature-x as a new worktree and a new branch named feature-x
git worktree add ../feature-x -b feature-x
This creates a sibling directory ../feature-x, checks out a brand-new branch feature-x in it, and leaves your main working tree untouched. Point one agent at that directory and it has the whole repo to itself.
To base the worktree on an existing branch instead of creating one:
# Check out an existing branch into a new worktree
git worktree add ../hotfix hotfix-1234
List your worktrees
git worktree list
Sample output:
/Users/you/code/app a1b2c3d [main]
/Users/you/code/feature-x e4f5a6b [feature-x]
/Users/you/code/hotfix 9c8d7e6 [hotfix-1234]
Each line shows the path, the checked-out commit, and the branch. This is your map of which agent should be working where.
Remove a worktree when you’re done
# Remove the worktree directory and its administrative files
git worktree remove ../feature-x
If the worktree has uncommitted changes you intend to discard, add --force. After the branch is merged you can delete it normally with git branch -d feature-x.
Prune stale references
If a worktree directory was deleted manually (for example by an agent’s cleanup step), Git can be left with dangling metadata. Clean it up with:
git worktree prune
This removes administrative entries for worktrees whose directories no longer exist. It’s safe to run anytime and a good periodic housekeeping step.
Per-Worktree Concerns: Don’t Forget the Environment
A worktree isolates your source files, but it does not automatically isolate everything an application needs to run. Plan for these before you turn agents loose:
- Dependencies.
node_modules,.venv,target/, and similar build artifacts live in each worktree directory. A fresh worktree needs its ownnpm install(orpnpm install,pip install,cargo build). Tools like pnpm with a global store make this cheap. - Ports. If every agent runs a dev server on
localhost:3000, they’ll collide. Assign a port range or read the port from an environment variable per worktree. - Databases. Two agents running migrations against the same database will corrupt each other’s state. Give each worktree its own database, schema, or a disposable container.
- Env files. Copy or template
.envinto each worktree so secrets and config travel with the directory.
A small setup script per worktree saves a lot of pain:
git worktree add ../feature-x -b feature-x
cd ../feature-x
cp ../app/.env .env
npm install
PORT=3100 npm run dev
Claude Code and Worktrees
Claude Code is worktree-aware, which is what makes this pattern smooth for AI-driven development.
You can launch Claude Code so that it operates inside an isolated worktree rather than your primary checkout. The --worktree flag (short form -w) creates a fresh worktree for the session, branched from your default branch, and cleans it up automatically when it’s no longer needed. Pass an optional name to label the worktree, and add --tmux (which requires --worktree) if you want the session opened in its own tmux pane.
# Run Claude Code in a new, isolated worktree for this session
claude --worktree
# Same thing, with a name for the worktree
claude -w feature-auth
You can also push isolation down to the subagent level. In a subagent’s frontmatter, declaring worktree isolation tells Claude Code to spin that subagent up in its own worktree:
---
name: refactor-auth
description: Refactor the authentication module
isolation: worktree
---
With isolation: worktree, the subagent edits its own branch in its own directory, branched from your default branch. Your main agent keeps working in parallel, neither steps on the other, and Claude Code prunes the worktree automatically if the subagent makes no changes. For a broader look at orchestrating several agents at once, see our guide to running multiple AI agents in parallel.
The Review and Merge Flow
The payoff of worktrees is a clean review step. Because every agent committed to its own branch, you review each branch independently, then merge the good ones.
# See what an agent changed on its branch
git log --oneline main..feature-x
git diff main...feature-x
When you’re satisfied, merge it back:
git switch main
git merge --no-ff feature-x
Then retire the worktree and branch:
git worktree remove ../feature-x
git branch -d feature-x
Because the branches were isolated the whole time, conflicts only appear at merge time, where Git surfaces them explicitly and you resolve them on your terms, one branch at a time. There’s no mid-flight clobbering, and no mystery about which agent produced which change. For more habits that keep agent-driven sessions tidy, check our Claude Code workflow tips.
Worktrees Without the Bookkeeping
The commands above are simple. Doing them correctly across five live branches while five agents keep moving is the actual work: env files, installs, merges, cleanup.
Pivio turns that loop into a panel. Its Worktrees view creates a branch and worktree in one step and opens it in a workspace of one to six agent panes, with dependency installs a click away. Each worktree row shows live status: which agents run in it, how far ahead or behind main it is, dirty files, open pull requests. From the same panel you can pull the latest from main into a branch, merge locally, or open a PR through gh. If a merge leaves conflicts, one click hands the worktree to an agent that resolves and stages them, and removing a worktree detaches its panes safely.
If you’re already using worktrees with Claude Code, get Pivio free, no account required. It’s free to download right now.
Frequently Asked Questions
Do git worktrees share node_modules and dependencies?
No. Each worktree is a separate directory, so node_modules, .venv, and target/ are not shared. A fresh worktree needs its own install (npm install, pip install, cargo build). A package manager with a global store, like pnpm, makes those extra installs cheap.
Can two worktrees check out the same branch?
No. Git refuses to check out the same branch in more than one worktree, which is exactly what stops two agents from quietly committing to the same branch. Give each worktree its own branch with git worktree add ../dir -b branch-name.
How do git worktrees prevent AI agents from causing merge conflicts?
By isolating where conflicts can happen. Each agent edits its own branch in its own directory, so nothing collides mid-flight. Real conflicts still exist, but they surface once, at merge time, when you bring a branch back into main and resolve them deliberately.
How is a git worktree different from a git clone?
A clone is a full, independent copy with its own object database and remotes. A worktree is an extra working directory attached to the same repository, sharing one history and one set of remotes. Commits in any worktree are instantly visible to the others, which makes reviewing and merging between them trivial.
Do worktrees use a lot of extra disk space?
Far less than cloning. All worktrees share one .git object store, so you only duplicate the checked-out files, not the repository history. Five worktrees are five working directories backed by a single set of objects.
Can Claude Code create worktrees for me?
Yes. claude --worktree (or -w) starts the session in a fresh worktree branched from your default branch and cleans it up automatically, and a subagent can declare isolation: worktree in its frontmatter to get its own sandbox. There’s also a --tmux flag, which requires --worktree, for opening the session in a tmux pane. Details are in the Claude Code worktree docs.