HerdbellBlog

Guide

How to use git worktrees to run Claude Code and Codex in parallel

By the Herdbell team · Published · Updated

Short answerRun Claude Code and Codex on one repo without conflicts: a git worktree per agent, claude --worktree, merging and cleanup, and how to track who needs you.

Two agents in the same folder will sooner or later edit the same file, run the same build or switch the branch under each other. A git worktree fixes that: it is a second checkout of the same repository, on its own branch, in its own folder. Each agent gets one, and their work only meets when you merge. The commands below come from the git, Claude Code and Codex documentation as of November 2026.

Why parallel agents collide

  • Both agents see the same working files, so one can overwrite the other's half-finished change.
  • A folder has one checked-out branch, so a branch switch by one agent changes the files under the other.
  • Builds, test runs and lock files clash when both run at once.

Create a worktree per agent

  1. git worktree add ../myapp-auth -b feature-auth creates a new folder on a new branch.
  2. git worktree add ../myapp-tests -b add-tests does the same for the second agent.
  3. git worktree list shows every worktree and its branch.

Git will not check out a branch that another worktree already has checked out, so give each agent its own branch.

Start Claude Code and Codex in each one

  • Claude Code: run claude inside ../myapp-auth. Or let Claude Code make the worktree: claude --worktree feature-auth (or -w) creates it under .claude/worktrees/feature-auth on a branch named worktree-feature-auth.
  • Codex app: choose Worktree under the composer and pick a starting branch. Codex creates and manages the worktree for that chat.
  • Codex CLI: run codex inside ../myapp-tests.

Claude Code needs at least one commit in the repository before it can create a worktree. Add .claude/worktrees/ to your .gitignore.

Merge and clean up

  1. Review and commit the work inside each worktree.
  2. Back in your main checkout, merge the branch, for example git merge feature-auth.
  3. Remove the worktree with git worktree remove ../myapp-auth. Git only removes clean worktrees unless you add --force.
  4. If you deleted a worktree folder by hand, run git worktree prune to clear what git still remembers about it.

When you exit an interactive Claude Code session started with --worktree, it offers to remove the worktree, and removing deletes any work left in it. Runs with -p do not clean up, so remove those yourself.

Track which agent needs you

Worktrees solve file conflicts, not the other problem: knowing which agent is waiting for you. With three or four sessions in different folders, one is usually stuck on a permission prompt in a window you are not looking at. Herdbell lists Claude Code, Codex, Cursor and Antigravity chats in one Mac menu bar list, makes your pointer glow when one needs you, and lets you answer from a popup at your cursor. See how to run multiple Claude Code sessions and Claude Code session manager for Mac.

Common questions

What is a git worktree?

A second working folder for the same repository, with its own checked-out branch. All worktrees share one history, so a commit made in one is visible from the others.

Can Claude Code create worktrees itself?

Yes. claude --worktree <name> (or -w) creates a worktree under .claude/worktrees/ on a new branch and starts the session there.

Does Codex support git worktrees?

Yes. In the Codex app, choose Worktree under the composer. Codex keeps your recent worktrees and cleans up older ones.

Can two worktrees use the same branch?

No. Git refuses to check out a branch that another worktree already has, unless you force it. Give each agent its own branch.