Git Worktrees Clearly Explained — And How to Use Them With AI Agents
Git worktrees let you check out multiple branches of the same repository simultaneously — each in its own directory, sharing a single .git store. This old Git feature has suddenly become essential in the AI agent era: instead of one agent blocking another by monopolizing the working directory, you give each agent its own worktree and let them code in parallel on separate branches. This is the missing piece for multi-agent development workflows.
Problem Without Worktrees
0:00In a standard Git workflow, you have one working directory. To switch branches, you run git checkout or git switch, which changes the files on disk. This creates several problems:
- Context switching cost — switching branches means saving state, stashing changes, rebuilding dependencies
- Single occupancy — only one branch can be checked out at a time in a single directory
- Agent collision — if two AI agents work on the same repo, they fight over the working directory
- Build cache invalidation — switching branches often invalidates build caches, costing minutes per switch
"With one working directory, switching branches is like moving to a different apartment every time you want to work on a different project. Worktrees let you have multiple apartments."
Why Worktrees Are Exploding Now
2:00Git worktrees have existed since Git 2.5 (2015), but they're having a moment right now. The reason: AI coding agents. When you want multiple agents working on the same repository simultaneously, each needs its own isolated working directory — and worktrees provide exactly that.
- Agent parallelism — give Agent A a worktree on
feature/authand Agent B a worktree onfeature/dashboard - No conflicts — each agent has its own file system space; no race conditions on file writes
- Shared history — all worktrees share the same Git object store, so commits are visible across all of them
- Tools are adopting it — Claude Code, Codex, and other AI agents are adding native worktree support
The pattern is clear: worktrees are becoming the standard infrastructure for multi-agent development, just as branches became standard for multi-developer workflows.
How Worktrees Work
4:00A worktree is a linked working directory that shares the .git object store with the main repository. Instead of cloning the repo multiple times (which duplicates all objects), worktrees create lightweight checkouts that reference the same underlying data.
Key constraint: each branch can only be checked out in one worktree at a time. You can't have two worktrees both on main. This prevents ambiguity about which working directory reflects a branch's state.
Practical Setup
7:00Creating and managing worktrees is simple — it's built into Git:
Each worktree gets its own node_modules, build cache, and running processes. This isolation is what makes parallel agent work possible — no file system conflicts.
Multiple Agents in Parallel
10:00Here's the workflow that brings it all together — running multiple AI agents on different features simultaneously:
This pattern combines worktrees for filesystem isolation with tmux for session persistence. Each agent gets its own branch, its own directory, and its own tmux pane. They share the Git history but never step on each other's files.
"Worktrees + tmux is the multi-agent development stack. Each agent gets a branch, a directory, and a pane. You orchestrate from the outside."
Best Practices
14:00After working with worktrees extensively, here are the patterns that work best:
- Naming convention — use
../project-feature-nameas the worktree path to keep things organized alongside the main repo - Keep main clean — use the main worktree only for reviewing PRs and merging. Do all active work in linked worktrees.
- Clean up after merge — always
git worktree removeafter merging a feature branch. Don't let stale worktrees accumulate. - Separate node_modules — each worktree needs its own
npm install. Consider usingpnpmwith shared content-addressable storage to save disk space. - One branch per worktree — Git enforces this, but it's also a good mental model. Think of worktrees as "active workspaces."
- Bare repo pattern — for maximum flexibility, clone with
--bareand create all working directories as worktrees.
✦ Key Takeaways
.git object store.