← Blog

How-to5 min read

Running two coding agents in one repository

Run a second Claude Code or Codex session on the same checkout and the two overwrite each other. Git worktrees let any Mira session start in its own.

A Mira session with its own file tree and editor for the workspace it runs inA Mira session with its own file tree and editor for the workspace it runs in

What actually goes wrong when two agents share a working tree

Running one coding agent in a repository is a solved problem, but running two at once is not. Both agents operate on the same working tree without knowing the other exists, overwriting and mixing each other's changes. Nothing crashes, which makes the failure quieter and worse: you end up with a diff that neither agent intended and no one can explain.

  1. Agent A is editing files

    Both agents operate in the same shared working tree

  2. Agent B checks out a branch

    Agent B may also stage a file that Agent A just rewrote

  3. Tests run on half-written changes

    Git raises no error while the two changes mix

  4. You get a diff neither agent intended
Clone the repo for each agent
Duplicates history and isolates branches
Run the agents one after another
Gives up the point of using two agents
Tell each: “Only these files”
No enforcement; agents can still cross

The three usual workarounds for running two agents in one repository—separate clones, taking turns, or verbal file rules—are all worse than they look.

Linked worktrees: give one repository several independent checkouts

Git has had the answer since version 2.5. Running git worktree add creates another working directory backed by the same object database and refs—a second checkout, not a second repository. Each worktree sits on its own branch with its own index and HEAD, so two agents working in separate worktrees cannot see each other's uncommitted changes, stage each other's files, or switch each other's branch.

One shared Git repositoryAll worktrees share the objects, history, remotes, and branches
  • ../feature-a
    1. Worktree checked out on branch feature-a
    2. Has its own staging index and HEAD
    3. Agent A edits files here in isolation
  • ../feature-b
    1. Worktree checked out on branch feature-b
    2. Has its own staging index and HEAD
    3. Agent B edits files here in isolation
Because the worktrees share everything that is expensive to copy, the work stays mergeable: Agent A can branch from what Agent B pushed an hour ago, and you can review both diffs against the same base.
You can set up the same isolation by hand in the terminal by creating a separate linked worktree and branch for each coding agent.
# one repository, two checkouts, two branches
git worktree add ../feature-a -b feature-a
git worktree add ../feature-b -b feature-b

# each has its own HEAD and index; the objects are shared
git worktree list

Built-in worktrees in Claude Code and Codex

Both Claude Code and Codex now ship worktree support so you don't have to run git worktree add by hand every time.

Claude Code (--worktree / -w)
Run claude --worktree feature-auth (or omit the name for a generated one) to create a worktree under .claude/worktrees/<name>/ on branch worktree-<name>, or ask Claude mid-session to “work in a worktree”. Add .claude/worktrees/ to .gitignore. On exit, a clean unnamed worktree is removed automatically; one with edits or commits prompts you to keep or remove it, and you can return with claude --worktree <name> --resume <session-id>.
Codex (Worktree mode and Hand off)
In the ChatGPT desktop app, select **Worktree** under the composer, choose the starting branch (or your current branch with unstaged edits), and optionally pick a local environment to run setup scripts. Codex starts on a detached HEAD by default. When you're ready, either click **Create branch here** to commit and push from the worktree, or click **Hand off** to move both chat and changes into your Local checkout.
Starting, resuming and cleaning up Claude Code worktrees from the CLI
# Start two isolated Claude Code sessions in separate terminals
claude --worktree feature-auth
claude --worktree fix-billing

# Return to a kept worktree later
claude --worktree feature-auth --resume <session-id>

# Remove a worktree left behind by a non-interactive -p run
git worktree unlock .claude/worktrees/<name>
git worktree remove .claude/worktrees/<name>

A new worktree has no .env and no node_modules

A worktree is a fresh checkout, so the files git ignores aren't there: no .env, no node_modules, no build output. To carry ignored files over, list them in a .worktreeinclude file at the repository root, in .gitignore syntax. Claude Code and Codex both read it, and only copy files that git ignores.

text
# .worktreeinclude
.env
.env.local
config/secrets.json
Claude Code
Copies the files in .worktreeinclude into every worktree it creates. Dependencies are up to you: install them in the worktree, or ask Claude to.
Codex
Copies the files in .worktreeinclude into the worktrees it manages. In the ChatGPT desktop app, a local environment's setup script runs when Codex creates a new worktree.
Mira
Copies the files in .worktreeinclude, or the ignored .env files at the root when there is none, and runs one setup script: one you write in Mira, or the one already set up in Conductor, Cursor, Orca or T3 Code. The first time, it lists every file and command and asks.

The setup script runs in the background, so the agent can start its first turn while dependencies install.

Why Mira builds linked worktrees into sessions

Managing worktrees by hand
In a Mira session
Creating a worktree
Run git worktree add manually for one branch at a time
Pick a base branch and start the session in its own worktree
Tracking branches and files
You have to remember which terminal is on which branch
Each pane shows that worktree's files, terminal, diff, and commits
How agents stay isolated
Relies on each agent obeying a rule not to touch other directories
Each pane works in its own isolated worktree directory
Merging the agents' changes
You review the diffs and merge the branches yourself
You still review the diffs and merge the branches yourself
Managing worktrees by hand works for two branches but gets tedious for six, whereas Mira lets you run several sessions at once, each isolated in its own worktree.

Parallel sessions in Mira mean several coding agents working at the same time, each on its own task. Mira does not act as an orchestrator that splits one task among worker agents. Nothing plans across panes or merges their code for you—you review the diffs side by side and merge them yourself.

Questions

Why does Codex start a worktree in detached HEAD?
Git only allows a branch to be checked out in one worktree at a time. Starting detached lets Codex base multiple worktrees on the same branch without colliding with your main checkout; click Create branch here when you want to commit and push.
Do Claude Code worktrees clean themselves up?
For interactive sessions, an unnamed worktree with no changes is removed on exit, while one with changes or commits asks whether to keep or remove it. Non-interactive -p runs don't prompt and leave their worktrees locked until you unlock and remove them.
Can I run Claude Code and Codex in worktrees side by side?
Yes. Because git worktrees are standard Git checkouts, you can point Claude Code at one and Codex at another — or start both as separate sessions in Mira, each with its own worktree, terminal, and diff pane.

Sources

Checked on November 29, 2026 against Anthropic's worktrees guide (code.claude.com/docs/en/worktrees) and OpenAI's Codex worktrees and local environments docs (learn.chatgpt.com/docs/environments/git-worktrees and local-environment). Check those pages for the latest flags and options.

Keep reading