Worktrees & Stashing Context
git worktree
Scenario: You're deep in a feature branch when a critical hotfix comes in. You could stash and switch - or you could check out a second working directory for main right next to your current one, fix the bug there, and never disturb your feature work.
git worktree gives you multiple working directories from one repository, each on a different branch:
$ git worktree add ../hotfix main # new dir ../hotfix checked out on main
$ cd ../hotfix # fix, commit, done
$ git worktree remove ../hotfix # clean up
$ git worktree list # see all worktrees
| Approach | When |
|---|---|
git stash | Quick, same directory, brief context switch |
git worktree | Work on two branches simultaneously without stashing |
Tip: Worktrees share the same .git object store, so they're cheap - no full re-clone. Great for building/testing one branch while coding on another.