Running multiple Claude Code agents in one repo with git worktree A developer described using git worktree to run multiple Claude Code agents in a single repository without file conflicts, since Git refuses to check out the same branch in two worktrees. The setup requires manually recreating ignored files like .env and node_modules in each worktree and assigning a unique dev-server port per copy, which the developer automated with a roughly 25-line shell script. The approach guarantees two agents cannot write to the same files by giving each its own branch and directory. I once had two Claude Code sessions open in the same repository. It lasted about four minutes before both were editing src/config.ts , and neither one knew the other existed. The fix isn't a cleverer prompt or a plugin. It's git worktree — a Git feature that has shipped since 2.5 — plus roughly 25 minutes of setup to handle the two things that actually break: ignored files and port collisions. Here's the flow I use now, from a clean tree to two agents working side by side. git worktree add takes ten seconds. The pain arrives afterwards. Ignored files are never copied. Git checks out tracked content only. .env , node modules , local SQLite files, build caches — none of them exist in the new directory. Launch an agent there and it burns its first ten minutes chasing a module-not-found error instead of your task. Ports collide. Two npm run dev processes both grabbing 3000 means one of them dies at startup. Every working copy needs its own port. And there's one rule you can't negotiate around: Git refuses to check out the same branch in two worktrees. One branch, one agent. That's not an annoyance — it's the guarantee that two agents physically cannot write to the same files. What ends up shared and what doesn't: | Item | Shared between worktrees? | What you have to do | |---|---|---| | History, branches, tags | Yes — one object store | nothing | | Checked-out files, uncommitted edits | No — separate directories | commit or stash first | | Ignored files .env , node modules , .sqlite | No — never copied | recreate in each worktree | | Dev servers and ports | No — separate processes | assign a unique port per copy | | The Claude Code process and terminal | No | one terminal per worktree | git --version and git worktree list . The second command must print at least one line. If it says git: 'worktree' is not a git command , your Git is too old. git status --porcelain should print nothing. Anything else: commit or git stash before continuing, because uncommitted changes stay in the main worktree and are invisible to the new one. claude --version . If the binary is missing, follow the official install docs for your OS — package names and flags change between releases, so don't paste a command from an old blog post. node modules that's several hundred MB each; the .git object store is shared, so history isn't duplicated. From your repo root, create sibling directories with symmetric names so you can tell them apart at a glance: cd /home/dev/acme-api git worktree add ../acme-api-auth -b agent/auth git worktree add ../acme-api-billing -b agent/billing Each command should print something like: Preparing worktree new branch 'agent/auth' HEAD is now at 4f2c1ab chore: bump deps If you'd rather not repeat that by hand, drop this into scripts/new-agent-worktree.sh and chmod +x it: bash /usr/bin/env bash set -euo pipefail NAME="$1" PORT="$2" REPO="$ git rev-parse --show-toplevel " WT="$ dirname "$REPO" /$ basename "$REPO" -$NAME" git worktree add "$WT" -b "agent/$NAME" ignored files are NOT copied by git worktree — do it manually -f "$REPO/.env" && cp "$REPO/.env" "$WT/.env" printf '\nPORT=%s\n' "$PORT" "$WT/.env" recreate dependencies inside the new working tree -f "$WT/package.json" && cd "$WT" && npm ci echo "worktree: $WT" echo "port: $PORT" Usage: ./scripts/new-agent-worktree.sh auth 3001 and ./scripts/new-agent-worktree.sh billing 3002 . Then verify you got what you asked for: git worktree list Three lines, one per path and branch: /home/dev/acme-api 4f2c1ab main /home/dev/acme-api-auth 4f2c1ab agent/auth /home/dev/acme-api-billing 4f2c1ab agent/billing If you only see one line, the add failed — scroll up in the output. In my experience it's nearly always a directory that already exists, or a branch that's already checked out somewhere else. Do it by hand once, in the same order the script does, so you know what's happening: cd /home/dev/acme-api-auth cp ../acme-api/.env .env ignored file: git did not copy it printf 'PORT=3001\n' .env unique port for this copy npm ci own node modules npm run dev terminal 1, stays running In a second terminal, from the same directory, start the agent: cd /home/dev/acme-api-auth claude Repeat in /home/dev/acme-api-billing with PORT=3002 . Checklist I run through before letting an agent write anything: - branch name is unique agent/auth, agent/billing - .env copied, and its DATABASE URL/state path points at this copy - dependencies installed inside this worktree - dev server port differs from every other running copy - pwd in the terminal prints this worktree, not the main repo - git status in the main repo is still clean From a third terminal, confirm both servers answer on distinct ports: curl -s -o /dev/null -w "%{http code}\n" http://localhost:3001/health curl -s -o /dev/null -w "%{http code}\n" http://localhost:3002/health Two 200 s means both copies are alive and isolated. Then make sure the agent is anchored where you think it is: cd /home/dev/acme-api-auth && git rev-parse --show-toplevel Expected: /home/dev/acme-api-auth . If it prints /home/dev/acme-api , you started the agent in the main repository and it will happily edit shared files. Don't take the isolation on faith — test it. Ask the auth agent to modify src/config.ts , then look at all three trees: git -C /home/dev/acme-api-auth status --short git -C /home/dev/acme-api-billing status --short git -C /home/dev/acme-api status --short The first command lists M src/config.ts . The other two print nothing at all. That empty output is the entire point — the edit exists in exactly one working tree. Finish the task through the normal flow: cd /home/dev/acme-api-auth git add -A && git commit -m "feat auth : rotate session tokens" git push -u origin agent/auth Open the pull request, and once it's merged, remove the copy: cd /home/dev/acme-api git worktree remove ../acme-api-auth git worktree prune Running parallel agents is a Git layout problem more than an AI problem. Branch-per-agent gives you a hard wall between sessions, and the actual work lives in the boring details: copy the ignored files, install dependencies inside each worktree, hand out a unique port per copy, and verify with git rev-parse --show-toplevel that every agent is standing in the right directory. Get those four things right and git worktree does the rest for free. This article was originally published on AI Release — https://ai-release.org https://ai-release.org , where I write up tooling experiments like this one. Shorter notes and new guides also land in the channel: https://t.me/ai release1 https://t.me/ai release1 .