I was constantly stashing changes to review PRs or fix urgent bugs on other branches. Stash, switch, fix, commit, switch back, pop stash, hope I didn’t mess something up. Git worktrees solved this entirely.
What’s a worktree? Link to heading
A worktree is a separate working directory with a different branch checked out, all linked to the same repository. Create one like this:
git worktree add ../fix-urgent-bug fix-urgent-bug
Now you have two directories - your main one stays on whatever branch you were on, and the new one has fix-urgent-bug checked out. No more stashing, no context switching, just cd between them.
My naming convention Link to heading
I keep all worktrees in a sibling directory with the ticket ID as the suffix:
# Main repo is at ~/Code/myproject
# Worktrees go in ~/Code/myproject-worktrees/
git worktree add ../myproject-worktrees/INF-123 feature/INF-123-new-feature
This keeps them organised and makes it obvious which ticket each worktree relates to. I typically have 2-4 worktrees active at any time - one for the main feature I’m working on, one or two for PR reviews, and occasionally one for an urgent fix.
Managing worktrees Link to heading
List your worktrees:
git worktree list
When you’re done, clean up:
git worktree remove ../myproject-worktrees/INF-123
If you deleted the directory manually, run this to tidy up the references:
git worktree prune
Why not just clone multiple times? Link to heading
You could achieve something similar by cloning the repo multiple times, but worktrees are better:
- Shared git objects - Worktrees share the
.gitdirectory, so you’re not duplicating the entire history. This matters for large repos. - Shared remotes and config - Fetch once, available everywhere. No need to configure each clone.
- Branch protection - Git prevents you from checking out the same branch in two worktrees, avoiding accidental conflicts.
- Cleaner workflow -
git worktree listshows all your active work. Multiple clones are just… directories.
The downside? Each worktree needs its own node_modules or virtual environment, which can eat disk space. For most projects, it’s worth it.
I also wrote a helper function with fzf to make worktree navigation easier.
Further reading Link to heading
- git-worktree documentation - official reference