Git worktrees for coding agents: run several agents in parallel without collisions
GuideBy Lucas Matías García TorresUpdated Data checked 6 min read
Reviewed on . How we work.

Short answer
Git worktrees for coding agents give each agent its own checkout of the same repository, on its own branch, so parallel tasks do not overwrite each other. Claude Code starts one with claude --worktree, Codex in the ChatGPT desktop app works in detached worktrees and keeps the latest 15, and Cursor runs setup commands from .cursor/worktrees.json and keeps 25 per machine. Ignored files such as .env must be copied in, and you still review and merge each branch yourself.
Prices and limits change often: check the vendor's page before you pay. How we make money.
Run two coding agents in the same folder and they will overwrite each other's edits and break each other's builds. Git worktrees for coding agents solve that: each agent gets its own checkout of the same repository, on its own branch, while all of them share one history. This guide explains what a worktree is, how Claude Code, Codex and Cursor use them, and the git commands to manage them yourself, as documented on October 28, 2026.
What a git worktree is
Git lets one repository have several working trees, so you can check out more than one branch at a time. The folder you cloned is the main worktree, and each extra one you create with git worktree add is a linked worktree. A linked worktree has its own files and its own per-worktree state, such as HEAD and the index, and shares everything else through the repository's .git directory. A commit made in one worktree shows up in the others at once, with no push or fetch in between.
Two rules matter when you hand worktrees to agents:
- One branch per worktree. Git refuses to check out a branch that another worktree already uses, with an error saying it "is already used by worktree". Give each agent its own branch, or use a detached worktree that is not on any branch.
- Submodules are a weak spot. The Git manual says multiple checkouts are still experimental, that submodule support is incomplete and that it does not recommend several checkouts of a superproject.
How Claude Code, Codex and Cursor use worktrees
Table: worktree support in three coding tools (official documentation, checked October 28, 2026)
| Tool | How to start one | Branch | Ignored files such as .env | Cleanup |
|---|---|---|---|---|
| Claude Code | claude --worktree plus a name, asking Claude to work in a worktree, or isolation: worktree for a subagent | A new branch named worktree- plus the name, from the default branch, or from your current HEAD with worktree.baseRef set to "head" | Copies the ones listed in .worktreeinclude | On exit, removes a clean worktree from an unnamed session and asks about the rest; runs with -p are not cleaned up |
| Codex in the ChatGPT desktop app | Choose Worktree under the composer and pick a starting branch | Detached HEAD until you choose Create branch here | Copies the ones listed in .worktreeinclude, plus an ignored AGENTS.override.md | Keeps the 15 most recent managed worktrees and saves a snapshot before deleting one |
| Cursor | The Agents Window, or /worktree and /best-of-n in the IDE | Not stated on the page | Runs your setup commands from .cursor/worktrees.json | Keeps up to 25 worktrees per machine by default, across all projects |
Claude Code
Run claude --worktree with a name, or the short form -w, in each terminal you want to use. Claude Code creates the worktree under .claude/worktrees/ at the repository root, on a new branch called worktree- plus that name, and makes up a name such as bright-running-fox if you leave it out. The repository needs at least one commit: in an empty one the command fails with "Failed to resolve base branch". Add .claude/worktrees/ to .gitignore so those folders do not show up as untracked files in your main checkout.
When you exit, Claude Code checks the worktree for changed or untracked files and new commits. A clean worktree from an unnamed session is removed with its branch; in every other case it asks whether to keep it. Runs with -p have no exit prompt and are never cleaned up, so remove those worktrees with git yourself, unlocking them first if git refuses.
While a session is isolated in a worktree, Claude Code blocks file edits and commands aimed at the main checkout, including git pointed there with -C or GIT_DIR. Its documentation is clear about the limit: those checks do not track which files a shell command writes, so a cp or a redirect into the main checkout falls under your normal permission and sandbox settings instead.
Codex
In the ChatGPT desktop app, Codex creates each managed worktree in $CODEX_HOME/worktrees from the HEAD of the branch you pick, applies your uncommitted changes to it and works in detached HEAD, so it can run several worktrees without creating branches you did not ask for. When you want to check the result in your usual setup, Handoff moves the chat and its code to your local checkout; OpenAI suggests it, for example, when you can run only one instance of your app. Codex never deletes a worktree tied to a pinned or in-progress chat, and it saves a snapshot before deleting one, so you can restore it from the chat.
Cursor
Cursor reads .cursor/worktrees.json when it creates a worktree and runs the commands listed under setup-worktree, or its -unix and -windows variants, for example npm ci and a copy of .env from the main checkout. It advises against symlinking dependencies into a worktree and suggests a fast package manager such as bun, pnpm or uv instead. /best-of-n runs one task with several models, each in its own worktree, and leaves the choice and the merge to you. Cleanup also counts worktrees in Cursor's worktree folder that were created another way, such as with git worktree add, so commit anything you want to keep to a branch or push it.
Doing it by hand with git
You do not need any of these tools to use worktrees: the same commands work with any agent that runs in a terminal. We ran these with Git 2.56.0:
git worktree add -b fix-login ../myapp-fix-login main
git worktree add -d ../myapp-experiment
git worktree list
git worktree remove ../myapp-fix-login
git worktree prune
In order: the first command creates a worktree on a new branch, fix-login, starting from main; the second creates a throwaway worktree on no branch, at the current commit; list shows every worktree with its commit and branch; remove deletes a worktree once its branch is merged; and prune clears what is left after you delete a worktree folder by hand.
Then start one agent in each folder. Three details from the Git manual: git worktree add with only a path creates a branch named after the last part of the path; remove refuses a worktree with modified or untracked files unless you add --force, and a locked one needs --force twice; and git worktree lock stops Git from pruning a worktree kept on a removable drive or network share.
A workflow that holds up
- Commit before you split. A new worktree starts from a commit, so uncommitted work stays behind unless your tool copies it, as Codex does.
- One task, one branch, one worktree. Split the work by area. Worktrees stop agents overwriting each other while they work, but two branches that change the same lines still conflict when you merge them.
- Prepare each worktree. A worktree is a fresh checkout: install dependencies and copy only the ignored files the agent needs, since every copy of .env is one more copy of your secrets on disk. Keep build and test commands in an AGENTS.md file so every agent runs the same ones.
- Watch disk and usage. Each worktree has its own files, dependencies and build caches, which is why Codex and Cursor cap how many they keep, and each agent spends its own tokens; our guide to agent token usage shows how fast that adds up.
- Test in turn if your app cannot run twice. If only one instance of your app can run at a time, test the worktrees one after another or bring the work back to your main checkout.
- Review, merge, remove. Review each branch as a pull request, merge it, then remove the worktree and delete the branch.
If you are new to agents in the terminal, start with our guide on how to use Claude Code, and to choose a tool, see our comparison of the best coding assistants.
How we checked
On October 28, 2026 we read the git-worktree manual on git-scm.com, last updated in Git 2.56.0, Anthropic's Claude Code pages on worktrees and common workflows, OpenAI's Codex page on worktrees and Cursor's documentation on worktrees. We ran the git commands in this guide with Git 2.56.0 on Windows 11 in a test repository. Tool behavior changes between versions, so check the documentation for the version you use.
What we checked
- git-worktree manual, last updated in Git 2.56.0: a repository can support multiple working trees, so more than one branch can be checked out at a time; git worktree add creates a linked worktree that shares everything with the repository except per-worktree files such as HEAD and index; refs are shared across all worktrees except refs/bisect, refs/worktree and refs/rewritten; by default add refuses to check out a branch already checked out by another worktree; git worktree add with only a path creates a new branch named after the final component of the path; add -d creates a worktree with a detached HEAD; remove only removes clean worktrees, with no untracked files and no modified tracked files, unless --force is used, and a locked worktree needs --force twice; the main worktree cannot be removed; prune removes administrative files of worktrees whose directories are missing; lock keeps a worktree on a portable device or network share from being pruned; multiple checkout is still experimental, submodule support is incomplete and multiple checkouts of a superproject are not recommended. (Git, )
- Claude Code worktrees: claude --worktree or -w with a name creates a worktree under .claude/worktrees/<name>/ at the repository root on a new branch named worktree-<name>, and Claude generates a name such as bright-running-fox if it is omitted; Anthropic recommends adding .claude/worktrees/ to .gitignore; new worktrees branch from the repository's default branch unless worktree.baseRef is set to "head", and the setting cannot name a branch; a .worktreeinclude file in .gitignore syntax copies only files that match a pattern and are gitignored; a subagent with isolation: worktree gets a temporary worktree that is removed automatically when it finishes without changes; on exit, a clean worktree from an unnamed session is removed with its branch, while a named session or a worktree with changes, untracked files or new commits prompts to keep or remove it; non-interactive runs with -p are not cleaned up, and git worktree unlock may be needed before git worktree remove; while isolated, Claude Code blocks edits and commands that target the main checkout and git redirected there with -C, --git-dir, GIT_DIR or GIT_WORK_TREE, but the checks do not track which files a shell command writes, such as cp or a shell redirect; worktrees share the repository's .git directory. (Anthropic, )
- Claude Code common workflows: each git worktree is a separate checkout on its own branch created from an existing commit, so the repository needs at least one commit; in a repository with no commits, claude --worktree fails with Failed to resolve base branch "HEAD": git rev-parse failed. (Anthropic, )
- Codex worktrees in the ChatGPT desktop app: worktrees require a Git repository; managed worktrees are created in $CODEX_HOME/worktrees, which can be changed under Settings > Worktrees > Worktree root; the starting commit is the HEAD of the selected branch and uncommitted changes on that branch are applied; the worktree is in a detached HEAD state, and Create branch here turns it into a branch; Handoff moves a chat between Local and Worktree, for example when you can run only one instance of your app; ignored files listed in .worktreeinclude are copied into local managed worktrees, and an ignored AGENTS.override.md is copied automatically; worktrees can take a lot of disk space because each has its own repository files, dependencies and build caches; by default Codex keeps the 15 most recent managed worktrees, does not delete one tied to a pinned or in-progress chat, and saves a snapshot before deleting one so it can be restored; Git prevents the same branch from being checked out in more than one worktree at a time. (OpenAI, )
- Cursor worktrees: agents can work in isolated Git checkouts started from the Agents Window, the IDE or the Cursor CLI; .cursor/worktrees.json accepts setup-worktree, setup-worktree-unix and setup-worktree-windows as a list of commands or a script path, and examples copy .env from $ROOT_WORKTREE_PATH; Cursor does not recommend symlinking dependencies into the worktree and suggests a fast package manager such as bun, pnpm or uv; from Cursor 3.5, cleanup keeps the newest worktrees up to cursor.worktreeMaxCount, 25 per machine by default across all workspaces, and worktrees created outside the manager, such as with git worktree add, are eligible for deletion; /worktree runs a task in an isolated worktree, and /best-of-n runs the same task across several models, each in its own worktree, without merging the result into the main checkout. (Cursor, )
What may change
- Coding tools change how they create, name and clean up worktrees from one version to the next. This page is reviewed monthly.
Frequently asked questions
What is a git worktree?
A worktree is an extra working folder attached to the same Git repository. Each one has its own files, index and checked-out branch, but all of them share the repository's commits, branches and remotes through one .git directory. Git calls the original folder the main worktree and the extra ones linked worktrees, and you create them with git worktree add.
Why do coding agents need worktrees?
Two agents editing the same folder overwrite each other's files and break each other's builds. A worktree per agent, each on its own branch, keeps their edits apart until you review and merge them. Claude Code, Codex in the ChatGPT desktop app and Cursor all use Git worktrees for this, according to their documentation on October 28, 2026.
How do I get my .env file into a new worktree?
A worktree is a fresh checkout, so files Git ignores, such as .env, are not there. Claude Code and Codex copy the ignored files you list in a .worktreeinclude file at the repository root, written in .gitignore syntax. Cursor instead runs the setup commands in .cursor/worktrees.json, which can copy .env from the main checkout.
Can two worktrees use the same branch?
No. By default Git refuses to check out a branch that another worktree is already using, because a branch can only move forward in one place at a time. Give each agent its own branch, or create a throwaway worktree with git worktree add -d, which is not on any branch. Codex avoids the problem by working in detached HEAD.
How do I delete a worktree?
Run git worktree remove with the worktree's path. Git only removes clean worktrees, with no modified or untracked files, unless you add --force, and a locked worktree needs --force twice. If you already deleted the folder by hand, git worktree prune clears the leftover records. Claude Code, Codex and Cursor also clean up the worktrees they create.
Sources
- git-worktree Documentation, Git. Accessed October 28, 2026.
- Run parallel sessions with worktrees, Anthropic. Accessed October 28, 2026.
- Common workflows, Anthropic. Accessed October 28, 2026.
- Worktrees (Codex), OpenAI. Accessed October 28, 2026.
- Worktrees, Cursor. Accessed October 28, 2026.
Spotted an error or an outdated price? Tell us and we will fix it.
Change history
- : First published. Checked against the git-worktree manual, Anthropic's Claude Code documentation on worktrees and common workflows, OpenAI's Codex worktrees page and Cursor's worktrees documentation on October 28, 2026; git commands run with Git 2.56.0.
Next review: .
Part of our Coding Assistants guide.
Keep reading
- How to use Claude Code: a beginner's guide
How to use Claude Code step by step: install it, log in, start a session, ask questions, make a first change, use Git, learn the commands and set up CLAUDE.md.
- AGENTS.md: the instructions file for coding agents
What AGENTS.md is, what to put in it and how Codex, Claude Code, Cursor and GitHub Copilot read it, with nesting rules, size limits and security advice.
- Best coding assistant: 7 tools compared by price and way of working
The best coding assistant by price and way of working: Claude Code, Codex, GitHub Copilot, Cursor, Antigravity, Mistral and local models, from vendor pages.
- Agent token usage: where the tokens go and how to spend fewer
Why an agent uses far more tokens than a chat: the loop, tool definitions, tool results and reasoning, with a worked cost example and six ways to cut it.
- GitHub Copilot pricing: plans, usage credits and what a month buys
GitHub Copilot plans from Free to Enterprise, how usage credits work since June 2026, and what a chat question or an agent task costs by model.
- What is an agent?
What an agent is, how it differs from a chatbot or a workflow, how the plan-act-observe loop works, real examples, when you do not need one and the risks.
Other topics

Assistants & Chatbots
21 guides

LLM APIs & Costs
16 guides

Agents & Automation
13 guides

Local Models & Hardware
16 guides