Home › Articles

Parallel agents with git worktrees in Claude Code, Codex and Cursor

Updated 2026-10-22

In short: two agents in one checkout overwrite each other's work. Give each agent its own git worktree: a separate folder on its own branch that shares one history. Claude Code, Codex and Cursor can all create worktrees for you. What they don't do is make a fresh checkout runnable or decide how the work fits back together, so set up .env and dependencies per worktree, split tasks so they touch different files, and run the full test suite on the merged result.

Why parallel agents in one folder go wrong

An agent that edits files, runs the build and reads the test output assumes nobody else is changing the folder. Run a second agent in the same checkout and each one sees the other's half-finished edits. Tests fail for reasons neither agent caused, and one agent "fixes" the other's work. Separate terminals or chat tabs don't help, because the files are still shared.

A git worktree fixes the file problem. In the Codex docs' words, each worktree "has its own copy of every file in your repo but they all share the same metadata (.git folder) about commits, branches, etc." Commits made in one worktree are visible from the others straight away, because they live in the same repository. The working files are not shared.

What a worktree does and doesn't isolate

Separate per worktreeStill shared
Tracked files and uncommitted editsCommits, branches and remotes (one .git)
The checked-out branch: git allows a branch in only one worktree at a timeAnything outside the folder: ports, local databases, caches, services
Build output and installed dependencies, once you install them thereNothing in the repo is copied that git ignores, so .env and node_modules start missing

Two things on the right trip people up in the first week:

Claude Code: claude --worktree

From the worktrees guide: pass --worktree (or -w) with a name, and Claude Code creates the worktree under .claude/worktrees/<name>/ on a new branch called worktree-<name>. Run the same command with another name in a second terminal for a second isolated session. The repository needs at least one commit first (common workflows).

# terminal 1
claude --worktree feature-auth

# terminal 2
claude --worktree fix-billing-rounding

# start from a pull request instead (quote the #)
claude --worktree "#1234"

Set it up once per repo:

# .worktreeinclude
.env
.env.local
config/secrets.json

When you exit, Claude checks the worktree. If it's clean, an unnamed session's worktree and branch are removed automatically, and a named session asks first. If it has changes or new commits, Claude asks whether to keep or remove it. Keep it and you can come back with the claude --worktree <name> --resume command Claude Code prints on exit. Non-interactive -p runs aren't cleaned up, so remove those with git worktree remove.

Inside one session you can also isolate subagents. Ask Claude to "use worktrees for your agents", or add isolation: worktree to a custom subagent's frontmatter in .claude/agents/. Each subagent gets a temporary worktree, removed automatically if it finishes without changes. One detail from the docs: such a subagent takes its instruction files from the main conversation, not from its worktree, so a CLAUDE.md that differs on the worktree's branch doesn't apply to it.

Some things carry across worktrees of the same repository: on most setups, "Yes, and don't ask again" Bash approvals are saved to the main checkout's .claude/settings.local.json, and project-scope plugins load in every worktree. While a session is isolated, Claude Code blocks file edits and git commands aimed at the main checkout.

Codex: Worktree mode in the ChatGPT desktop app

The Codex worktrees docs describe this for Codex in the ChatGPT desktop app (and the mobile app controlling a connected computer):

  1. In a new chat, select Worktree under the composer, and optionally a local environment to run setup scripts.
  2. Choose the starting branch: main, a feature branch, or your current branch with unstaged changes, which Codex applies to the worktree.
  3. Submit the prompt. Codex creates the worktree in $CODEX_HOME/worktrees in a detached HEAD state, so several worktrees don't create a pile of branches.
  4. When it's done, either click Create branch here to commit, push and open a pull request from the worktree, or Hand off the chat to Local to test it in your usual checkout.

Setup works in two parts. Local environments hold setup scripts (for example npm install and npm run build) that run automatically when Codex creates a worktree. The configuration lives in the .codex folder at the project root, so you can commit it for your team. Ignored files go in the same .worktreeinclude file as above, and Codex copies an ignored AGENTS.override.md into managed worktrees on its own. Both copy rules apply to worktrees the desktop app manages, not to ones you create yourself.

By default Codex keeps your 15 most recent managed worktrees. It doesn't delete one tied to a pinned chat, a chat still in progress, or a permanent worktree, and it saves a snapshot before deleting one so you can restore it from the chat.

Using the Codex CLI? The docs above cover the app. In the terminal, make the worktree with git (next section) and start Codex in it, for example codex --cd ../app-auth. Your committed AGENTS.md is part of every worktree, so Codex loads the same instructions there.

Cursor: /worktree, /best-of-n and the Agents Window

Per Cursor's worktrees docs, the UI for worktrees is in the Agents Window: start or move an agent into a worktree there, then review, commit or open a PR from that checkout, or bring the result back into your main workspace. In the IDE you use commands:

Setup goes in .cursor/worktrees.json, which Cursor checks when it creates a worktree from the Agents Window, the IDE or the Cursor CLI. setup-worktree takes an array of commands or a script path, with setup-worktree-unix and setup-worktree-windows as OS-specific overrides. $ROOT_WORKTREE_PATH points at your main checkout:

{
  "setup-worktree": [
    "npm ci",
    "cp $ROOT_WORKTREE_PATH/.env .env"
  ]
}

Cursor advises against symlinking dependencies into the worktree and suggests a fast package manager such as pnpm, bun or uv instead. If setup fails, open the Output panel and select Worktrees Setup.

Watch the cleanup. Cursor keeps 25 worktrees per machine by default (cursor.worktreeMaxCount, checked every cursor.worktreeCleanupIntervalHours), and the docs say worktrees created outside its manager, for example with git worktree add, "are eligible for deletion". Commit or push anything you want to keep.

Any agent: plain git

For Codex CLI, Copilot, Windsurf or a mix of tools, create the worktrees yourself. Keeping them next to the repo rather than inside it means there's nothing to add to .gitignore:

git worktree add ../app-auth -b agent/auth
git worktree add ../app-billing -b agent/billing
cp .env ../app-auth/ && cp .env ../app-billing/
git worktree list

cd ../app-auth && claude          # terminal 1
cd ../app-billing && codex        # terminal 2

Install dependencies in each folder before you trust any test result there. Two errors you'll meet, both from git itself:

Split the work so it merges

Worktrees stop agents from overwriting each other's files. They don't stop two agents from making plans that can't both be true. Git merges text, so a rename on one branch and a new call to the old name on another can merge cleanly and then fail to build. A few habits help:

Each parallel task is still a normal session. A short plan file per worktree keeps it on track; see the plan-first workflow.

Cleanup at a glance

ToolWhere worktrees goAutomatic cleanup
Claude Code.claude/worktrees/<name>/Clean unnamed sessions removed on exit; otherwise it asks. Subagent and background worktrees swept after cleanupPeriodDays unless they hold work
Codex (desktop app)$CODEX_HOME/worktrees (change in Settings > Worktrees)Keeps the 15 most recent managed worktrees by default; snapshot before deletion
CursorCursor's worktree rootKeeps 25 per machine by default; the docs say worktrees made outside its manager can be deleted too
Plain gitWherever you put themNone. git worktree remove, then git worktree prune for folders you deleted by hand

Checklist

  1. Make sure the repo has at least one commit and your instruction files are committed.
  2. One task, one worktree, one branch per agent.
  3. List ignored files in .worktreeinclude (Claude Code, Codex app) or copy them in .cursor/worktrees.json (Cursor).
  4. Install dependencies in each worktree; give each dev server its own port.
  5. Split tasks by area, and land shared contracts on main first.
  6. Merge through one integrator, then run the full test suite on the result.
  7. Remove finished worktrees and branches; know your tool's automatic cleanup limit.

FAQ

Can two AI agents work in the same folder at the same time?

They can, but they edit the same files, so one agent's half-finished change lands in the other's build and tests. Give each agent its own git worktree: a separate checkout on its own branch that shares the repository's history. Claude Code (claude --worktree), Codex in the ChatGPT desktop app (Worktree mode) and Cursor (/worktree or the Agents Window) can each create one for you.

Why is my .env missing in the worktree?

A worktree is a fresh checkout, so it only contains tracked files. Gitignored files such as .env or node_modules aren't there. Claude Code and the Codex app copy ignored files listed in a .worktreeinclude file at the repo root; Cursor runs the setup commands in .cursor/worktrees.json, for example cp $ROOT_WORKTREE_PATH/.env .env. Install dependencies in each worktree too.

Why does git say a branch is already used by another worktree?

Git lets a branch be checked out in only one worktree at a time. Check out a different branch in one of them, merge the branch from your main checkout without checking it out, or in Codex use Hand off to move the chat to Local instead.

Does each worktree load my CLAUDE.md or AGENTS.md?

Yes, if the file is committed, because every worktree has its own copy of the tracked files. Edits you make in one worktree don't reach the others until you commit and merge them. One exception: a Claude Code subagent running in its own worktree takes its instruction files from the main conversation. Codex copies an ignored AGENTS.override.md into the worktrees it manages.

How do I clean up old worktrees?

Claude Code offers to remove a worktree when you exit the session and removes clean unnamed ones automatically. The Codex app keeps your 15 most recent managed worktrees by default, and Cursor keeps 25 per machine by default. For worktrees you made yourself, run git worktree list and git worktree remove <path>.

Sources

Official docs, checked 2026-10-22.

Get the free rules sampler + new tips by email

Confirm your email and we'll send you straight to the sampler download. Then one short email every two weeks covering tool format changes (Cursor, Claude Code, Codex, Windsurf, Copilot), new rules and kit updates. No spam.

We store only your email, with our email provider Resend. See the privacy policy.

Unsubscribe anytime from any email. Rather skip email? Get the sampler on GitHub or download the zip directly.