Original URL: https://getagenttools.com/blog/codex-worktree/

# Codex worktree: what --worktree created in my test, where it lives, and why features list says removed

10 Nov 2026Marek.

- codex
- coding-agents
- agent-ops

A Codex worktree is a separate Git checkout that Codex creates for one session. In Codex CLI 0.166.0 you get it with `--worktree`. The help says "Run the session in a new managed Git worktree". This post is for people who run Codex in a terminal and want to know where the checkout goes and what breaks. Short answer: it lands in `~/.codex/worktrees/` on a detached HEAD. The CLI does not clean it up. `codex features list` says `worktrees removed true`. That means the old switch is gone and the feature is always on.

 

```bash
codex --worktree                      # new interactive session
codex exec --worktree "your task"     # scripted run
codex exec fork --worktree <SESSION_ID> "next step"
git worktree list                     # see what Codex left behind
```

  

- **Location.** `$CODEX_HOME/worktrees/<4 hex chars>/<repo name>`. My two runs got `85ab/w18` and `a31b/w18`.
- **No branch.** The checkout is a detached HEAD on the commit you had checked out. `git status` inside it printed `## HEAD (no branch)`.
- **Committed work only.** An uncommitted edit and an untracked file in my repo did not show up in the worktree.
- **"removed" is not gone.** The source calls `worktrees` a "Removed compatibility flag" and says managed worktrees are "always available".
- **No resume.** `codex resume --worktree` exits with an error. Use `fork` with `--worktree`.

 

The Claude Code side, with named branches and the review and merge workflow, is in my [Claude Code worktrees](https://getagenttools.com/blog/claude-code-worktrees/) post. This one is the Codex CLI.

 

## Does Codex CLI support worktrees?

 

Yes. Codex CLI 0.166.0 has a `--worktree` flag on `codex`, `codex exec` and `codex fork`. The [CLI reference](https://learn.chatgpt.com/docs/developer-commands?surface=cli) says it "Supports new interactive sessions, codex fork \<SESSION\_ID\>, codex exec, and codex exec fork; requires local execution." On 5 November 2026 I ran it twice in a fresh repo with one commit. Both runs worked.

 

The [Codex worktrees page](https://learn.chatgpt.com/docs/environments/git-worktrees) describes the ChatGPT desktop app. There you pick Worktree under the composer and choose a base branch. The CLI flag has no branch picker. It starts from the commit you are on.

 

## Why does codex features list say worktrees removed?

 

The flag was removed, not the feature. `codex features list` on 0.166.0 prints `worktrees removed true`. In the source at tag `rust-v0.166.0`, `codex-rs/features/src/lib.rs` documents the `Worktrees` entry as "Removed compatibility flag. Managed worktrees and repository-aware sessions are always available." The entry has `default_enabled: true`.

 

The same file defines the `Removed` stage as "The feature flag is useless but kept for backward compatibility reason." When Codex reads `[features]` from config, it skips a `worktrees` key without applying it. So `worktrees = false` should do nothing. I read that in the code. I did not test it.

 

## Where are Codex worktrees stored?

 

Under `$CODEX_HOME/worktrees`, which is `~/.codex/worktrees` by default. The docs say "Codex creates worktrees in $CODEX\_HOME/worktrees." In `codex-rs/worktree/src/paths.rs` each checkout gets a bucket named after the first four hex characters of a random UUID. The repo folder name goes inside it.

 

My first run printed `workdir: /Users/<me>/.codex/worktrees/85ab/w18` in the `codex exec` header. The second run got `a31b`. On macOS Codex also writes a `.metadata_never_index` file into the root, which keeps Spotlight out.

 

The docs say you change the root in the desktop app under Settings \> Worktrees \> Worktree root. In the source the CLI reads the same value, `git-worktree-root` under `[desktop]` in the host config. A code comment says allocation "belongs to the host, not this session's project, profile, or overrides". So a `-c` override should not move it. I did not test a custom root.

 

## Which branch does a Codex worktree use?

 

None. The worktree is a detached HEAD on the commit of your current HEAD. In `codex-rs/worktree/src/lib.rs` Codex runs `git worktree add --detach --no-checkout` and then a hard reset to that commit. After my run, `git worktree list --porcelain` showed the new checkout with the same SHA as `main` and the word `detached`.

 

The source checkout had a modified `README.md` and an untracked `notes.txt`. Inside the worktree `cat README.md` printed only `hello`. `ls -a` listed `.git` and `README.md`. The desktop docs say the app applies uncommitted changes to a new worktree. The CLI flag did not. Commit first, or the agent works on an older tree.

 

## Why does codex worktree setup fail?

 

Usually the source folder is the problem. I triggered four errors on purpose. Each one exited within a second. None created a checkout. Outside a Git repo, `codex exec --worktree` stopped with `Error: git command failed: fatal: not a git repository (or any of the parent directories): .git`. `--skip-git-repo-check` does not help. The worktree needs a repo.

 

`--worktree --ephemeral` gives `Error: --worktree cannot be combined with --ephemeral`. `codex exec resume --worktree` says `` `--worktree` cannot resume an existing session; use `codex exec fork --worktree` ``. The fourth, `codex resume --worktree`, is in the FAQ. The source has one more check. A folder marked untrusted gets `--worktree requires a source that is not explicitly untrusted`.

 

The "setup" in the desktop app is something else. The [local environments page](https://learn.chatgpt.com/docs/environments/local-environment) describes setup scripts that "run automatically when Codex creates a new worktree at the start of a new chat". I found no setup script call in the CLI worktree code. If your error mentions a setup script, look at the desktop app's local environment.

 

## How does a Codex worktree compare with claude --worktree?

 

Claude Code makes a named branch inside your repo. Codex makes a detached checkout in its home folder and leaves branching to you. GitHub Copilot CLI names a branch but starts from HEAD, see [Copilot CLI worktree](https://getagenttools.com/blog/copilot-cli-worktree/). The Claude Code column comes from the [Claude Code worktrees docs](https://code.claude.com/docs/en/worktrees), not from a test.

 

|  | Codex CLI 0.166.0 | Claude Code |
| --- | --- | --- |
| Flag | `--worktree` | `--worktree <name>` or `-w` |
| Location | `~/.codex/worktrees/<hex>/<repo>` | `.claude/worktrees/<name>/` in the repo |
| Branch | Detached HEAD | `worktree-<name>` |
| Base | Current HEAD commit | Default remote branch, or HEAD with `worktree.baseRef` |
| Uncommitted changes | Not carried (my test) | Starts from a commit, `.worktreeinclude` copies ignored files |
| Cleanup | None in the CLI | Clean unnamed worktree removed on exit; prompt if it has work or the session is named; none after `-p` |
| Resume | Not supported, use fork | `claude --worktree <name> --resume` |

 

## Frequently asked questions

 

### Does codex resume support --worktree?

 

No. `codex resume --help` on 0.166.0 lists `--worktree`, but `codex resume --worktree --last` exits with `` `--worktree` supports new interactive sessions, `codex fork`, `codex exec`, and `codex exec fork` ``. Fork the session with `--worktree` instead. Resume itself is covered in [codex resume](https://getagenttools.com/blog/codex-resume/).

 

### Does Codex delete worktrees automatically?

 

Not from the CLI. The source says the CLI "uses the same root without enabling automatic cleanup". The desktop app keeps the 15 most recent, per the docs. Right after my runs both test checkouts were still on disk; I removed them myself later. Remove one with `git worktree remove <path>` from the source repo.

 

### What does codex worktree mean?

 

It means a Git worktree that Codex created and tracks. The Codex docs define a worktree as a second copy ("checkout") of your repository that shares the same `.git` metadata. A managed one lives under `~/.codex/worktrees` and records the session that owns it in `codex-thread.json`.

 

### Did you test this?

 

Yes, on macOS with `codex-cli 0.166.0` on 5 November 2026. Two `codex exec --worktree -s read-only` runs and four error cases, each logged. I did not test the TUI, a custom root, Windows or Linux.

 

## The verdict

 

I use Codex every day as a worker next to Claude Code. `--worktree` does what the help says. Commit before you run it, since the checkout starts from HEAD. Add a branch inside the worktree if the work should become a pull request. Run `git worktree list` now and then, since the CLI cleans up nothing. For scripted runs, start with [codex exec](https://getagenttools.com/blog/codex-exec/). For what a `read-only` run can touch, see [Codex sandbox](https://getagenttools.com/blog/codex-sandbox/).

 

Sources, read on 5 November 2026: the Codex [worktrees page](https://learn.chatgpt.com/docs/environments/git-worktrees), [local environments page](https://learn.chatgpt.com/docs/environments/local-environment) and [CLI reference](https://learn.chatgpt.com/docs/developer-commands?surface=cli), the [Claude Code worktrees page](https://code.claude.com/docs/en/worktrees) and local help output for `codex-cli 0.166.0`. Code references are from [openai/codex](https://github.com/openai/codex) at tag `rust-v0.166.0`, mainly `codex-rs/worktree/src/` and `codex-rs/features/src/lib.rs`. The test runs are my own.

Put this into practice

[See the tool list](https://getagenttools.com/tools/)[Train your team](https://getagenttools.com/#training)
