Git Stash: Your Secret Weapon for Context Switching
Learn: Git Stash: Your Secret Weapon for Context Switching
Welcome to TopperBlog! 👋
I'm a tech content creator passionate about helping developers level up their careers and master cutting-edge technologies.
🎯 What I Write About:
• AI/ML Engineering & LLMs
• Web3 & Blockchain Development
• System Design & Architecture
• Interview Preparation (FAANG)
• Freelancing & Remote Work
• Modern Tech Stacks (Next.js, React, Rust, TypeScript)
• Performance Optimization & Best Practices
💼 Mission: Sharing practical, actionable insights that accelerate your tech career and maximize your earning potential.
📚 15+ In-Depth Guides covering everything from earning $10k/month as a freelancer to cracking FAANG interviews.
🌐 Let's connect and grow together in this amazing tech journey!
#TechBlogger #SoftwareEngineering #CareerGrowth #WebDevelopment #AIEngineering
Git Stash: Your Secret Weapon for Context Switching
You know that moment when you're deep in the zone—three files open, half-written code scattered across your workspace like a beautiful disaster—and your manager Slacks you: "Production is down. Need you on the hotfix NOW"?
Your heart sinks. Your palms sweat. You stare at your uncommitted changes like a parent looking at their messy toddler before unexpected guests arrive.
Do you commit this half-baked garbage with a message like "WIP don't judge me"? Do you copy everything to a text file like some kind of caveman? Do you just... cry a little?
There's a better way. It's called git stash, and it's about to become your new best friend.
The Context-Switching Nightmare We All Live
Let's be honest: the idea that developers work on one thing at a time is adorable fiction, like "I'll just watch one episode" or "I'll only have one slice of pizza."
The reality? You're:
- Building a new feature on
feature/user-dashboard - When QA finds a bug in staging
- While your coworker needs help reviewing their PR
- And oh god, production is actually on fire
Each context switch is like being yanked out of a swimming pool and thrown into a different one. Except the pools are made of code, and you're not wearing floaties.
The traditional "solution" developers reach for is committing incomplete work. We've all seen these commit messages:
"temp"
"asdf"
"DO NOT MERGE"
"I swear I'll fix this later"
"*screaming internally*"
This pollutes your git history like plastic in the ocean. Future you (or worse, your teammates) will have to wade through this garbage to understand what actually happened. It's the digital equivalent of shoving everything under your bed when mom says to clean your room.
Why This Actually Matters (Beyond Your Sanity)
"But wait," you say, "I can just commit and rebase later, right?"
Sure, Jan. And I'm definitely going to organize my downloads folder this weekend.
Here's why dirty commits are worse than you think:
1. Git history becomes archaeological fiction
When someone runs git blame six months from now, they'll find your "WIP BROKEN DO NOT USE" commit and question every life choice that led them to this codebase.
2. Bisecting becomes impossible
git bisect is an incredible tool for finding bugs—unless half your commits are "this doesn't work yet lol." It's like trying to find a needle in a haystack where half the hay is actually just more needles.
3. Code review hell Nothing says "I respect your time" like a PR with 47 commits, 23 of which are variations of "fix typo" and "actually fix typo."
4. Your professional reputation Yes, we're all friends here, but there's a difference between "developer who occasionally makes mistakes" and "developer whose git log looks like a stream of consciousness from a caffeinated squirrel."
Enter Git Stash: The Hero We Deserve
Think of git stash as a magical pocket dimension where you can temporarily hide your work. It's like having a pause button for your code—everything freezes exactly as it is, your working directory becomes clean, and you can jump to any branch or commit you need.
When you're done with the interruption, you pull your work back out of the pocket dimension, and boom—you're exactly where you left off.
The Basic Stash (Your New Muscle Memory)
Here's the simplest use case:
# You're working on feature/awesome-thing
# *Slack notification* "URGENT: Fix needed on main"
git stash
# Your working directory is now clean!
# Switch to main, fix the thing, commit it
git checkout main
# ... do the hotfix ...
git commit -m "Fix: Production no longer on fire"
# Now go back to your feature
git checkout feature/awesome-thing
git stash pop
# You're back exactly where you were!
That's it. Seriously. git stash to save, git stash pop to restore. You just learned 80% of what you need.
Level Up: Stash Like a Pro
But wait, there's more! (I've always wanted to say that.)
Multiple Stashes: Because Life is Complicated
You can stash multiple times. Git keeps them in a stack:
git stash # Stash #1
# Do some other work
git stash # Stash #2
# Do even more work
git stash # Stash #3
# See all your stashes
git stash list
# stash@{0}: WIP on feature/thing: a1b2c3d Last commit message
# stash@{1}: WIP on feature/other: d4e5f6g Another commit
# stash@{2}: WIP on main: g7h8i9j Yet another commit
The most recent stash is stash@{0}. They're numbered like an array (because of course they are—we're developers, we can't escape zero-indexing even in our tools).
Give Your Stashes Names (Future You Will Thank You)
Here's a pro move: give your stashes descriptive names instead of letting them default to "WIP on whatever."
git stash save "Half-finished user authentication logic"
git stash save "Experimental approach to caching - might be terrible"
git stash save "Code I wrote at 2am - definitely terrible"
git stash list
# stash@{0}: On feature/auth: Code I wrote at 2am - definitely terrible
# stash@{1}: On feature/cache: Experimental approach to caching - might be terrible
# stash@{2}: On feature/auth: Half-finished user authentication logic
Now when you run git stash list, you actually know what's in each stash without playing Russian roulette with git stash pop.
Stash Specific Files (Surgical Precision)
Sometimes you don't want to stash everything. Maybe you've got some debug logging you want to keep, but you need to stash the actual feature work:
# Stash only specific files
git stash push -m "Just the auth changes" src/auth.js src/middleware/auth.js
# Or stash everything EXCEPT certain files
git stash push --keep-index
The --keep-index flag is particularly clever: it stashes your changes but keeps anything you've already git add-ed in your staging area. It's like saying "save this, but I'm still working on it."
Include Untracked Files (The Forgotten Children)
By default, git stash only stashes tracked files—files git already knows about. New files you just created? Git ignores them like a cat ignores your existence.
# Stash everything, including new files
git stash -u
# Or the more explicit version
git stash --include-untracked
# Stash EVERYTHING, even .gitignore'd files (use with caution!)
git stash -a
# or
git stash --all
I rarely use --all because stashing your node_modules or .env files is usually a bad idea, but hey, sometimes you need to stash the world.
Pop vs Apply: Choose Your Own Adventure
There are two ways to restore a stash, and the difference matters:
# Pop: Apply the stash AND remove it from the stash list
git stash pop
# Apply: Apply the stash but KEEP it in the stash list
git stash apply
When to use pop: Most of the time. You're done with the interruption, you want your work back, and you don't need the stash anymore.
When to use apply: When you want to apply the same changes to multiple branches, or when you're not sure if the stash will apply cleanly and you want to keep it as a backup.
Here's a real scenario: You've got a bug fix that needs to go into both main and release-1.0. Stash your fix, apply it to main, commit, switch to release-1.0, apply the same stash again, commit. Boom—same fix in two places without cherry-picking gymnastics.
git stash save "Critical security fix"
git checkout main
git stash apply
git commit -m "Security: Fix XSS vulnerability"
git checkout release-1.0
git stash apply
git commit -m "Security: Fix XSS vulnerability"
git stash drop # Now we're done with it
When Stashes Go Wrong (And How to Fix It)
Let's talk about the elephant in the room: merge conflicts in stashes.
Sometimes you stash your work, do other stuff, and when you try to pop the stash, git says "lol nope, conflicts everywhere."
git stash pop
# Auto-merging src/app.js
# CONFLICT (content): Merge conflict in src/app.js
Don't panic. This is actually fine. Git has applied as much as it can and marked the conflicts, just like a regular merge. You resolve them the same way:
- Open the conflicted files
- Look for the
<<<<<<<,=======,>>>>>>>markers - Fix the conflicts
git addthe resolved files- Continue with your life
The stash is automatically dropped after a successful pop, but if there were conflicts, it stays in the stash list. Once you've resolved everything, manually drop it:
git stash drop
Advanced Stash Wizardry
Create a Branch from a Stash
Sometimes you stash something, then realize "wait, this should actually be its own feature branch." Git's got you:
git stash branch new-feature-name stash@{0}
This creates a new branch, checks it out, applies the stash, and drops the stash. It's like git stash pop but with a new branch created at the commit where you originally made the stash. Incredibly useful when you realize your "quick fix" has turned into a whole thing.
Inspect a Stash Without Applying It
Want to peek inside a stash before applying it?
# See what changed in the most recent stash
git stash show
# See the actual diff
git stash show -p
# Look at a specific stash
git stash show -p stash@{2}
The -p flag shows the patch (the actual code changes), not just the file names.
Partial Stash (Interactive Mode)
This is some next-level stuff:
git stash -p
Git will walk through each change and ask "stash this hunk?" You can pick and choose exactly what to stash, leaving the rest in your working directory. It's like git add -p but for stashing.
I use this when I've been working on two unrelated things in the same file (yes, I know, I should have better discipline) and I need to stash just one of them.
Real-World Stash Workflows
Let me share some battle-tested patterns:
The "Oh Crap" Workflow
# You're on the wrong branch
git stash
git checkout correct-branch
git stash pop
# Crisis averted
We've all started working on main when we meant to create a feature branch. Stash saves you from the awkward git reset dance.
The "Experimental Idea" Workflow
# You want to try something crazy
git stash save "Current stable approach"
# Try the crazy thing
# If it works: great!
# If it doesn't:
git reset --hard
git stash pop
# Back to safety
This is like quicksave in a video game. Try risky things without fear.
The "Clean Slate for Testing" Workflow
# You need to test the current branch state without your changes
git stash
# Run tests, verify behavior
git stash pop
# Continue working
Super useful when you're debugging and need to know if the bug exists in the committed code or just in your changes.
The "Pull with Uncommitted Changes" Workflow
# You need to pull but have uncommitted work
git stash
git pull
git stash pop
Git will often let you pull with uncommitted changes, but sometimes it refuses. Stash is the polite way to handle it.
Common Stash Mistakes (Learn from My Pain)
Mistake #1: Forgetting you have stashes
I once had 15 stashes. I remembered 2 of them. The others were archaeological mysteries.
Solution: Regularly run git stash list and clean up old stashes with git stash drop stash@{n} or git stash clear (which nukes all stashes—use carefully).
Mistake #2: Stashing and switching branches without thinking
Stashes are global to your repository, not tied to a specific branch. If you stash on feature-a, switch to feature-b, and pop, you're applying feature-a's changes to feature-b. This can get weird fast.
Solution: Always check git status and git branch before popping. Know where you are.
Mistake #3: Using stash as long-term storage
Stash is for temporary storage. It's a pocket, not a warehouse. If you need to save work for more than a day or two, commit it to a WIP branch instead.
Solution: If a stash is older than a week, either commit it properly or delete it. It's probably not relevant anymore anyway.
Stash vs. Other Approaches
Why not just commit to a WIP branch?
You can! And sometimes you should. But stash is faster for quick context switches. No need to think of a branch name, no pollution of your branch list, no cleanup later.
Why not use git worktree?
Worktrees are amazing for working on multiple branches simultaneously, but they require more setup and disk space. Stash is lighter weight for quick switches.
Why not just use your IDE's shelf feature?
Some IDEs (like IntelliJ) have their own "shelf" feature that's similar to stash. Use it if you want! But stash is universal, works in any environment, and doesn't depend on your IDE.
The Stash Cheat Sheet
Here's your reference card:
# Basic stashing
git stash # Stash tracked files
git stash -u # Stash tracked + untracked files
git stash -a # Stash everything (including ignored)
git stash save "description" # Stash with a message
# Viewing stashes
git stash list # See all stashes
git stash show # Show files in latest stash
git stash show -p # Show diff of latest stash
git stash show stash@{2} # Show specific stash
# Applying stashes
git stash pop # Apply latest stash and remove it
git stash apply # Apply latest stash and keep it
git stash apply stash@{2} # Apply specific stash
# Managing stashes
git stash drop # Delete latest stash
git stash drop stash@{2} # Delete specific stash
git stash clear # Delete ALL stashes (careful!)
git stash branch new-branch # Create branch from stash
# Advanced
git stash -p # Interactive stashing
git stash push -m "msg" file.js # Stash specific files
Your Action Plan
Here's what you should do right now:
Practice the basic flow on a throwaway repo. Stash, switch branches, pop. Do it until it feels natural.
Add a git alias for your most common stash commands:
git config --global alias.ss 'stash save' git config --global alias.sp 'stash pop' git config --global alias.sl 'stash list'Make it a habit to check
git stash listweekly and clean up old stashes.Next time you're interrupted, resist the urge to commit WIP. Stash instead. Feel the satisfaction of a clean git history.
The Bottom Line
Git stash is like having a pause button for your code. It's not flashy, it's not complicated, but it's one of those tools that, once you internalize it, you'll wonder how you ever lived without.
You'll stop polluting your git history with "WIP" commits. You'll context-switch with confidence instead of dread. You'll look like a git wizard to your junior developers (who will definitely ask "how did you do that?").
Most importantly, you'll spend less time managing git and more time actually writing code. Which is, you know, the whole point.
Now go forth and stash with confidence. Your future self—and your git history—will thank you.
Got a gnarly stash situation you need help with? Or a stash war story to share? The comments are your safe space. We've all been there.