← back to blog
Git Worktrees for AI Coding Agents: The Complete Guide

Git Worktrees for AI Coding Agents: The Complete Guide

Parsa Khazaeepoul

by Parsa Khazaeepoul

Published June 25, 2026

If you have ever run two AI coding agents on the same project and watched them silently overwrite each other's work, you already know the problem. The fix is a git feature called worktrees, and once you understand how they work, the entire multi-agent workflow clicks into place.

- - - - - - - - - - - - - - - -

what a worktree is

Normally, a git repository has one working directory. That is the folder where your files live, where you edit code, where your editor is pointed. When you check out a branch, the files in that folder change to match the branch.

A worktree is a second (or third, or tenth) working directory for the same repository. Each worktree is checked out to a different branch, has its own set of files on disk, and shares the same git history. Changes in one worktree do not affect files in another.

git worktree add ../my-project-feature-x -b feature-x

That command creates a new directory at ../my-project-feature-x, checks out a new branch called feature-x, and links it back to your original repository's .git directory. Both directories share the same commit history, remote configuration, and hooks. But their files are completely independent.

- - - - - - - - - - - - - - - -

why agents need them

AI coding agents work by reading files, making changes, and writing those changes back. If two agents are pointed at the same directory, they are editing the same files. Agent A modifies auth.ts. Agent B modifies auth.ts. One of them overwrites the other. There is no locking mechanism. There is no conflict resolution. The last write wins.

Separate branches do not help here. Branches control which version of files you see when you check them out. But if both agents are operating in the same directory, they both see the same files on disk regardless of which branch they think they are on. You need separate directories.

Worktrees give each agent its own directory, its own branch, its own set of files. Agent A works in ../project-auth-refactor. Agent B works in ../project-billing-tests. They cannot interfere with each other. Their diffs are clean and reviewable independently.

- - - - - - - - - - - - - - - -

the lifecycle

A typical worktree-per-agent session goes through four steps:

This is straightforward with one agent. With three or five running simultaneously, the overhead adds up. You are creating worktrees, naming branches, copying env files, tracking which terminal has which worktree, reviewing diffs across multiple directories, and cleaning up when you are done. That is why agent managers exist: they automate this lifecycle so you can focus on the review step, which is the part that actually requires judgment.

- - - - - - - - - - - - - - - -

worktrees on windows

Git worktrees work on Windows, but there are a few things to know:

- - - - - - - - - - - - - - - -

automating worktrees with pane

Pane makes worktrees invisible. When you create a pane for a new task, Pane creates a worktree, checks out a branch, copies your environment files, and starts the agent you picked. When you close the pane, the worktree is cleaned up. You never run git worktree add or git worktree remove yourself.

This is not unique to Pane. Superset, Conductor, and Claude Squad all use worktrees for isolation. The differences are in platform support, agent compatibility, and workflow design. Pane is the only one that runs on Windows, Mac, and Linux and supports any CLI agent. See the worktree tools comparison for a full breakdown.

- - - - - - - - - - - - - - - -

common patterns

For the practical walkthrough of running multiple agents in parallel, see How to Run Multiple AI Agents in Parallel. For the Windows-specific setup, see the Windows setup guide.