Day 03 Parallelism

Parallel Agents with Git Worktrees

The 15-year-old git feature that makes N parallel coding agents possible without merge conflicts, lock files, or any coordination protocol. You can learn this in 15 minutes and it will change how you use agentic IDEs forever.

~1 hour Intermediate Hands-on Bo Peng

Today's Objective

By the end of this lesson you will have spawned multiple parallel agents working on the same repository without collisions, understood the mechanics of git worktrees at a level that lets you debug them when they misbehave, and picked four real workflows where this pattern saves you hours.

Most developers have never used git worktrees. The feature has been in git for years but sits in a corner of the docs nobody visits. Then agentic coding IDEs showed up and worktrees turned out to be the single most important primitive in the whole toolchain for making parallel agents actually useful. Once the pattern is visible it is obvious — but almost nobody teaches it, and almost nobody uses it.

This lesson fixes that. By the end, you will be spawning three or four agents in parallel on the same repo, each working on a different feature branch, each in its own isolated working directory, with zero conflicts and zero ceremony.

01

The Problem Worktrees Solve

You have a repo. You want to try three different approaches to a refactor in parallel, or fix three different bugs at the same time, or let one agent run a long migration while you keep working on features. The naive approach is to clone the repo three times, but that wastes disk space, loses git history sharing, and means each clone has its own stale copy of the same branches.

The second naive approach is to git checkout between branches as you work. This serializes everything — you cannot have branch A and branch B both in a usable state simultaneously because your working directory can only be on one branch at a time.

Git worktrees fix this. A worktree is a second working directory pointing at the same repository. Same .git metadata, same commit history, different physical directory on disk, different HEAD, different staging area. You can have as many as you want.

02

The Four Commands You Actually Need

Git worktrees have more commands than this, but these four cover 99% of real use.

git_worktree_cheatsheet.sh
Bash
# 1. Create a new worktree at a new path on a new branch
git worktree add ../myproject-feat-auth feat/auth

# 2. List all active worktrees
git worktree list

# 3. Remove a worktree when done (does NOT delete the branch)
git worktree remove ../myproject-feat-auth

# 4. Prune stale worktrees that got deleted manually
git worktree prune

That is the entire API surface you need to remember. Run git worktree add ../myproject-feat-auth feat/auth from your main repo and git creates a complete working directory at that path, checks out the feat/auth branch into it, and you can cd there and work normally. The main working directory is untouched.

Naming convention. Put worktrees in a sibling directory next to your main repo, not inside it. Pattern: ~/code/myproject is the main, worktrees live at ~/code/myproject-feat-auth, ~/code/myproject-bugfix-x, and so on. Keeps them out of the main project's own file tree and easy to find with shell completion.
03

Running Agents in Worktrees

The interesting part — actually spawning agents in parallel worktrees. Here is the pattern that Claude Code's Agent tool uses under the hood when you pass it isolation: "worktree".

parallel_agents.sh
Bash
# Spawn three agents on three different features, in parallel
cd ~/code/myproject

git worktree add ../myproject-feat-auth    feat/auth
git worktree add ../myproject-feat-search  feat/search
git worktree add ../myproject-bugfix-429   bugfix/rate-limit

# Launch three Claude Code sessions in three directories
(cd ../myproject-feat-auth    && claude "implement OAuth login flow") &
(cd ../myproject-feat-search  && claude "add fuzzy search to the product page") &
(cd ../myproject-bugfix-429   && claude "fix the 429 retry loop in the webhook handler") &

wait   # Wait for all three to finish

# Now review each worktree's changes independently
(cd ../myproject-feat-auth    && git diff main)
(cd ../myproject-feat-search  && git diff main)
(cd ../myproject-bugfix-429   && git diff main)

Three agents. Three branches. Three directories. Zero collisions. When they all finish, you review each branch on its own merits, merge the winners, and delete the losers. The failed worktrees cost nothing — git worktree remove and the disk space is back.

Why this works: Each worktree has its own HEAD and its own working copy of files. The three agents edit completely different physical files on disk even though they share the same underlying git repository. Git sees three independent checkouts. Your shell sees three independent directories. Your OS's file system is doing all the coordination you need.
04

Four Real Workflows Where This Saves You Hours

01

Parallel Feature Branches

You have 5 independent features to ship. Spawn 5 agents in 5 worktrees. Each on its own branch. When they all finish, review in parallel and merge the winners. Saves you 5x serial time.

02

Speculative Refactors

Try three different approaches to the same refactor. Each in a separate worktree. Run the test suite in each. Whichever passes cleanest is the one you keep. Throw the others away with one command.

03

Long-Running Migrations

Kick off an agent to do a dependency upgrade or a schema migration in a dedicated worktree. Keep working on your main worktree while it runs. Check in on the migration later — it didn't block your flow.

04

Safe Destructive Experiments

Dangerous refactor that might break everything? Run it in a worktree, not your main directory. If it goes sideways, git worktree remove deletes the entire branch and working copy in one command. Blast radius = zero.

05

Gotchas You Will Hit

Worktrees are simple but they have four rough edges. Know them before you spawn ten of them.

Don't work inside .git/. It is tempting to look at git's internal worktree metadata to understand what is happening. Do not edit any files inside .git/worktrees/ by hand. If a worktree gets corrupted, git worktree remove and recreate it — don't try to fix it manually.
06

Hands-On: Run Three Agents in Parallel

This is the most fun hands-on of the week. Pick a real project you have on disk.

  1. cd into the project's root (the one with .git).
  2. Create three worktrees for three feature branches you don't actually care about: git worktree add ../$(basename $PWD)-exp-1 exp/1, same for 2 and 3.
  3. Open three terminal windows. In each, cd into one of the new worktree directories.
  4. In terminal 1, touch a file: echo "// exp 1" >> README.md && git commit -am "exp 1".
  5. In terminal 2, touch a different file: echo "// exp 2" >> README.md && git commit -am "exp 2".
  6. In terminal 3, do the same with a third message.
  7. Run git worktree list from any terminal. You should see all four worktrees (main + three exps) with their commit hashes and branches.
  8. Clean up: git worktree remove ../$(basename $PWD)-exp-1 and same for 2 and 3. Then git branch -D exp/1 exp/2 exp/3 to delete the branches.

You just ran three parallel branches through three independent working directories on one repo. That is the whole pattern. Now replace those touching README with three agents working on three real features and you are running parallel agentic development. It's identical.

Supporting Videos & Reading

Dive deeper into git worktrees and parallel agents.

Day 3 Checkpoint

Before Day 4, confirm:

What's Next

You can now spawn agents in parallel. Day 4 handles the harder half of the problem: making sure they stop. Runaway agents that spin forever in an infinite loop are one of the most common failure modes in production — tomorrow we learn the four stopping conditions every serious agentic IDE uses.

Continue To Day 4
Iteration Loops and Stopping Conditions