Dev.to AI ๐Ÿค– Ai ๐Ÿ‘ 0 ๐Ÿ“– 2 min read

Run Parallel AI Agents with Git Worktrees

Two agents in one working tree step on each other's edits. The fix is to give each Claude Code session its own directory and branch with git worktree. Here's the condensed version (tested on git 2.54.0 and Claude Code 2.

Two agents in one working tree step on each other's edits. The fix is to give each Claude Code session its own directory and branch with git worktree. Here's the condensed version (tested on git 2.54.0 and Claude Code 2.1.309, 2026-10-23).

Git worktree basics

cd todo-app
git worktree add ../todo-app-login -b feature/login
git worktree list

Things that bite:

  • A branch can be checked out in only one worktree at a time. A second one fails with fatal: 'feature/login' is already used by worktree at ...
  • git worktree remove refuses while modified or untracked files remain
  • If you delete a worktree with rm, clean up with git worktree prune -v

Claude Code's --worktree

claude --worktree feature-auth

This creates .claude/worktrees/feature-auth/ on a new branch worktree-feature-auth. Run it in another terminal with a different name for a second session. Add .claude/worktrees/ to .gitignore. New worktrees branch from the default branch; set worktree.baseRef to "head" to branch from current work.

What each worktree needs

A worktree is a fresh checkout: no node_modules, no gitignored .env. Install dependencies in each one. A .worktreeinclude file (gitignore syntax) copies matching gitignored files into new worktrees:

.env
.env.local

Dev-server ports and local databases are shared, so give each worktree its own port.

How many to run

On a subscription, usage is shared across sessions (five-hour and weekly limits), so more sessions hit limits sooner. The costs docs say agent teams use roughly 7x more tokens than standard sessions when teammates run in plan mode. We found no official maximum.

Cleaning up

  • Interactive session with no changes: an unnamed worktree and its branch are removed automatically
  • With changes: you're asked to keep or remove; resume with claude --worktree <name> --resume
  • -p runs leave worktrees behind; remove them with git worktree remove (git worktree unlock first if locked)

Sources: https://git-scm.com/docs/git-worktree , https://code.claude.com/docs/en/worktrees (checked 2026-10-23)

๐Ÿ‘‰ Full article: https://agentkoji.com/en/articles/agent-parallel-worktree/

๐Ÿ“ฐ Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.