Listen to today's AI briefing

Daily podcast — 5 min, AI-narrated summary of top stories

How Git Worktrees Fix Multi-Instance Claude Code Chaos
Open SourceScore: 93

How Git Worktrees Fix Multi-Instance Claude Code Chaos

A setup script and workflow for using git worktrees to safely run multiple Claude Code instances in parallel, with conflict recovery patterns.

GAla Smith & AI Research Desk·8h ago·4 min read·3 views·AI-Generated
Share:
Source: dev.tovia devto_claudecode, hn_claude_codeCorroborated

The Problem: Shared Directories Cause Silent Data Loss

Mastering Git Worktrees with Claude Code for Parallel Development ...

When you run multiple Claude Code instances against the same repository, you risk silent data loss. The root cause is simple: all instances share the same working directory. If Instance A has uncommitted changes and Instance B runs git pull --rebase, Instance A's work gets swept up and lost. If Instance C runs git stash, changes from A and B end up in C's stash, disappearing from their original context.

This isn't a theoretical issue—it's what happens when you scale Claude Code beyond a single instance. The traditional solution of carefully coordinating instances doesn't work with autonomous agents.

The Fix: Dedicated Worktree Per Instance

The solution is git worktree. Each Claude Code instance gets its own working directory and dedicated WIP branch, completely isolated from others.

Setup Script

Create .claude/scripts/setup-instance-worktree.sh:

#!/bin/bash
set -e

INSTANCE=$1
REPO_ROOT=$(git rev-parse --show-toplevel)
WORKTREE_DIR="$REPO_ROOT/.claude/worktrees/instance-$INSTANCE"
BRANCH="claude/${INSTANCE}-wip"

if [ -d "$WORKTREE_DIR" ]; then
  echo "worktree already exists: $WORKTREE_DIR"
  exit 0
fi

git worktree add "$WORKTREE_DIR" -b "$BRANCH" 2>/dev/null || \
  git worktree add "$WORKTREE_DIR" "$BRANCH"

echo "created: $WORKTREE_DIR (branch: $BRANCH)"

Usage:

bash .claude/scripts/setup-instance-worktree.sh ps1
bash .claude/scripts/setup-instance-worktree.sh vscode
bash .claude/scripts/setup-instance-worktree.sh win

This creates a structure like:

my_web_app/
  .claude/worktrees/
    instance-ps1/    ← branch: claude/ps1-wip
    instance-ps2/    ← branch: claude/ps2-wip
    instance-vscode/ ← branch: claude/vscode-wip

Key Configuration

Add worktrees to your local exclude file (not .gitignore):

# .git/info/exclude
.claude/worktrees/

View all active worktrees:

git worktree list

The Push Workflow: Direct to Main

Each instance works exclusively on its WIP branch and pushes directly to origin/main when ready. This is simpler than maintaining long-lived feature branches.

Recommended Push Pattern

From within the worktree directory:

git push origin HEAD:main

This pushes the currently checked-out branch to origin/main without needing to remember branch names.

Conflict Recovery (The 30-Second Fix)

When two instances push simultaneously, the second gets rejected. Recovery is one command:

git pull --rebase origin main && git push origin HEAD:main

Using rebase avoids merge commits and keeps your history clean.

Handling Specific Conflict Patterns

How to Use Git Worktrees in Claude Code: Parallel AI ...

1. Simultaneous ROADMAP.md Appends

If every instance appends to docs/GROWTH_STRATEGY_ROADMAP.md at session end, you'll get constant conflicts.

Fix: Assign fixed sections per instance:

### PS版#1 Session Log

### PS版#2 Session Log

### VSCode Session Log

Instances never touch each other's sections, so rebase auto-merges succeed.

2. Migration File Timestamp Collision

When instances create Supabase migration files simultaneously:

20260419220000_seed_foo.sql   ← created by PS#1
20260419220000_seed_bar.sql   ← created by Win at the same second

Fix: Check current max timestamp before creating:

ls supabase/migrations/ | grep $(date +%Y%m%d) | sort | tail -1
# → 20260419220000_seed_foo.sql

# New file: +10 seconds
touch supabase/migrations/20260419220010_seed_bar.sql

3. WIP Branch Far Behind Main

After a long session, origin/main might be 20+ commits ahead.

Diagnosis:

git log --oneline claude/ps1-wip..origin/main | wc -l
# If 20+, inspect before rebasing

Fix for heavy conflicts:

git rebase --abort
git fetch origin
git merge origin/main  # accept the merge commit
# resolve conflicts manually
git push origin HEAD:main

Key Rules for Multi-Instance Success

  1. Never work in the main repo directory — it's push target only
  2. git stash is banned — WIP commit instead (git commit -m "WIP")
  3. Commit before pull --rebase — staged-only changes can still be lost
  4. Edit → commit → push in a single bash invoke — prevents linter revert interference

Results You Can Expect

stash interference Changes from other instances disappear Each instance has its own stash pull --rebase Sweeps up other instances' uncommitted work Only affects the local wip branch Parallel push Collision / overwrite risk Always rebase-clean before push Debugging Unclear which instance made a change Traceable by wip branch name

git worktree isn't just a nice-to-have for multi-instance Claude Code—it's the design. One worktree + WIP branch per instance eliminates the entire class of "my changes disappeared" incidents.

Following this story?

Get a weekly digest with AI predictions, trends, and analysis — free.

AI Analysis

**Stop using shared directories immediately.** If you're running more than one Claude Code instance, create worktrees today. The setup script takes 30 seconds to implement and prevents hours of debugging lost changes. **Adopt the `HEAD:main` push pattern.** This is simpler than managing long-lived feature branches. Each instance becomes a temporary worker that does its job and pushes directly to main. Conflicts are rare and recoverable with a single rebase command. **Assign instance-specific sections in shared files.** The ROADMAP.md example shows a pattern you should apply to any file that multiple instances might modify. Create clear ownership boundaries within files to avoid unnecessary merge conflicts. This is especially important for log files, session notes, or any append-only documentation.

Mentioned in this article

Enjoyed this article?
Share:

Related Articles

More in Open Source

View all