Why Are You Still Using Git Stash When Worktrees Could Manage Your Context Better?
I was halfway through a massive architectural refactor last Tuesday when the "everything is on fire" Slack notification arrived. I hit git stash push -m "WIP refactor" and, as usual, spent ten minutes trying to remember exactly which state I’d left my environment in when I finally tried to pop that stash three hours later.
We’ve all been conditioned to treat git stash as the universal "pause" button. It’s fine for a quick one-liner, but for anything substantial, stashing is actually a massive tax on your mental context. You’re essentially throwing your half-finished thoughts into a junk drawer and hoping you can find the matching socks later.
There is a better way that most people ignore: Git Worktrees.
The problem with the stash-and-switch
When you stash your work to jump onto a bug fix, you aren't just moving code; you’re resetting your entire working directory. If you’re a web developer, that might mean your node_modules need a re-install if the bug fix branch is significantly older. If you're using a compiled language, you're likely nuking your build cache.
Then there’s the "ghost of changes past." If you have untracked files that weren't included in the stash, they’re still sitting there, potentially breaking your build on the *new* branch. It's messy.
Enter the Worktree
A worktree allows you to have multiple branches checked out at the same time in different directories, all linked to the same .git history. Instead of switching branches in your current folder, you just walk into a different room.
Here is the magic command:
git worktree add ../hotfix-branch-name mainThis does three things instantly:
1. Creates a new directory called hotfix-branch-name one level up from your current project.
2. Checks out a new branch based on main in that directory.
3. Leaves your current, messy, half-finished refactor exactly where it is—untouched, un-stashed, and still open in your IDE.
A real-world workflow
Let's say you're working in a folder called super-app. You’re deep in the weeds.
1. The emergency happens.
Instead of stashing, you run:
git worktree add ../emergency-fix origin/main2. Pivot.cd ../emergency-fix. Run your tests. Fix the bug. Commit and push.
git add .
git commit -m "fix: resolve critical null pointer"
git push origin head3. Return to sanity.cd ../super-app. Everything is exactly as you left it. No git stash pop, no merge conflicts with your WIP, no "wait, did I save that one file?" anxiety.
4. Cleanup.
Once the fix is merged, you can delete that temporary directory:
git worktree remove ../emergency-fixWhy this is superior
The biggest win isn't the command line; it's the IDE. I frequently keep two instances of VS Code open. One window has my long-term feature branch where I'm breaking everything. The other window is focused on the hotfix. I can literally copy-paste a snippet of logic from my "broken" branch into the "fix" branch without toggling back and forth between git states.
You also avoid the "Stash Roulette" where you accidentally pop the wrong stash or, worse, git stash drop something you actually needed.
The "Gotchas"
It’s not all sunshine. There are two main things to keep in mind:
1. The "Same Branch" Rule: Git won't let you check out the same branch in two different worktrees. If you need to work on the *same* branch, you’re stuck with the stash. But usually, an emergency fix requires a new branch anyway, so this is rarely a dealbreaker.
2. Heavy Dependencies: If your node_modules or vendor folders are 2GB, creating a new worktree means a new copy of those dependencies. I usually just cp -r node_modules ../new-worktree to save time, but it’s worth noting if you’re tight on disk space.
Stop hoarding stashes
If you run git stash list and see more than three items, you’re doing it wrong. You're building up technical debt in your own workflow.
Next time a context switch threatens to derail your afternoon, don't reach for the stash. Create a parallel universe with a worktree, do the work, and then collapse that universe when you're done. Your brain—and your build cache—will thank you.