{"slug": "running-coding-agents-in-parallel-with-git-worktrees", "title": "Running Coding Agents in Parallel with Git Worktrees", "summary": "A developer describes using Git worktrees to run multiple coding agents in parallel without file conflicts. The approach creates separate working directories for each agent, all backed by the same repository, allowing simultaneous work on different branches and direct merges without pushing or pulling. The developer notes that Git itself serves as the communication mechanism between agents, while GitHub remains useful for human review and CI.", "body_md": "I kept hitting the same wall with coding agents. One Claude Code or Codex session in a repo works great. The moment I wanted two tasks moving at once - login in one terminal, payments in another - they started stepping on each other. Same working directory, same checked-out branch, two processes editing the same files. Chaos.\n\nThe fix turned out to be a Git feature that has been sitting there for years: `git worktree`\n\n. It gives you several working directories backed by the **same repository**. Each folder has its own checked-out branch, but all of them share the same objects, commits and branch list.\n\nFrom your main checkout:\n\n```\ngit worktree add ../integration -b integration main\ngit worktree add ../feature-login -b feature/login main\ngit worktree add ../feature-payments -b feature/payments main\n```\n\nWhich leaves you with something like:\n\n```\nproject/\n├── main/               → branch main\n├── integration/        → branch integration\n├── feature-login/      → branch feature/login\n└── feature-payments/   → branch feature/payments\n```\n\nNow every agent gets its own folder. One terminal per worktree, one agent per terminal, and nobody touches anybody else's files:\n\n```\ncd feature-login    # agent 1 works here\ncd feature-payments # agent 2 works here, at the same time\n```\n\nMy first instinct was: agent finishes login, pushes the branch, then I pull it into integration. That's the muscle memory from working in a team.\n\nIt's unnecessary here. All the worktrees belong to the same repository on the same machine, so Git already knows every branch locally. When agent 1 finishes:\n\n```\ncd feature-login\ngit add .\ngit commit -m \"feat: implement login\"\n```\n\n...the integration worktree can merge it directly:\n\n```\ncd ../integration\ngit merge feature/login\ngit merge feature/payments\nnpm test\n```\n\nNo `git push`\n\n, no `git pull`\n\n. The directories are different, but `feature/login`\n\nand `integration`\n\nare branches of the same repo. When integration is green:\n\n```\ncd ../main\ngit merge integration\n```\n\nYou don't even have to wait for a worktree to be deleted before merging its branch. Both folders can exist at the same time. The only thing Git refuses is having the *same* branch checked out in two worktrees at once - which is exactly the protection you want when agents are involved.\n\nWhen a feature is done, resist the `rm -rf`\n\n:\n\n```\ngit worktree remove ../feature-login\ngit branch -d feature/login\n```\n\n`git worktree remove`\n\ndeletes the folder *and* cleans Git's internal registry. If you already nuked the folder by hand, `git worktree prune`\n\nfixes the bookkeeping after the fact.\n\nThe full cycle for one task ends up being:\n\n```\ngit worktree add ../feature-login -b feature/login main   # create\ncd ../feature-login                                       # agent works, commits\ncd ../integration && git merge feature/login && npm test  # integrate\ngit worktree remove ../feature-login                      # discard workspace\ngit branch -d feature/login                               # discard branch\n```\n\nFor the agents to collaborate on one machine: nowhere. Git itself is the communication mechanism.\n\nGitHub earns its place the moment humans enter the loop - PRs, review, CI, an audit trail:\n\n```\ncd feature-login\ngit push -u origin feature/login   # open a PR from this\n```\n\nAnd if you want integration to merge exactly what's on the remote rather than the local branch:\n\n```\ngit fetch origin\ngit merge origin/feature/login\n```\n\nI now think of it as two separate planes. Locally: worktree → commit → merge into integration → tests. Remotely: push → PR → review → CI. The second plane is for people; the first one is where the agents actually work.\n\nThe detail that sold me on the whole thing: the integration agent never copies code from the other agents, never reads their folders, doesn't even know they exist. It only knows their branch names. Git was the message bus all along.\n\nWorktrees isolate files and branches. They don't isolate runtime. The moment two agents each run `npm run dev`\n\nand a Playwright suite, they all reach for :3000, the same test database, and a surprising amount of RAM per headless browser.\n\nThe workaround I use today is boring: each worktree gets its own port through an env file written at creation time, and Playwright reads it for its baseURL:\n\n```\necho \"PORT=3101\" > ../feature-login/.env.local\necho \"PORT=3102\" > ../feature-payments/.env.local\n```\n\nBut ports are only the visible part of the problem. Isolating test databases, keeping three browser suites from eating the machine, and deciding whether integration should be the only worktree allowed to run E2E at all - that deserves a post of its own. It's the next one I'm writing.", "url": "https://wpnews.pro/news/running-coding-agents-in-parallel-with-git-worktrees", "canonical_source": "https://dev.to/servatj/running-coding-agents-in-parallel-with-git-worktrees-507i", "published_at": "2026-08-30 21:29:33+00:00", "updated_at": "2026-08-30 21:52:23.757761+00:00", "lang": "en", "topics": ["developer-tools", "ai-agents"], "entities": ["Git", "Claude Code", "Codex", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/running-coding-agents-in-parallel-with-git-worktrees", "markdown": "https://wpnews.pro/news/running-coding-agents-in-parallel-with-git-worktrees.md", "text": "https://wpnews.pro/news/running-coding-agents-in-parallel-with-git-worktrees.txt", "jsonld": "https://wpnews.pro/news/running-coding-agents-in-parallel-with-git-worktrees.jsonld"}}