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.
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.
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.
Git worktrees have more commands than this, but these four cover 99% of real use.
# 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.
~/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.
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".
# 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.
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.
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.
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.
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.
Worktrees are simple but they have four rough edges. Know them before you spawn ten of them.
git submodule update --init)..git/hooks. Every worktree shares the same hooks. If your pre-commit hook has side effects that depend on the worktree path, test it.rm -rf instead of git worktree remove, git thinks it still exists. Run git worktree prune periodically to clean up phantoms..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.
This is the most fun hands-on of the week. Pick a real project you have on disk.
cd into the project's root (the one with .git).git worktree add ../$(basename $PWD)-exp-1 exp/1, same for 2 and 3.cd into one of the new worktree directories.echo "// exp 1" >> README.md && git commit -am "exp 1".echo "// exp 2" >> README.md && git commit -am "exp 2".git worktree list from any terminal. You should see all four worktrees (main + three exps) with their commit hashes and branches.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.
Before Day 4, confirm:
git worktree add do, exactly? Describe it in one sentence.git worktree remove and deleting the directory with rm -rf?git worktree add on one of your real repos yet? If not, go do it right now.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.