Agent Architecture/Worktrees & Isolation
Intermediate8 min

Worktrees for Safe Experimentation

Worktrees make failed experiments zero-cost. Try a major refactor, compare two approaches simultaneously, or review a PR — without ever touching your main working directory.

Quick Reference

  • Failed experiments have zero cleanup cost — exit without committing, worktree disappears
  • A/B approach: spin up two worktrees with different solutions, compare, pick the winner
  • Spike branches: try a refactor in a worktree, validate it, then merge into main
  • PR review: checkout the PR branch in a worktree, test locally without disrupting your work
  • The 'throw it away' mindset: worktrees make the cost of abandonment equal to the cost of success
  • Multiple worktree sessions can run simultaneously on the same codebase
  • Main branch is untouched throughout the entire experiment
  • Committed work is always recoverable — git history preserves it even after worktree cleanup

The Mental Model: Disposable Sandboxes

The key insight about worktrees is not technical — it is psychological. When the cost of failure is zero, you take risks you would otherwise avoid. A worktree is a disposable sandbox. If the experiment succeeds, merge it. If it fails, exit. The main branch never knew it happened.

Zero-Cost Failure Mode

Exit a worktree session without committing, and the worktree is removed automatically. No stash to clean up, no branch to delete, no merge conflict to undo. The main tree is exactly as you left it. The experiment never happened — except you now know that approach does not work.

This changes how you approach risk. Before worktrees, starting a large refactor meant committing to it — abandoning halfway left a messy branch. With worktrees, every experiment is reversible up until you choose to merge. The decision to commit is explicit, not accidental.