Workflows & ExamplesFIELD NOTE · 17 MIN

Codex Worktrees Run Parallel, Until They Collide

Codex worktrees in the app and CLI, tested on v0.172.0: where they live, what they leave behind (.env, ports, deps), cleanup, and Codex next to Claude Code.

AITerm 17 min read

A Codex worktree is a git worktree that Codex creates so one task can edit files without touching your checkout. The desktop app and Codex CLI (since v0.154.0, September 2026) both put them in $CODEX_HOME/worktrees, in detached HEAD. They isolate tracked files. Your .env, dependencies and dev server port are a separate problem.

Updated November 27, 2026 · by the AITerm team

211 MB

per Codex worktree of our own repository, tracked files only, before a single dependency (Nov 27, 2026)

0

gitignored files Codex CLI 0.172.0 copied into its worktree in our test, .env included

15

managed worktrees the Codex desktop app keeps by default before it starts deleting the oldest

Key takeaways

  • App worktrees and CLI worktrees share a folder, not a feature set. Setup scripts and .worktreeinclude are app-only.
  • The CLI starts from your last commit. Uncommitted edits in your checkout do not follow the task.
  • Collisions come from what git does not track: ports, .env, node_modules, and the branch you already have checked out.
  • Archiving a CLI session leaves its worktree on disk. Run git worktree list once a week.
  • Codex and Claude Code can share a repository safely if each one stays in its own worktree root.

What is a Codex worktree?

A Codex worktree is an extra checkout of your repository that Codex creates for one chat or task. It shares the .git history with your main checkout but has its own files, so the agent can edit, build and test without touching yours. Codex starts it in detached HEAD from the commit you pick, and no branch appears until you ask for one.

At AITerm, where we build a native macOS terminal for piloting Codex and Claude Code agent sessions, we ran Codex CLI 0.172.0 against our own repository on November 27, 2026 to check what the docs leave out. Short version: the worktree is real, fast and isolated, and it arrives without your .env, without your uncommitted edits and without a cleanup plan.

Here's the thing. Most pages ranking for this query describe the desktop app only, and some still say the CLI has no worktree support. That stopped being true on September 9, when release rust-v0.154.0 added --worktree and /worktree. Version 0.156.0 turned the feature on by default on September 22, and 0.164.0 deleted the flag on October 23. There is nothing left to enable.

App worktrees and CLI worktrees are different features

The app and the CLI use the same word and the same folder, then behave differently. OpenAI documents the desktop app version in detail and gives the CLI a few lines in its command reference, so we filled the gaps by testing. Turns out the differences are exactly the ones that cause collisions.

Codex app Codex CLI 0.172 Claude Code
Location$CODEX_HOME/worktrees, editable$CODEX_HOME/worktrees/<id>/<repo>.claude/worktrees/<name> in the repo
BranchDetached HEADDetached HEADNew branch worktree-<name>
Starts fromSelected branch, uncommitted changes includedCurrent commit, uncommitted changes droppedRemote default branch, unless baseRef: "head"
Gitignored filesCopied if listed in .worktreeincludeNot copiedCopied if listed in .worktreeinclude
Setup scriptLocal environment in .codex/None documentedNone built in, or a WorktreeCreate hook
CleanupKeeps 15, snapshot before deleteNone seen in our testPrompt on exit, sweep for background ones

The row that bites first is "starts from". In the app, OpenAI's docs say uncommitted changes on the starting branch get applied to the worktree. In the CLI, we edited a tracked file without committing, ran codex exec --worktree, and the worktree held the committed version. If you hand the CLI a task that depends on what you just typed, commit or stash first.

How do I run several Codex tasks in parallel on the same repo?

To run several Codex tasks in parallel on one repository, start each task in its own managed worktree: codex --worktree or /worktree in the CLI, or Worktree under the composer in the ChatGPT desktop app. Each task gets its own checkout in detached HEAD, so edits never collide. AITerm, a native macOS terminal for Codex and Claude Code, gives each worktree its own pane with a live state.

In practice, the CLI side is four commands. Run them from the main checkout, one terminal or pane per task:

# interactive task in a fresh managed worktree
codex --worktree

# scripted task; exec started read-only in our test, so allow edits
codex exec --worktree -s workspace-write "add tests for the parser"

# branch an existing session into its own worktree
codex fork <SESSION_ID> --worktree

# see every session across linked worktrees
codex agents
01

Commit before you fan out

The CLI copies the commit, not your working state. Two minutes of git commit saves a task built on a stale file.

02

One task per worktree

Split by files, not by ambition. Two tasks that both touch the router will merge into a conflict nobody wrote.

03

Equip the worktree

Copy .env, install dependencies, pick a free port. The app does it with a local environment; in the CLI you do it.

04

Review, then land it

Create a branch from the worktree, read the diff, merge. In the app, Hand off moves the chat back to your local checkout.

Two restrictions are easy to trip on. The CLI reference says --worktree needs local execution and cannot be combined with resume, review, --ephemeral or --ignore-user-config. And codex fork --worktree needs an explicit session ID: --last and the picker do not work there. If you live in the TUI, /worktree also has a Browse worktrees view that shows each worktree with the chat that owns it.

What we measured on our own repository

Codex itself is fast here. Git does the work, and Codex adds almost nothing on top. We cloned the AITerm repository (812 tracked files, Swift sources, the marketing site, signed app bundles) and timed things on our Linux build server, 2 vCPUs. We ran Codex without signing in: it creates the worktree before it talks to the model, so the result holds.

01 · DISK

211 MB per worktree

The Codex worktree weighed exactly what our checkout weighs. The 743 MB .git folder is shared, never copied.

02 · TIME

1.1 to 4.2 seconds

Three successive git worktree add runs on the same repo. A laptop SSD will do better; the order of magnitude holds.

03 · LEFT BEHIND

.env and local edits

Neither reached the CLI worktree, even with a .worktreeinclude listing .env at the root.

04 · LEFT OVER

3 of 3 worktrees stayed

After codex archive on one session and codex delete --force on another, git worktree list still showed all three.

The disk cost is the number people underestimate, because it scales with retention, not with how many agents you run right now. The desktop app keeps 15 managed worktrees by default. On our repository, that is more than 3 GB of checkouts sitting there before anyone runs npm install:

DISK USED, OUR REPO + N CODEX WORKTREES Repo only 954 MB +1 worktree 1,165 MB +4 worktrees 1,798 MB +15 (app default) 4,119 MB TRACKED FILES ONLY. SOURCE: AITERM, DU -SH, NOV 27, 2026
954 MB = 211 MB checkout + 743 MB shared .git. Each worktree adds 211 MB. Dependencies and build folders come on top.

A JavaScript project pays twice: once for the checkout, once for a fresh node_modules in every worktree, because a worktree shares history, not installed packages. Our repo is mostly Swift and static files, so treat the 211 MB as a floor.

Conditions: Codex CLI 0.172.0 from npm, isolated CODEX_HOME, local clone of our repository, sizes from du -sh. Your numbers will differ with repo size and disk. The method takes two minutes: run codex exec --worktree, then du -sh the folder it prints as workdir.

Where do Codex worktrees collide?

Codex worktrees collide wherever git stops tracking: shared ports, missing .env files, absent dependencies, a branch already checked out elsewhere, and worktrees nobody removes. The file edits themselves stay isolated. Everything a running app needs beyond tracked files, though, is either missing from the worktree or shared with every other one.

PORTS

Two dev servers, one 3000

Every worktree runs the same npm run dev. The second one fails on a busy port, or worse, your browser tests the wrong worktree. Nothing in the Codex docs remaps ports.

SECRETS

No .env, odd failures

A gitignored file never travels. The agent sees a missing API key, then "fixes" it by hardcoding a value or mocking the client.

DEPENDENCIES

command not found

No node_modules, no virtualenv, no build cache. Tests fail for reasons unrelated to the change, and the agent burns turns on them.

BRANCHES

One branch, one place

Git refuses a second checkout of the same branch: fatal: '<branch>' is already used by worktree at '<path>'. We reproduced it on our own repo.

LEFTOVERS

Orphans in git worktree list

Delete a folder by hand and git keeps the entry as prunable. Archive a CLI session and the folder stays. Both pile up quietly.

The branch error confuses people because Codex works around it by design: detached HEAD means no branch to fight over. It comes back the moment you try to check out the agent's work on a branch you already have open locally. The app's answer is Hand off, which moves the chat and its code to your local checkout. In the CLI, create a new branch inside the worktree with git switch -c and merge that.

Our opinion

Leave Codex's detached HEAD alone. A lot of worktree advice tells you to name a branch per agent before it starts. For throwaway tasks, that is ceremony: half of them get discarded, and you end up deleting branches nobody merged. Name the branch when the diff deserves to live, not before.

Fixing .env, ports and dependencies at creation

Fix them at creation time, once, instead of letting the agent discover each gap. In the desktop app, OpenAI gives you two tools: a .worktreeinclude file for gitignored files and a local environment, stored in .codex/ at the project root, whose setup script runs on every new worktree. Commit both so the whole team gets them.

# .worktreeinclude (repo root, gitignore syntax, gitignored files only)
.env
.env.local
config/secrets.json

You're probably thinking that file covers the CLI too. It does not. OpenAI's worktree page says .worktreeinclude applies to app-managed local worktrees, "not remote worktrees or Git worktrees you create yourself from the command line", and our CLI test matched: no .env. For the CLI, a small shell function does the equipping after Codex creates the worktree. This is the shape we would start from:

# run from inside a fresh Codex worktree, with the main checkout as $1
equip_worktree() {
  main="$1"
  cp -n "$main/.env" "$main/.env.local" . 2>/dev/null
  if [ -f pnpm-lock.yaml ]; then pnpm install --frozen-lockfile
  elif [ -f package-lock.json ]; then npm ci; fi
  export PORT=$((3000 + RANDOM % 900))
  echo "worktree ready on port $PORT"
}

Honestly, the port line is the crude part. A random offset avoids the common clash, but two worktrees can still draw the same number, and your framework has to read PORT for it to matter. Next.js and most Node servers do. If your dev server hardcodes 3000, fix that first, because no worktree tool can work around it.

The app's equivalent is cleaner. A local environment holds a setup script, with optional macOS, Windows and Linux variants, plus "actions" such as starting the dev server, shown in the app's top bar. OpenAI's own example is the obvious one: npm install, then npm run build.

Running Codex next to Claude Code

This is how we work most days, and it is safe if each agent keeps its own worktree root. Codex writes under ~/.codex/worktrees, outside the repo. Claude Code writes under .claude/worktrees/, inside it, on branches named worktree-<name>. They never step on each other's folders. They do share three things you should know about.

01

One .worktreeinclude

Claude Code and the Codex app read the same file name with the same syntax. One file at the root serves both. The Codex CLI ignores it.

02

Different starting points

Claude Code branches from the remote default branch by default. Codex CLI starts from your current commit. Same moment, two different bases.

03

One gitignore line

Add .claude/worktrees/ to .gitignore, as Anthropic's docs suggest, or Codex sees Claude's worktrees as untracked files in its own diff.

The second card is the one that produces confusing reviews. Start both agents while you sit on an unmerged feature commit, and Claude Code works from main while Codex works from your feature. Their diffs then disagree about files neither agent touched. Set worktree.baseRef to "head" in Claude Code's settings if you want both to start from the same place.

Our split, for what it is worth: Claude Code on the feature branch, Codex on tests and mechanical sweeps, each in its own worktree. We explain why in Codex vs Claude Code. A third tool adds its own rules. Cursor 3.5 and later caps worktrees at 25 per machine and, per its docs, its cleanup can delete worktrees created outside Cursor, including ones you made with git worktree add.

Trap

Never point Codex and Claude Code at the same working directory "just for a quick fix". Our first side-by-side run did exactly that, and the merged diff compiled while silently dropping a localisation key. Two agents, two worktrees, always.

How many Codex worktrees should you run at once?

Three or four, for most people. A worktree costs a few seconds and some disk; the real limits are your attention and your usage window. Every parallel task draws on the same ChatGPT plan, and OpenAI says Codex and ChatGPT share usage limits, so six busy worktrees drain one shared window, not six. Our Codex usage guide shows how those windows reset.

We landed on four after trying six with Claude Code, and the reasoning carries over: past four, reviewing diffs becomes the bottleneck, and the extra agents mostly wait for you. The full numbers are in our guide to running multiple AI coding agents. If you are choosing a plan around agent count, Codex pricing covers what each tier allows.

And yes, there is a case for more: long, independent, scripted jobs through codex exec --worktree that nobody watches live. Those are batch work, and the limit is your quota, not your eyes.

Cleaning up the worktrees Codex leaves behind

The app cleans up after itself; the CLI, in our test, did not. The desktop app deletes a managed worktree when you archive its chat or when it passes the retention limit, after saving a snapshot you can restore. It skips worktrees tied to a pinned or running chat, and "permanent" worktrees from older versions are never touched.

For everything else, git has the tools. Run this from the main checkout:

git worktree list                    # every checkout, Codex and Claude ones included
git worktree remove ~/.codex/worktrees/8dbc/my-repo
git worktree prune -v                # drop entries whose folder is already gone
du -sh ~/.codex/worktrees            # what retention is costing you

Look, the prune step is the one people skip. When we deleted a worktree folder by hand, git kept listing it as prunable until we ran git worktree prune -v, which printed "gitdir file points to non-existent location" and removed it. If a worktree sits on an external drive, git worktree lock protects it from that cleanup.

Seeing every worktree from one window

AITerm is a native macOS terminal for piloting Codex, Claude Code and other agent sessions. Each isolated workspace is a git worktree on a branch prefixed aiterm/. At creation, it copies .env and small local config files, shifts the dev server port upward from 3000, and installs dependencies from your lockfile. Each agent runs in its own pane with a live state, and the workspace merges or discards in one gesture.

An AITerm window with several native panes running Codex and Claude Code sessions on the same project, with per-session status colours in the sidebar
One pane per agent session, the state of each one in the sidebar.

We did not get there on the first try. On August 11, 2026, our own Preview feature started next dev inside a fresh worktree and failed with "next: command not found", because a worktree has no node_modules. Our build and test gates had the same handicap and failed for reasons unrelated to the code. We deserved it: we were building a worktree feature and still forgot the most basic thing a worktree leaves out. The fix was dull, an install command picked from the lockfile, run before anything else.

None of this needs our app. A .worktreeinclude, a setup script and a weekly git worktree list work in any terminal, including tmux if that is your setup (our tmux guide covers the pane side). What the app removes is the alt-tabbing to find out which worktree is waiting on you. For the rest of the Codex toolchain, start with our Codex CLI guide. If you are still comparing agents, the Claude Code alternatives roundup lists which ones isolate work at all, and Claude Code subagents explains the isolation: worktree field.

The short version: commit, fan out, equip each worktree, review, prune. If you try this setup, tell us what breaks: hello@aiterm.app.

Going further

Sources

  1. Worktrees, OpenAI Codex documentation, read November 27, 2026: $CODEX_HOME/worktrees, Worktree root setting, detached HEAD, uncommitted changes applied, Local and Handoff, .worktreeinclude scope, the one-branch rule, 15 worktrees kept, snapshots, permanent worktrees.
  2. Developer commands (CLI), OpenAI Codex documentation, read November 27, 2026: codex exec --worktree, codex fork <SESSION_ID> --worktree, incompatible flags, /worktree and Browse worktrees.
  3. Local environments, OpenAI Codex documentation, read November 27, 2026: .codex folder, setup scripts per platform, actions.
  4. Codex CLI rust-v0.154.0, openai/codex release notes, September 9, 2026: experimental --worktree and /worktree, worktree browser in the TUI.
  5. Codex CLI rust-v0.156.0, openai/codex release notes, September 22, 2026: worktrees enabled by default, worktree sessions from the agent command center.
  6. Codex CLI rust-v0.162.0, openai/codex release notes, October 8, 2026: tools for the model to create and list managed worktrees.
  7. Codex CLI rust-v0.164.0, October 23, 2026, worktree feature flag retired, and rust-v0.165.0, October 27, 2026, repository filter across linked worktrees.
  8. Codex CLI rust-v0.172.0, openai/codex release notes, November 24, 2026: the version we tested.
  9. git-worktree, Git documentation: linked worktrees, the checked-out branch safeguard, prune, lock, --detach.
  10. Run parallel sessions with worktrees, Claude Code documentation, read November 27, 2026: .claude/worktrees/, worktree-<name> branches, worktree.baseRef, .worktreeinclude, cleanup on exit.
  11. Pricing, OpenAI Codex documentation, read November 25, 2026: "ChatGPT Work and Codex share pricing, credits, and usage limits".
  12. Worktrees, Cursor documentation, read November 27, 2026: .cursor/worktrees.json, 25 worktrees per machine, cleanup of worktrees created outside Cursor.

Frequently asked questions

What is a worktree in Codex?

A Codex worktree is a git worktree that Codex creates and manages so a task can edit files without touching your main checkout. It shares the repository's history but has its own files, and it starts in detached HEAD, so Codex creates no branch while it works. The ChatGPT desktop app and Codex CLI both create them, under $CODEX_HOME/worktrees by default.

Does Codex CLI support worktrees?

Yes. Codex CLI added an experimental --worktree flag and /worktree command in v0.154.0 on September 9, 2026, enabled worktrees by default in v0.156.0 on September 22, and retired the feature flag in v0.164.0 on October 23. You can run codex --worktree, codex exec --worktree, or codex fork <SESSION_ID> --worktree. Resume does not accept --worktree.

Where does Codex store its worktrees?

In $CODEX_HOME/worktrees, which is ~/.codex/worktrees unless you changed CODEX_HOME. In our test of Codex CLI 0.172.0, each worktree landed in a folder with a four-character id, such as ~/.codex/worktrees/8dbc/your-repo. The desktop app lets you change the location in Settings, under Worktree root.

Does Codex copy my .env file into a worktree?

Only in the desktop app, and only if you ask. A .worktreeinclude file at the repository root lists gitignored files, such as .env, for the app to copy into its managed worktrees. OpenAI's docs say this does not apply to worktrees created from the command line, and in our test Codex CLI 0.172.0 left .env behind even with a .worktreeinclude in place.

How many worktrees does Codex keep?

The desktop app keeps the 15 most recent Codex-managed worktrees by default and deletes older ones, after saving a snapshot you can restore from the chat. It never auto-deletes a worktree tied to a pinned or running chat. In Codex CLI 0.172.0 we saw no automatic cleanup: archiving or deleting a session left its worktree on disk.

Can two Codex worktrees use the same branch?

No. Git allows a branch to be checked out in only one worktree at a time, including your main checkout, and returns an error like fatal: 'feature/a' is already used by worktree. That is why Codex starts its worktrees in detached HEAD. To use the work on your own branch, use Create branch here in the app, or hand the chat off to Local.

How do I delete old Codex worktrees?

Run git worktree list in the repository to see every worktree, then git worktree remove <path> for each one you no longer need. If you deleted a folder by hand, git worktree prune removes the stale entry. In the desktop app, archiving a chat deletes its managed worktree, and the retention limit is in settings.

Related articles