Overview
Instead of:- Stashing changes
- Switching branches
- Switching back
- Restoring stash
- Create a worktree on another branch
- Work on both simultaneously
- No context switching overhead
Commands
Add Worktree
List Worktrees
Remove Worktree
Prune Stale Entries
Workflow Example
Scenario: Working on Feature, Need to Hotfix
You’re deep in feature development when a critical bug is reported. Without worktrees:Directory Structure
Use Cases
Parallel Development
Testing Branches
Long-running Operations
Code Review
Best Practices
Limitations
- Each branch can only be checked out in one worktree
- The main worktree cannot be removed
- All worktrees share the same
.witdirectory
Worktrees vs Other Approaches
| Approach | Pros | Cons |
|---|---|---|
| Worktrees | No context switch, parallel work | Disk space |
| Stashing | No extra directories | Manual, error-prone |
| Clone | Complete isolation | Separate repo, sync issues |
| Auto-stash | Automatic | Still switches context |
Troubleshooting
Branch already checked out
Branch already checked out
A branch can only be in one worktree:Remove the existing worktree or use a different branch.
Worktree directory exists
Worktree directory exists
Remove or choose a different path:
Stale worktree entries
Stale worktree entries
If you deleted a worktree directory manually: