power user

Git Worktrees with Claude Code: Parallel Branches Without Conflicts

Quick Answer

Git worktrees give each Claude task its own working directory checked out on a separate branch. Create one with 'git worktree add ../myproject-feature feature-branch', run Claude in that directory, and your main checkout is untouched. Merge when done.

Git worktrees are the power user's secret weapon for Claude Code. A worktree is a second (or third, or tenth) checkout of your repository, each on a different branch, all sharing the same .git directory. You can have Claude writing a new feature in one worktree while you debug a bug in your main checkout and another Claude instance fixes tests in a third worktree — all simultaneously.

Without worktrees, running a background Claude task means stashing your changes, checking out a branch, running Claude, then unstashing — or risking Claude modifying your work-in-progress files. With worktrees, each Claude session has its own isolated directory and branch, so there is zero interference.

The workflow is: create a worktree on a new branch, cd into it, start Claude with the task, let it run (interactively or headless), review the result with git diff or git log, then merge the branch back to main. Clean up with 'git worktree remove'.

For parallel agent workflows, the pattern is to create N worktrees programmatically, launch N background Claude processes (one per worktree), wait for them all to complete, then review and merge results. This is how you scale Claude Code from one task to many simultaneously.

Examples

Basic worktree setup and teardownbash
# Create a worktree on a new branch
git worktree add ../myproject-feature feature/add-dark-mode

# Run Claude in the worktree
cd ../myproject-feature
claude "Add a dark mode toggle to the header component. Use the existing CSS variable system."

# Back in main checkout — review changes
git log feature/add-dark-mode --oneline -5
git diff main...feature/add-dark-mode

# Merge if happy
git merge feature/add-dark-mode

# Clean up
git worktree remove ../myproject-feature
git branch -d feature/add-dark-mode
Launch parallel Claude tasks across worktreesbash
#!/bin/bash
# parallel-claude.sh — run multiple Claude tasks in parallel worktrees

REPO=$(git rev-parse --show-toplevel)
BASE=$(basename $REPO)
TIMESTAMP=$(date +%Y%m%d-%H%M%S)

declare -A TASKS
TASKS["add-tests"]="Write unit tests for all functions in src/utils/ that don't have tests yet"
TASKS["fix-types"]="Fix all TypeScript type errors reported by tsc --noEmit"
TASKS["update-docs"]="Update README.md to reflect the current API surface"

PIDS=()

for NAME in "${!TASKS[@]}"; do
  BRANCH="claude/${NAME}-${TIMESTAMP}"
  WORKTREE="${REPO}/../${BASE}-${NAME}"
  TASK="${TASKS[$NAME]}"

  git worktree add "$WORKTREE" -b "$BRANCH"
  
  (
    cd "$WORKTREE"
    claude --headless --print "$TASK" > "/tmp/claude-${NAME}.log" 2>&1
    echo "[DONE] $NAME → $BRANCH"
  ) &
  PIDS+=("$!")
done

# Wait for all tasks
for PID in "${PIDS[@]}"; do wait $PID; done
echo "All tasks complete. Review branches and merge."

Tips

  • Name worktree directories clearly: '../myproject-fix-auth' is easier to manage than '../myproject-wt1'.
  • Use 'git worktree list' to see all active worktrees and their branches.
  • Each worktree needs its own node_modules if you are in a Node project — run 'npm install' in the new worktree before starting Claude.
  • Worktrees share .git history but have separate working directories — changes in one worktree never affect another.
  • Set up a shell alias: 'alias cwt="git worktree add"' to speed up the creation step.

FAQ

Can two Claude sessions run in the same worktree simultaneously?+

Technically yes, but it will cause conflicts. Always use one worktree per Claude session. If you need Claude to work on the same files from two angles, use separate worktrees on separate branches and merge carefully.

Do I need to reinstall dependencies in each new worktree?+

For Node.js projects, yes — each worktree needs its own node_modules. A workaround is to symlink node_modules from the main checkout if dependencies are identical: 'ln -s /path/to/main/node_modules ./node_modules'. For Python, use the same venv if the dependencies match.

What happens to a worktree if the branch is deleted?+

The worktree becomes 'orphaned' but the directory still exists with its files. You can prune orphaned worktrees with 'git worktree prune'. Always remove worktrees with 'git worktree remove' before deleting the branch.

Related Guides