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.
Related: How to start a Codex interactive session
Related: How to run a command with Codex exec
Related: How to start a Codex task in a worktree
Steps to set the Codex working directory:
- 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.
- 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.
- 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.
Related: How to run a command with Codex exec
- 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.
- 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.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.