Git Worktrees Are the Isolation Layer AI Coding Agents Need
AI coding agents that share a single working directory pollute each other's context and burn tokens on unrelated diffs. Worktrees give each agent a clean, isolated checkout, making parallel AI-driven tasks safe and reviewable without the overhead of full clones.
A single Git repository can spawn multiple linked working directories — each with its own HEAD, staging area, and working tree — that share the same object database. This eliminates the stash-and-switch dance when juggling a hotfix, a feature branch, and an AI agent's experimental refactor simultaneously.
Claude Code and similar tools now create a worktree per session by default, often tucked inside a hidden `.claude/worktrees/` folder. That default explains the common head-scratcher where edits seem to vanish: the dev server runs against the main tree while the AI silently modifies a detached worktree copy.
Worktrees also provide a cheap sandbox. An agent can trash its own directory without touching the main codebase; a bad experiment is a single `git worktree remove` away. Configuration files like `.env` don't carry over automatically, but a `.worktreeinclude` file can tell the tool which ignored files to replicate.
Worktrees turn branch isolation into a filesystem-level concern, which maps cleanly onto how AI agents already operate — one session, one directory.
The confusion over "missing edits" is a UX failure, not a Git failure: tools that silently redirect file writes to a hidden worktree break the user's mental model of where code lives.
Worktrees are lighter than clones but heavier than `git stash`; the trade-off pays off only when you genuinely need concurrent, long-running work on different branches.
AI tooling is pushing a 2015-era Git feature into the spotlight because the old assumption — one developer, one working tree at a time — no longer holds when multiple agents act on the same repo.
The `.worktreeinclude` pattern is a pragmatic stopgap, but it highlights a missing primitive: worktrees don't have a standard way to declare which untracked files should follow a branch.