{"slug": "running-multiple-claude-code-agents-in-one-repo-with-git-worktree", "title": "Running multiple Claude Code agents in one repo with git worktree", "summary": "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.", "body_md": "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.\n\nThe 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.\n\nHere's the flow I use now, from a clean tree to two agents working side by side.\n\n`git worktree add` takes ten seconds. The pain arrives afterwards.\n\n**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.\n\n**Ports collide.** Two `npm run dev` processes both grabbing 3000 means one of them dies at startup. Every working copy needs its own port.\n\nAnd 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.\n\nWhat ends up shared and what doesn't:\n\n| Item | Shared between worktrees? | What you have to do | \n|---|---|---|\n| History, branches, tags | Yes — one object store | nothing | \n| Checked-out files, uncommitted edits | No — separate directories | commit or stash first | \n| Ignored files ( `.env` ,`node_modules` ,`*.sqlite` ) | No — never copied | recreate in each worktree | \n| Dev servers and ports | No — separate processes | assign a unique port per copy | \n| The Claude Code process and terminal | No | one terminal per worktree | \n\n`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.\nFrom your repo root, create sibling directories with symmetric names so you can tell them apart at a glance:\n\n```\ncd /home/dev/acme-api\ngit worktree add ../acme-api-auth    -b agent/auth\ngit worktree add ../acme-api-billing -b agent/billing\n```\n\nEach command should print something like:\n\n```\nPreparing worktree (new branch 'agent/auth')\nHEAD is now at 4f2c1ab chore: bump deps\n```\n\nIf you'd rather not repeat that by hand, drop this into `scripts/new-agent-worktree.sh` and `chmod +x` it:\n\n``` bash\n#!/usr/bin/env bash\nset -euo pipefail\nNAME=\"$1\"\nPORT=\"$2\"\nREPO=\"$(git rev-parse --show-toplevel)\"\nWT=\"$(dirname \"$REPO\")/$(basename \"$REPO\")-$NAME\"\n\ngit worktree add \"$WT\" -b \"agent/$NAME\"\n\n# ignored files are NOT copied by git worktree — do it manually\n[ -f \"$REPO/.env\" ] && cp \"$REPO/.env\" \"$WT/.env\"\nprintf '\\nPORT=%s\\n' \"$PORT\" >> \"$WT/.env\"\n\n# recreate dependencies inside the new working tree\n[ -f \"$WT/package.json\" ] && (cd \"$WT\" && npm ci)\n\necho \"worktree: $WT\"\necho \"port:     $PORT\"\n```\n\nUsage: `./scripts/new-agent-worktree.sh auth 3001` and `./scripts/new-agent-worktree.sh billing 3002`.\n\nThen verify you got what you asked for:\n\n```\ngit worktree list\n```\n\nThree lines, one per path and branch:\n\n```\n/home/dev/acme-api           4f2c1ab [main]\n/home/dev/acme-api-auth      4f2c1ab [agent/auth]\n/home/dev/acme-api-billing   4f2c1ab [agent/billing]\n```\n\nIf 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.\n\nDo it by hand once, in the same order the script does, so you know what's happening:\n\n```\ncd /home/dev/acme-api-auth\ncp ../acme-api/.env .env            # ignored file: git did not copy it\nprintf 'PORT=3001\\n' >> .env        # unique port for this copy\nnpm ci                              # own node_modules\nnpm run dev                         # terminal 1, stays running\n```\n\nIn a second terminal, from the same directory, start the agent:\n\n```\ncd /home/dev/acme-api-auth\nclaude\n```\n\nRepeat in `/home/dev/acme-api-billing` with `PORT=3002`.\n\nChecklist I run through before letting an agent write anything:\n\n```\n- [ ] branch name is unique (agent/auth, agent/billing)\n- [ ] .env copied, and its DATABASE_URL/state path points at this copy\n- [ ] dependencies installed inside this worktree\n- [ ] dev server port differs from every other running copy\n- [ ] `pwd` in the terminal prints this worktree, not the main repo\n- [ ] `git status` in the main repo is still clean\n```\n\nFrom a third terminal, confirm both servers answer on distinct ports:\n\n```\ncurl -s -o /dev/null -w \"%{http_code}\\n\" http://localhost:3001/health\ncurl -s -o /dev/null -w \"%{http_code}\\n\" http://localhost:3002/health\n```\n\nTwo `200` s means both copies are alive and isolated. Then make sure the agent is anchored where you think it is:\n\n```\ncd /home/dev/acme-api-auth && git rev-parse --show-toplevel\n```\n\nExpected: `/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.\n\nDon't take the isolation on faith — test it. Ask the `auth` agent to modify `src/config.ts`, then look at all three trees:\n\n```\ngit -C /home/dev/acme-api-auth    status --short\ngit -C /home/dev/acme-api-billing status --short\ngit -C /home/dev/acme-api         status --short\n```\n\nThe 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.\n\nFinish the task through the normal flow:\n\n```\ncd /home/dev/acme-api-auth\ngit add -A && git commit -m \"feat(auth): rotate session tokens\"\ngit push -u origin agent/auth\n```\n\nOpen the pull request, and once it's merged, remove the copy:\n\n```\ncd /home/dev/acme-api\ngit worktree remove ../acme-api-auth\ngit worktree prune\n```\n\nRunning 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.\n\nThis 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).", "url": "https://wpnews.pro/news/running-multiple-claude-code-agents-in-one-repo-with-git-worktree", "canonical_source": "https://dev.to/qunirice_verase_1ab0caeba/running-multiple-claude-code-agents-in-one-repo-with-git-worktree-2p4g", "published_at": "2026-11-23 10:10:00+00:00", "updated_at": "2026-11-23 10:12:13.660393+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "ai-tools"], "entities": ["Claude Code", "Git", "Anthropic"], "also_reported_by": [], "takeaway": "Give each Claude Code agent its own git worktree and branch, then manually copy ignored files like .env and node_modules and assign a unique port per worktree, since git worktree copies only tracked content and Git blocks the same branch in two worktrees.", "recommendation": {"advice": "Run each coding agent in a separate git worktree on its own branch, and script the copy of .env and node_modules plus a unique PORT per worktree before launching.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/running-multiple-claude-code-agents-in-one-repo-with-git-worktree", "markdown": "https://wpnews.pro/news/running-multiple-claude-code-agents-in-one-repo-with-git-worktree.md", "text": "https://wpnews.pro/news/running-multiple-claude-code-agents-in-one-repo-with-git-worktree.txt", "jsonld": "https://wpnews.pro/news/running-multiple-claude-code-agents-in-one-repo-with-git-worktree.jsonld"}}