Work on Two Branches at Once with git worktree
18 Mar 2026You’re mid-feature, uncommitted changes everywhere, and a production bug needs a hotfix right now. The old move is git stash, checkout main, fix, checkout back, git stash pop, and hope nothing conflicts. git worktree skips all of it: check out a second branch into a second folder and work in both at once.
Set it up
git worktree add ../myapp-hotfix main
This creates a new directory, ../myapp-hotfix, checked out to main, sharing the same .git history as your main checkout. No second clone, no re-downloading the repo, no duplicated .git objects — worktrees share the object database.
cd ../myapp-hotfix
git checkout -b hotfix/checkout-crash
# fix the bug, commit, push, open the PR
cd ../myapp
# your original feature branch is exactly as you left it
Your feature branch’s uncommitted changes are untouched, because they’re in a different directory entirely. No stash, no pop, no risk of a stash conflict eating your work.
Where this actually earns its keep
- Hotfix during feature work. The scenario above — no more stash juggling.
- Long-running builds or tests. Kick off a slow test suite or build in one worktree, keep coding in another. They don’t block each other because they’re separate directories with separate working states.
- Reviewing a PR without losing your place.
git worktree add ../review-pr-42 origin/feature-branch, look at the code in an editor, run it locally, thengit worktree removewhen done. Your own branch’s state never moved. - Comparing behavior across branches. Run the old version and the new version side by side, two terminals, two directories, to diff actual runtime behavior instead of just the diff.
The commands you need
git worktree list # see all active worktrees
git worktree remove ../myapp-hotfix # done with it, clean up
git worktree prune # clean up stale references after manual deletion
Don’t rm -rf a worktree directory directly — use git worktree remove. If you do delete it manually, run git worktree prune afterward or git will keep a dangling reference to a directory that no longer exists.
The gotcha
You can’t check out the same branch in two worktrees at once. Git blocks it:
fatal: 'feature-x' is already checked out at '/path/to/myapp'
That’s by design — two working directories editing the same branch would fight over which changes are “real.” Use a different branch name for the second worktree, even a throwaway one, if you need to poke at the same code from two places.
Also: each worktree needs its own node_modules / build artifacts / venv. They’re separate directories, and dependency installs don’t share across worktrees the way git objects do. For a Node project, that means a second npm install per worktree — budget for that if your node_modules is large.
Worktrees replace stashing for context switches, not branching for actual parallel feature work by different people. If you’re the only one touching the code and you just need “let me quickly do something else without losing my place,” this is the tool.