# Git Worktrees: Multiple Branches, Zero Context Switching

> Stop stashing changes and switching branches. Learn how git worktrees let you work on multiple branches simultaneously in separate directories.

Source: https://curiouslychase.com/development/git-worktrees/

## What Are Git Worktrees?

Git worktrees let you have multiple branches checked out simultaneously in different directories. Instead of constantly switching branches and stashing changes, each worktree is an independent working directory sharing the same `.git` repository.

## How Worktrees Work

**Current situation:** Your main working directory is on one branch (e.g., `main`).

**Worktree paradigm:**
1. Keep your primary branch (main) in the current directory
2. Create worktrees in a sibling `.worktrees/` directory for other branches
3. Each worktree is an independent working directory sharing the same `.git` repo

**Recommended structure:**
```
your-project/              # main branch (current dir)
your-project/.worktrees/
  ├── feature-x/           # feature-x branch
  ├── bugfix-y/            # bugfix-y branch
  └── staging/             # staging branch
```

## Common Workflow

**Create new branch:**
```bash
git worktree add .worktrees/feature-name -b feature-name
```

**Use existing branch:**
```bash
git worktree add .worktrees/existing-branch existing-branch
```

**List all worktrees:**
```bash
git worktree list
```

**Clean up when done:**
```bash
git worktree remove .worktrees/feature-name
```

## Why Use Worktrees?

**No stashing/context switching** — Stop interrupting your flow to switch branches and stash changes.

**Run multiple dev servers** — Test one branch while developing on another.

**Easy branch comparisons** — Open two directories side-by-side to compare implementations.

**Clean mental model** — Each branch has its own physical space on disk.

## When to Use Worktrees

- Working on a feature while fixing urgent bugs on main
- Running regression tests on one branch while developing on another
- Comparing implementations across branches
- Code review while maintaining local work-in-progress
- **Running parallel AI agents** — Point multiple Claude Code instances at different worktrees to work on separate features simultaneously ([see guide](claude-agents-parallel-worktrees))
