Run 5 Claude Code instances in parallel using git worktree
Author kanta13jp1 demonstrates how `git worktree` eliminates cross-instance conflicts when running 5 Claude Code instances in parallel, replacing risky `git stash` usage with isolated working directories and WIP commits.
Score breakdown
Use `git worktree` to give each Claude Code agent its own isolated directory so parallel agentic workflows never silently overwrite each other's uncommitted changes.
- 01Running 5 Claude Code instances in a shared working directory causes `git stash` in one instance to capture and destroy uncommitted changes belonging to other instances.
- 02`git worktree add .claude/worktrees/instance-<name> -b claude/<name>-wip origin/main` gives each instance a fully isolated working directory on its own branch.
- 03A stash or rebase inside one worktree has zero effect on files in any other worktree.
When five Claude Code instances share a single working directory, `git stash` becomes a silent data-loss trap. The author illustrates a concrete failure mode: a PS instance runs `git stash → git pull --rebase → git stash pop` while a Win instance has uncommitted edits to `lib/pages/horse_racing.dart`. Because `git stash` operates on the entire working directory with no concept of process ownership, the Win instance's changes are captured and effectively lost. The root cause is architectural — shared state between independent agents.
The fix is to give each instance a dedicated worktree using `git worktree add .claude/worktrees/instance-ps -b claude/ps-wip origin/main` (repeated per instance).
The fix is to give each instance a dedicated worktree using `git worktree add .claude/worktrees/instance-ps -b claude/ps-wip origin/main` (repeated per instance). Each worktree is a fully independent working directory on its own branch, so a stash or rebase in the PS worktree has zero effect on the Win worktree. The author also replaces `git stash` with WIP commits (`git add -A && git commit -m "WIP: $(date +%H:%M)"`), reverting with `git reset HEAD~1` when needed, making the workflow entirely stash-free. A critical operational guard is verifying the working root with `git rev-parse --show-toplevel` — if it returns the main repo path instead of the instance-specific worktree path, the instance must `cd` to its worktree immediately. File writes to the wrong path (e.g., `/repo/docs/...` instead of `/repo/.claude/worktrees/instance-ps/docs/...`) can corrupt another instance's in-progress work. At five or more parallel agents, the author concludes, worktree isolation is "not optional — it's the foundation."
Key facts
- 01Running 5 Claude Code instances in a shared working directory causes `git stash` in one instance to capture and destroy uncommitted changes belonging to other instances.
- 02`git worktree add .claude/worktrees/instance-<name> -b claude/<name>-wip origin/main` gives each instance a fully isolated working directory on its own branch.
- 03A stash or rebase inside one worktree has zero effect on files in any other worktree.
- 04WIP commits (`git add -A && git commit -m "WIP: $(date +%H:%M)"`) replace `git stash` entirely; changes are unstaged later with `git reset HEAD~1`.
- 05`git rev-parse --show-toplevel` must return the instance-specific worktree path, not the main repo path, before any file writes.
- 06Writing files to the main repo path instead of the correct worktree path can corrupt another instance's in-progress work.