People searching git worktree claude code usually hit the same pain: one checkout, two agent sessions, and a working tree full of half-finished edits that neither session owns. Git worktrees give each session its own directory and branch while sharing a single object database. The pattern generalizes to any coding agent—Claude Code, Cursor Agent, Codex CLI, and similar tools—not only one vendor’s product.
01
Independence note
02
Why use worktrees for parallel agent sessions
A coding agent assumes it can edit files, run tests, and leave the tree in a coherent state. Two agents in one working directory race on the same paths, stash each other’s work, and produce diffs that mix unrelated intents. Worktrees fix the isolation problem without cloning the whole remote again.
- One branch per session: review and revert stay cheap.
- Shared objects: fetches and packs stay in one
.git. - Independent builds: each tree can run its own
node_modules/ build cache if you choose. - Human control: you still merge via PR; agents do not need write access to
main.
03
Basic git worktree commands
From your primary clone (illustrative paths—adjust to your machine):
# list existing worktrees
git worktree list
# create a sibling directory on a new branch
git worktree add -b agent/fix-login ../codemo-wt-fix-login main
# create from an existing remote branch
git worktree add ../codemo-wt-hotfix origin/hotfix/payment
# when the session is done and the branch is merged or abandoned
git worktree remove ../codemo-wt-fix-login
# if the directory was deleted by hand:
git worktree pruneKeep the primary worktree on a stable branch (often main) for pulls and release work; put agent sandboxes in sibling directories named after the task.
04
Directory layout suggestions
~/src/
codemo/ # primary worktree (main)
codemo-wt-fix-login/ # agent session A
codemo-wt-docs-seo/ # agent session B
codemo-wt-hotfix/ # short-lived firefight- Put worktrees beside the main clone, not inside it (avoids nested repo confusion).
- Encode the task in the folder name so humans and agents can tell sessions apart.
- Decide whether each worktree gets its own install (
pnpm install) or shares a global cache—agent-heavy trees often need their ownnode_modulesto avoid lock races. - Keep secrets out of the tree; use the same env / secret manager pattern in every worktree.
05
Branch and PR habits
- One agent session → one branch → one PR theme. Do not let a session “also fix” unrelated lint.
- Name branches with a prefix you can grep:
agent/…orwt/…. - Open the PR from the worktree after verify; paste the agent brief into the PR body for reviewers.
- Delete the local worktree after merge; prune stale metadata regularly.
- Rebase or merge
maininto the worktree before a long overnight agent run if the trunk moves fast.
For the wider delivery loop (brief → explore → change → verify → land), see the coding agent workflow section in the agent engineering series.
06
Conflicts and shared untracked-file pitfalls
- Same branch checked out twice: git refuses overlapping checkouts of one branch—use distinct branch names per worktree.
- Untracked shared paths: files ignored by git (local
.env, IDE metadata, large build artifacts) are not isolated by worktree magic if you symlink them carelessly across trees. - Port and lock collisions: two agents binding the same dev server port or writing the same global cache lock will still fight—give each session its own ports / temp dirs when needed.
- Forgotten worktrees: disk fills with half-dead trees;
git worktree listshould be part of weekly hygiene. - Submodules / LFS: initialize them per worktree; do not assume the primary tree’s checkout covers siblings.
07
Claude Code, Cursor, and other coding agents
Claude Code
Point each Claude Code session at a dedicated worktree directory (cwd = that tree). Brief the agent with the branch name and “do not switch branches.” Searching for git worktree + Claude Code mostly means this: isolate the session’s edits so a second chat cannot clobber the first.
Cursor
Open a separate Cursor window (or workspace) rooted at the worktree path. Keep project rules / AGENTS.md / skills in the repo so every worktree inherits them after checkout. Pair with cursor agent tips for briefing and review habits.
Codex and generic CLIs
Any CLI agent that edits files in cwd benefits from the same layout:cd into the worktree, run the agent, verify, then PR. Multi-config setups (separate profiles per task) combine cleanly with worktrees—see Codex CLI multiple configs if you juggle accounts or model settings.
08
When you should not use worktrees
- Single short task on a clean tree—ordinary branch checkout is enough.
- Huge generated artifacts that make every worktree a full disk clone unless you carefully share caches.
- Workflows that require exclusive access to a local hardware device or a singleton Docker volume.
- Teams that cannot agree on branch hygiene—worktrees amplify mess if nobody removes them.
Related reading: Agent engineering series · Multi-agent coding workflow · Spec-driven development for coding agents · Cursor agent tips · How to write a good agent prompt · AGENTS.md vs rules · How to write CLAUDE.md
09
FAQ
What is git worktree for Claude Code / coding agents?
It is a second (or third) checkout of the same repository on another branch and directory. Each coding agent session—Claude Code, Cursor, Codex, etc.—gets its own tree so parallel edits do not collide.
Is a full second clone better?
Clones work but duplicate objects and remotes. Worktrees share the object database and keep git fetch centralized. Prefer worktrees unless you need totally separate remotes or credentials.
Can two worktrees use the same branch?
Generally no for a normal checkout—git prevents checking out one branch in two places. Give each agent session its own branch.
Do worktrees replace good prompts and review?
No. Isolation stops file races; you still need clear briefs, verify commands, and human PR review. See good agent prompt and the workflow section.