Every developer knows the pain: you’re deep in a feature branch, someone asks you to review a PR, and now you’re stashing changes, switching branches, rebuilding dependencies, and losing your mental context. Git worktrees solve this elegantly.
The Problem with Branch Switching
Traditional git workflow assumes one working directory:
| |
Every switch costs time and mental energy. Your editor loses state. Your dev server restarts. Your flow is gone.
Worktrees: Multiple Checkouts, One Repo
Git worktrees let you check out multiple branches simultaneously in separate directories, all sharing the same .git database:
| |
Your original working directory stays exactly as you left it. No stashing, no rebuilding, no lost context.
Practical Patterns
Pattern 1: Permanent Main Branch Worktree
Keep main always ready for quick checks:
| |
Now cd ../project-main && git pull gives you instant access to production code without touching your work.
Pattern 2: Ephemeral Review Trees
Create worktrees on-demand for PR reviews:
| |
Pattern 3: Hotfix Isolation
Production is on fire. You need to patch without contaminating your feature work:
| |
Managing Worktrees
| |
IDE Integration
Most modern IDEs handle worktrees gracefully:
VS Code: Open the worktree directory as a separate window. Each window maintains its own state, extensions work normally, and you can have both open side-by-side.
| |
JetBrains IDEs: File → Open and select the worktree directory. The IDE recognizes it as part of the same git repository.
The Dependency Problem
Worktrees share git data but not node_modules, venv, or build artifacts. This is actually a feature — different branches might need different dependencies.
For Node.js projects:
| |
For Python:
| |
Caveats
Don’t checkout the same branch twice. Git prevents this because two worktrees modifying the same branch would create conflicts:
| |
Worktrees aren’t clones. They share refs, so a git fetch in one worktree updates all of them. This is usually what you want.
Clean up after yourself. Orphaned worktrees don’t hurt anything but clutter your disk:
| |
When Worktrees Shine
- Code review without losing your place
- Comparing implementations across branches side-by-side
- Running tests on main while developing on feature
- Hotfixes with zero risk to in-progress work
- Long-running tasks (builds, test suites) that shouldn’t block you
When to Just Switch Branches
Worktrees have overhead. If you’re making a quick, clean switch and coming right back, traditional checkout is fine:
| |
Use worktrees when the context switch is expensive or when you need both states accessible simultaneously.
Git worktrees have been around since Git 2.5 (2015) but remain underused. Once you internalize the pattern, the “stash-switch-rebuild-switch-pop” dance feels prehistoric. Your branches can coexist, and so can your mental contexts.