Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Workflow recipe

Refactor in parallel with git worktrees and two agents

Use git worktrees to run Claude Code and Codex in parallel on the same refactor from one written plan, compare tests, diff size and readability, then merge one.

By Checked Advanced1 h 30 min6 steps
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark
On this page

To run two coding agents in parallel, give each its own git worktree so neither can overwrite the other’s files. Write one plan and commit it, check the test baseline, start Claude Code in one worktree and Codex in the other with the same prompt, compare the results on tests, diff size and readability, and merge the better branch.

A git worktree is a second working directory for the same repository, on its own branch. Two agents in two worktrees cannot overwrite each other’s files, so you can give both the same refactor and compare the results. For a refactor with more than one reasonable design, seeing two attempts side by side is often quicker than steering one agent through a second try.

This recipe runs Claude Code in one worktree and Codex in another, from the same written plan, then compares them on tests, diff size and readability before merging one.

EditorAbout 15 min

1. Write the plan both agents will follow

The comparison is only fair if both agents get the same brief. Write it as a file in the repository, with the goal, the constraints and how to tell it is done.

# Refactor: split src/orders/service.ts

## Goal
service.ts is 1,400 lines and mixes validation, pricing and persistence.
Split it into src/orders/validation.ts, pricing.ts and repository.ts,
with service.ts keeping only orchestration.

## Constraints
- No behaviour changes. Public exports from src/orders/index.ts stay the same.
- No new dependencies.
- Keep each commit small and passing: move code first, then tidy.

## Done when
- npm test and npm run check pass.
- service.ts is under 300 lines.
- No file in src/orders imports from service.ts except index.ts.

If you use a planning skill such as writing-plans, have an agent draft this file and then edit it yourself.

TerminalAbout 5 min

2. Check the baseline, commit the plan and make it reachable

Both worktrees must start from a commit that contains the plan.

git switch main && git pull
npm ci && npm test           # the suite must pass before you start
git add plans/split-orders-module.md
git commit -m "Add plan for splitting the orders module"

Add the worktree folder to .gitignore so it does not show up as untracked files:

echo ".claude/worktrees/" >> .gitignore
git add .gitignore && git commit -m "Ignore agent worktrees"

Then make sure Claude Code’s worktree will see those commits. claude --worktree does not branch from your local HEAD: by default it branches from the repository’s default branch on the remote (origin/HEAD). When we re-ran this step, a plan committed locally but not pushed was missing from the new worktree, while a git worktree add worktree had it. Either push the commits first (git push), or tell Claude Code to branch from your current work in .claude/settings.json:

{
  "worktree": {
    "baseRef": "head"
  }
}

Run claude once in the repository before step 3 if you have not used it there; an interactive --worktree session needs the folder to be trusted and exits with an error otherwise.

Two terminalsAbout 5 min

3. Start one agent in each worktree

In the first terminal, start Claude Code in a new worktree. The --worktree flag creates .claude/worktrees/orders-claude/ on a new branch worktree-orders-claude:

claude --worktree orders-claude

In the second terminal, create a worktree with git and start Codex in it:

git worktree add -b refactor/orders-codex ../repo-orders-codex
cd ../repo-orders-codex
npm ci
codex

Each worktree is a fresh checkout, so install dependencies in both. Copy any untracked files the tests need, such as .env.test. Before you paste the prompt, ask each agent to cat plans/split-orders-module.md; if the file is missing, the worktree started from the wrong commit (see step 2).

Both agentsAbout 40 min

4. Give both agents the same prompt

Refactor brief · Claude Code and Codex
Follow the plan in plans/split-orders-module.md exactly. Work in small steps. After each step run npm test and npm run check, and commit with a message describing the step. If a step breaks tests you cannot fix within two attempts, stop and explain. When finished, write a short summary: files created, what moved where, anything you were unsure about, and the final line count of src/orders/service.ts.

Leave them to work. Check in occasionally, but avoid steering one more than the other, or the comparison stops being useful. If one gets stuck, note where; that is information too.

TerminalAbout 20 min

5. Compare the two results

From the main checkout, compare each branch with main:

git diff --stat main...worktree-orders-claude
git diff --stat main...refactor/orders-codex
git log --oneline main..worktree-orders-claude
git log --oneline main..refactor/orders-codex

Then run the checks in each worktree yourself, rather than trusting the agents’ summaries:

(cd .claude/worktrees/orders-claude && npm test && npm run check)
(cd ../repo-orders-codex && npm test && npm run check)
wc -l .claude/worktrees/orders-claude/src/orders/*.ts ../repo-orders-codex/src/orders/*.ts

Score both against the plan: tests and checks pass, public exports unchanged (git diff main...<branch> -- src/orders/index.ts should be empty), size targets met, commits small and readable, and how easy the new files are to read. You can also ask a third, fresh agent session to review both diffs against the plan and list differences, but make the decision yourself.

TerminalAbout 5 min

6. Merge the better branch and clean up

Open a pull request from the branch you chose, and mention in the description that an alternative existed and why you picked this one. If the other branch had a good idea, such as a better file name or an extra test, cherry-pick that commit or note it as a follow-up.

Then remove the worktrees and the losing branch:

git worktree remove ../repo-orders-codex
git branch -D refactor/orders-codex        # if you merged the Claude branch
git worktree list                          # check what is left

Exiting the Claude Code session prompts you to keep or remove its worktree when it holds work, and removes a clean one. A worktree from a non-interactive -p run is left behind and locked: in our check, git worktree remove refused with “cannot remove a locked working tree”. Unlock it first:

git worktree unlock .claude/worktrees/orders-claude
git worktree remove .claude/worktrees/orders-claude
git branch -D worktree-orders-claude      # if you merged the Codex branch

Check it worked

Verification checklist

0 of 7 done

When it goes wrong

If tests fail in a worktree but pass in your main checkout, the worktree is missing something untracked: dependencies, environment files or generated code. Claude Code can copy gitignored files into new worktrees automatically if you list them in a .worktreeinclude file.

If both agents edit the same shared resource, such as a local database or a dev server port, they can interfere even in separate worktrees. Give each its own database name or port in the prompt.

If one agent finishes in ten minutes and the other is still going after an hour, look at its commits. Long runs usually mean it hit failing tests and is looping; stop it and read where it got stuck.

If the agent in the Claude Code worktree cannot find the plan, or its code is a commit behind, the worktree branched from origin/HEAD rather than your local commit. Push the plan or set worktree.baseRef to "head", remove the worktree and start again.

If git worktree remove refuses because the worktree is locked, it was left by a non-interactive run; git worktree unlock it first. If it refuses because of uncommitted changes, check whether there is anything worth keeping, then use --force.

Variations

You can run two sessions of the same agent with different instructions instead, for example “minimal moves” against “also rename for clarity”, and compare the trade-off.

Claude Code can also run subagents in their own worktrees inside a single session, which suits splitting one large task into independent parts rather than comparing two attempts at the same one. Add isolation: worktree to a subagent’s frontmatter in .claude/agents/, and each run gets a temporary worktree that is removed if it made no changes.

To split one large change into parallel batches rather than compare two attempts, see migrate JavaScript to TypeScript with a coding agent, which runs leaf modules in separate worktrees and shows the merge conflict that adjacent import lines cause. The Using Git worktrees and Finishing a development branch skills give an agent the same set-up and clean-up routine.

The git worktree entry has the short definition, and subagents in Claude Code is the other way to split work inside one session. Claude Code vs Codex vs Cursor compares the agents themselves, and the spec-driven feature build shows how to write a plan worth following. The AI-assisted software engineering path puts this in sequence. More recipes are in the AI workflow examples hub.

Outcome and tools

Outcome
Two attempts at the same refactor on separate branches, scored against one written plan, with the better one in a pull request and the worktrees removed.
For
Developers
Variants for
Claude Code, Codex
Back to top