# Codex Worktrees: Run Codex CLI or the App in a Git Worktree

_Codex supports worktrees natively. Pick **Worktree** when you start a chat in the desktop app, type `/worktree` in the CLI, or pass `--worktree` to `codex exec`._

## The Fix

**Desktop app.** In the new chat view, select **Worktree** under the composer. Choose the starting branch, which can be your current branch with its uncommitted changes, then submit the prompt. Codex creates the worktree and runs the chat there.

**CLI, new session.** Start Codex CLI in a new managed worktree:

```bash
codex --worktree
```

**CLI, open session.** In an interactive session inside a local Git repository, type:

```text
/worktree
```

Codex creates a managed worktree and continues the chat in it. Choose **Browse worktrees** there to find existing worktrees and their owners. The command is unavailable while a task is running. `/new` and `/fork` also let you choose a new worktree instead of the current directory.

**Non-interactive runs.** Add `--worktree` to `codex exec` or to a fork:

```bash
codex exec --worktree "implement the next step"
codex exec fork  --worktree "try an alternative approach"
```

`--worktree` needs local execution. It does not combine with `codex resume`, `review`, `--ephemeral` or `--ignore-user-config`.

## Why It Works This Way

Codex creates each worktree in detached `HEAD` state under `$CODEX_HOME/worktrees`. Change the folder in **Settings > Worktrees > Worktree root**. A detached worktree has no branch, so click **Create branch here** in the chat header before you commit and open a pull request. Git then holds that branch in the worktree, and your local checkout cannot check it out until the worktree lets go of it. **Handoff** moves a chat and its changes between your local checkout and its worktree.

A new worktree has tracked files only. To copy ignored files such as `.env` into each managed worktree, list them in `.worktreeinclude` at the repository root. A local environment can also run setup scripts, such as a dependency install, when the worktree is created.

The desktop app keeps the 15 most recent managed worktrees by default. It skips worktrees tied to a pinned chat or a chat still running, and it saves a snapshot before deleting one, which you restore by reopening the chat. You can change the limit or turn automatic deletion off. Worktrees you create yourself with `git worktree add` are not managed this way.

## How Treq Handles It

Treq starts Codex as one of its supported agents, with edit access, inside a [Treq workspace](/docs/concepts/workspaces). Each workspace is its own working copy and branch under `.treq/workspaces/`, so Codex does not need `--worktree` there. Start one from the task input, or from the [CLI](/docs/reference/cli) with `treq agent start  "" -a codex`. Treq uses your installed Codex CLI and its existing sign-in.

## Sources

- [Codex worktrees](https://learn.chatgpt.com/docs/environments/git-worktrees)
- [Codex CLI commands](https://learn.chatgpt.com/docs/developer-commands?surface=cli)

## Related

- [Cursor worktrees](./cursor-worktrees.md)
- [GitHub Copilot worktrees](./github-copilot-worktrees.md)
- [Codex pricing](/learn/guides/codex-pricing): what several Codex agents in parallel cost
