Published on
|
5 min read

How to Use Git Stash Like a Pro: Beyond the Basics

We’ve all been there: You’re deep into a complex refactor, 50 lines deep into an experimental feature, and suddenly BAM. A critical bug hit production, and you need to switch branches now.

You aren't ready to commit your "messy" WIP code, but you can't throw it away either. This is where git stash becomes your best friend. But if you're only using git stash and git stash pop, you're leaving 90% of its power on the table.

In this guide, we’ll move past the basics and learn how to manage your workspace like a pro.


Why Git Stash is a Productivity Superpower

At its core, git stash takes your uncommitted changes (both staged and unstaged), records them, and reverts your working directory to match the HEAD commit.

It’s the digital equivalent of a "Pause" button for your coding session. Using it effectively means:

  • Zero "WIP" Commits: No more polluting your git history with "saving work" or "temporary" commits.
  • Fearless Context Switching: Jump between bug fixes and features in seconds.
  • Cleaner Pulls: Stash your changes before pulling from main to avoid messy local merges.

The Pro's Step by Step Framework

If you want to use stashing properly, follow this workflow to ensure you never lose a line of code.

Step 1: Name Your Stashes

By default, Git gives stashed changes generic names like WIP on main: 6a7b8c. After three stashes, you'll have no idea what is what.

The "Amateur" Way:

git stash

The "Pro" Way:

git stash push -m "Refactoring the auth middleware"

Why? The -m (message) flag makes it searchable and readable when you list your stashes later.

Step 2: Don't Forget Untracked Files

Standard git stash only tracks files already in the index. New files you just created? They'll be left sitting in your directory, potentially breaking the other branch you're switching to.

The Pro Command:

git stash -u
# or
git stash --include-untracked

Step 3: Inspect Before You Pop

Don't blindly pop. If you've been working on multiple things, check what's in the stash first.

View your stack:

git stash list

See the actual diff inside a specific stash:

git stash show -p stash@{0}

Specialized Git Stash Techniques

1. Partial Stashing (The "Interactive" Pro)

Sometimes you only want to stash some of your changes while keeping others to finish a commit.

git stash -p

Git will walk you through every "hunk" of code and ask: Stash this hunk? [y,n,q,a,d,j,J,g,/,e,?]

2. Apply vs. Pop: Know the Difference

  • git stash pop: Applies the changes and deletes them from the stash list.
  • git stash apply: Applies the changes but keeps them in the stash list.

Pro Tip: Use apply if you want to test changes across multiple branches. Use pop only when you are certain you're done with that "paused" session.

3. Stashing into a New Branch

If your stashed changes have diverged too much from the current branch, applying them might cause massive merge conflicts. You can "pop" your stash directly into its own new branch:

git stash branch feature-auth-fix stash@{0}

This creates a new branch, checks out the commit you were on when you stashed, and then applies the changes there. Pure magic.


Naming Traps and Best Practices

  • The "Stash Stack" Trap: Don't let your stash list grow to 20 items. If a stash sits there for more than 48 hours, it should probably be a branch instead.
  • Clean up frequently: Use git stash clear (to wipe everything) or git stash drop stash@{n} (to wipe one) to keep your stack relevant.
  • Stash before Rebase: If you're rebasing your feature branch onto main, stashing your local uncommitted tweaks first will prevent the rebase from failing due to a "dirty" working directory.

Standard Git Stash Cheat Sheet

CommandWhat it does
git stash push -m "msg"Stash with a helpful description
git stash listSee all your paused sessions
git stash applyBring changes back (keep the backup)
git stash popBring changes back (delete the backup)
git stash show -pSee exactly what code is inside
git stash dropDelete the most recent stash
git stash -uStash everything, including new files

FAQ: Common Git Stash Hurdles

What if I have a merge conflict after popping?

Git will leave the changes in your working directory but won't delete the stash. You'll need to resolve the conflicts manually, then run git stash drop yourself once you're happy.

Can I stash ignored files too?

Yes. Use git stash -a (or --all) to stash both untracked and ignored files (like .env or build artifacts). Use this with caution!

Does stashing work across branches?

Absolutely. You can stash on feature-A and pop on feature-B. This is a common way to "move" work-in-progress code from the wrong branch to the right one.

Conclusion: Master the Frictionless Workflow

Git Stash isn't just a temporary storage bin; it's a tool for maintaining a high-velocity workflow. By naming your stashes, including untracked files, and using partial stashing, you eliminate the friction of context switching.

The next time a "quick fix" interrupts your flow, don't rush a messy commit. Push, Switch, Fix, Switch, Pop. That’s how a pro handles the chaos of software engineering.


Mustafiz Kaifee

Mustafiz Kaifee

@mustafiz_kaifee
Share