Skip to main content

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:

codex --worktree

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

/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:

codex exec --worktree "implement the next step"
codex exec fork <SESSION_ID> --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. 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 with treq agent start <branch> "<prompt>" -a codex. Treq uses your installed Codex CLI and its existing sign-in.

Sources​