In this categoryAutomation · 38
AutomationAdvanced

How to Run Two Agents in Parallel with Git Worktrees

Use git worktrees to give each agent its own checked-out folder so two tasks run at once without stepping on each other.

10 minAdvanced

Running two agents in the same folder is a recipe for conflicts: they edit the same files and overwrite each other's work. Git worktrees solve this by giving each branch its own working directory backed by one shared repo. Point an agent at each folder and they run in parallel, cleanly. This guide sets that up.

What you need

  • A git repository
  • Two independent tasks that touch different areas
  • About 12 minutes

Step 1: Create a worktree per task

From the repo, add a worktree for each task. Each one is a real folder on its own branch, sharing history with the main checkout.

zsh - add worktrees
$git worktree add ../app-feature-a -b feature-a
Preparing worktree (new branch 'feature-a')
$git worktree add ../app-feature-b -b feature-b
Preparing worktree (new branch 'feature-b')
$

Step 2: Launch an agent in each folder

Open a terminal in each worktree and start an agent there. Because the directories are separate, their edits never collide.

terminal
# terminal 1
cd ../app-feature-a && claude

# terminal 2
cd ../app-feature-b && claude

Step 3: Keep tasks non-overlapping

Parallelism pays off only when the tasks touch different code. Give each agent a scope that does not overlap the other so the eventual merge is trivial.

Two worktrees - separate scopes
app-feature-a -> src/api/ (agent 1)
app-feature-b -> src/ui/ (agent 2)
shared repo, separate folders, no file overlap
Each agent owns a different part of the tree.

Step 4: Merge and clean up

When both finish, merge each branch as usual, then remove the worktrees so they do not linger.

terminal
git checkout main
git merge feature-a
git merge feature-b
git worktree remove ../app-feature-a
git worktree remove ../app-feature-b
One build per worktree
Each folder has its own node_modules and build output. Run installs and builds inside each worktree, and do not share a single dev server port between them.

Result: two agents make progress at the same time in isolated folders, and the work merges back cleanly because nothing overlapped.

Watch related tutorials

Free weekly email

New guides in your inbox

Fresh step-by-step how-to guides as we publish them. One email a week, no more.

Tags
#agents#git#worktrees#parallel#workflow