Launching Codex from the wrong folder can load the wrong project files, instructions, and Git context. Use the working-directory flag when the parent shell should stay where it is but the Codex run needs to start inside a specific repository or scratch folder.

The -C and --cd flags select the working root before Codex processes the prompt. Relative paths, project instructions, project-scoped .codex/config.toml layers, and repository context are then discovered from that selected directory.

For codex exec, the repository guard still applies after the working root is selected. A Git repository passed with -C passes that guard, while an intentional scratch directory needs --skip-git-repo-check; use --add-dir only when the session needs an extra writable path beside the main workspace.

Steps to set the Codex working directory:

  1. Choose the absolute path that should own the Codex session.
    $ git -C /home/user/work/example-repo rev-parse --show-toplevel
    /home/user/work/example-repo

    Use a repository root when the task should load project instructions, Git status, and project-scoped Codex config from that tree.

  2. Start an interactive Codex session with that directory as the working root.
    $ codex -C /home/user/work/example-repo

    The terminal UI should open with the directory line set to ~/work/example-repo or the absolute target path. -C and --cd are equivalent.

  3. Run a non-interactive Codex task with the same working root.
    $ codex exec -C /home/user/work/example-repo --sandbox read-only "Return OK."
    OpenAI Codex v0.139.0
    --------
    workdir: /home/user/work/example-repo
    sandbox: read-only
    ##### snipped #####

    The workdir line confirms that Codex switched to the chosen directory before processing the prompt. --sandbox read-only keeps the check from writing files.

  4. Add the repository-check override only for an intentional scratch directory.
    $ codex exec -C /home/user/tmp/codex-scratch --skip-git-repo-check --sandbox read-only "Return OK."
    OpenAI Codex v0.139.0
    --------
    workdir: /home/user/tmp/codex-scratch
    sandbox: read-only
    ##### snipped #####

    Without --skip-git-repo-check, codex exec stops outside Git with Not inside a trusted directory and --skip-git-repo-check was not specified. Do not use the override for normal repository work.

  5. Use --add-dir only for extra writable paths.
    $ codex -C /home/user/work/example-repo --sandbox workspace-write --add-dir /home/user/shared-notes

    -C changes the primary workspace. --add-dir adds a separate path that Codex can write alongside it.