Work on Multiple Branches with git worktree
Use git worktree to check out multiple branches simultaneously in separate directories. No more stash-and-switch dance.
git worktree lets you check out multiple branches at the same time, each in its own directory. No more git stash, git checkout, npm install dance every time you need to switch branches. Perfect for reviewing a PR while keeping your own work-in-progress untouched, or running two dev servers side by side.
Tested on git 2.43+.
When to Use This
- Reviewing a PR while your own feature branch stays intact
- Running an old version of the app side-by-side with the new one
- Hotfixing main while staying on your feature branch
- Comparing behavior between two branches without rebuilding
Don't use this when disk space is tight (each worktree is a full checkout) or when you need branches that share the same files (use a single checkout).
Code
# Create a new worktree for an existing branch
git worktree add ../myapp-pr-456 origin/feature/login
# Create a new worktree AND a new branch from main
git worktree add -b hotfix/auth ../myapp-hotfix main
# List all worktrees
git worktree list
# Remove a worktree when you're done
git worktree remove ../myapp-pr-456
# Force-remove if the worktree directory was already deleted
git worktree pruneThe new worktree appears as a sibling directory by convention. You can put it anywhere on disk, but keeping siblings makes them easy to find.
Usage
The "review a PR while staying on your branch" workflow:
# 1) You're on feature/dashboard with uncommitted changes
$ git status
On branch feature/dashboard
Changes not staged for commit:
modified: src/Dashboard.tsx
# 2) A teammate asks you to review their PR (#456) urgently.
# Create a worktree for it without touching your branch.
$ git fetch origin pull/456/head:pr-456
$ git worktree add ../myapp-pr-456 pr-456
# 3) Switch to the new directory, install deps, and review
$ cd ../myapp-pr-456
$ npm install
$ npm run dev
# Review at http://localhost:3000
# 4) When done, remove the worktree
$ cd ../myapp
$ git worktree remove ../myapp-pr-456
$ git branch -D pr-456Your original feature/dashboard directory is untouched the entire time. No stash, no rebuild.
A second pattern: side-by-side comparison of two branches.
# Main branch in original directory, dev branch in a sibling
git worktree add ../myapp-dev dev
# Run two dev servers on different ports
cd ../myapp && PORT=3000 npm run dev
cd ../myapp-dev && PORT=3001 npm run devPitfalls
- Each worktree is a full checkout. Disk space adds up fast on big repos. Don't create 10 worktrees and forget about them.
- You can't check out the same branch in two worktrees. Git refuses, because it would create an ambiguous HEAD. Use
git worktree add --forceto override (rarely a good idea). node_modulesis per-worktree. Each worktree needs its ownnpm install. That's the trade-off — fast switching, but slower first-time setup.- Removing a worktree directory manually leaves a stale entry. Run
git worktree pruneto clean up.git worktree removeis the safe way. - Submodules don't auto-init in new worktrees. Run
git submodule update --initafter creating each worktree if you have submodules. - The main checkout (
.gitlives in this one) cannot be removed as a worktree. It's the "primary" checkout and is always present.
Related Snippets & Reading
- Useful git stash Patterns — the workflow worktree replaces
- Interactive Rebase to Squash Commits — works inside any worktree
- git worktree official docs — full reference
Frequently Asked Questions
What is a git worktree?
A worktree is a separate working directory tied to the same git repository. Each worktree can have a different branch checked out, with its own working files. They share the same .git history, so commits in one worktree are immediately visible in the others.
Why use worktrees instead of stashing and switching branches?
Stashing loses the build state — node_modules, .next caches, IDE indexing all reset on every switch. Worktrees keep each branch's working state intact, so switching is instant. Run two dev servers, two test runs, two IDE windows in parallel.