🔀 The Context Switch Nightmare
You’re deep in feature work. Urgent bug reported. You stash, switch branches, fix bug, switch back, pop stash… 20 minutes lost. Your flow? Destroyed.
Git Worktrees: Multiple Working Directories, One Repo
❌ The Old Way (Painful)
# Working on feature branch $ git status On branch feature/new-dashboard Changes not staged... # Urgent bug! $ git stash $ git checkout hotfix/critical-bug $ vim fix.js $ git add . && git commit -m "Fix" $ git checkout feature/new-dashboard $ git stash pop # Problems: # - Lost 5-10 minutes switching # - Forgot what you were doing # - Stash conflicts # - Can't run both branches simultaneously
✅ Git Worktrees (Seamless)
# Setup once $ cd ~/projects/myapp $ git worktree add ../myapp-hotfix hotfix/critical-bug $ git worktree add ../myapp-feature2 feature/other-feature # Your folder structure: ~/projects/ myapp/ (main branch) myapp-hotfix/ (hotfix branch) myapp-feature2/ (feature branch) # Now just switch folders! $ cd ../myapp-hotfix $ vim fix.js $ git commit -m "Fix" $ cd ../myapp # Back to feature work instantly! # Benefits: # - Zero context switching time # - No stashing # - Run multiple branches at once (different ports) # - Each worktree has its own node_modules!
🛠️ Essential Worktree Commands
# Create worktree for existing branch git worktree add ../myapp-hotfix hotfix/critical-bug # Create worktree AND new branch git worktree add -b feature/new-thing ../myapp-newthing # List all worktrees git worktree list # /Users/dev/projects/myapp abc1234 [main] # /Users/dev/projects/myapp-hotfix def5678 [hotfix/critical-bug] # Remove worktree (after you're done) git worktree remove ../myapp-hotfix # Or just delete the folder rm -rf ../myapp-hotfix git worktree prune # Clean up references # Move worktree git worktree move ../myapp-hotfix ../archive/myapp-hotfix
| Scenario | Without Worktrees | With Worktrees |
|---|---|---|
| Switch to fix urgent bug | 5-10 min (stash, switch, pop) | 10 sec (cd ../hotfix) |
| Test 2 branches together | Impossible | Easy (run both) |
| Compare branches | Switch back & forth | Open both in editor |
| node_modules conflicts | Frequent (rebuild) | Never (separate) |
✅ Perfect Use Cases
- Hotfix while working on feature
- Code review (checkout PR in worktree)
- Long-running branches (release vs main)
- Testing multiple branches together
- Different dependencies per branch
⚠️ Gotchas
- Can’t checkout same branch in 2 worktrees
- Disk space: Each worktree = full working copy
- Must remember to prune deleted worktrees
- VS Code: Open worktrees as separate windows
🎯 My Personal Workflow
~/projects/ myapp/ # main - stable code myapp-dev/ # develop - current sprint myapp-hotfix/ # For urgent fixes myapp-review/ # For reviewing PRs # Terminal tabs: Tab 1: myapp-dev (my active work) Tab 2: myapp-hotfix (ready for emergencies) Tab 3: myapp-review (reviewing teammates code) # Can run all 3 on different ports simultaneously! # myapp-dev: localhost:3000 # myapp-hotfix: localhost:3001 # myapp-review: localhost:3002
“I haven’t used git stash in 2 years. Worktrees changed my life. When a hotfix comes in, I just cd to the hotfix folder. Feature work stays untouched.”
