Skip to content

Isolated Codex tasks with Git worktrees

Codex worktrees isolate mutable files so parallel tasks can inspect, edit, install dependencies, and run tests without sharing one working directory. The Codex app can create a managed worktree for a task. Codex CLI 0.146.0 has no --worktree flag, so terminal workflows create the checkout with Git and start Codex inside it.

NeedUseBoundary
App-managed isolated taskCreate a Codex task with the worktree environmentStart from an explicit repository state and inspect the resulting diff
CLI session in an isolated checkoutgit worktree add, then run codex in that directoryGit owns creation, branch naming, listing, and removal
Non-Git directoryDirect local taskWorktree isolation is unavailable; copy or initialize only when policy allows

The app workflow is documented in Codex worktrees. CLI availability was checked with codex --help on 4 September 2026.

  1. Check the repository and current changes.

    Terminal window
    git status --short
    git branch --show-current
    git worktree list

    Do not base a new worktree on uncommitted changes unless you deliberately commit or otherwise transfer them.

  2. Create a branch and checkout.

    Terminal window
    git fetch origin
    git worktree add -b codex/fix-auth ../project-codex-fix-auth origin/main

    Replace the branch, path, and base with verified values. Never invent the default branch.

  3. Start Codex in the worktree.

    Terminal window
    cd ../project-codex-fix-auth
    codex

    Load project-specific environment setup without copying production credentials. Run separate ports for parallel dev servers.

  4. Verify and integrate.

    Run the repository gates, inspect git diff, commit on the task branch, and open a PR. Merge through the normal review path; do not apply several agent patches blindly.

  5. Remove only after preserving work.

    Terminal window
    git -C ../project-codex-fix-auth status --short
    git worktree remove ../project-codex-fix-auth
    git worktree prune

    The remove command refuses dirty worktrees by default. Do not add --force until the exact disposable state is proven.

Give every task:

  • one outcome and non-goals;
  • a distinct worktree and branch;
  • non-overlapping ownership where possible;
  • its own ports, caches, and local service state;
  • explicit shared-cloud prohibitions;
  • exact tests and a proof report;
  • an integration order and human owner.
Work only in this checkout and branch.
Implement the accepted plan.md without touching shared cloud state.
Run the documented gates and report commands plus exit codes.
Stop if another task changed the same interface or if the plan must change.
Do not merge, deploy, or remove the worktree.

A branch is already checked out elsewhere. Inspect git worktree list and create a distinct branch; Git permits one checked-out worktree per branch.

The task sees no local environment file. Use the repository’s documented bootstrap or copy only explicitly allowed gitignored development files. Never solve setup by copying production secrets.

Parallel servers collide. Assign a port block per worktree and pass it explicitly to the dev server and tests.

Changes conflict during integration. Rebase or merge in a human-chosen order, rerun the full gates, and update plan.md when the design changed.

  • Every running task maps to one path and branch in git worktree list.
  • Development ports and local databases are distinct.
  • No task can mutate shared production or third-party state.
  • Each task reports its own commands, exit codes, and diff.
  • Integration runs the combined test suite.
  • Cleanup preserves every wanted commit and branch.

Use the Codex AI-native SDLC adapter for plan, build, review, and automation. Recheck Codex developer commands before scripting CLI flags.