Chapter 9: Git Stash

Git stash. ☕

It’s February 13, 2026, still Hyderabad, and imagine this situation (it happens to everyone):

You’re working on a feature, your code is half-broken and messy, you have 5 modified files + 2 new files… Suddenly your team lead messages: “Hey, can you quickly fix that bug in production on the main branch? It’s urgent!”

What do you do? You don’t want to commit half-done messy code. You don’t want to create a temporary branch just for one small fix. You don’t want to lose your current work either.

This is exactly what git stash is made for.

What is git stash? (Simple human explanation)

git stash is Git’s “save my current work temporarily and clean my working directory” command.

It does three things at once:

  1. Takes all your uncommitted changes (both staged and unstaged)
  2. Saves them in a safe place (a special stack inside .git/)
  3. Reverts your working directory and index to match the last commit (clean slate)

Later, when you’re ready, you can bring those changes back exactly as they were — even across branches.

Think of it like:

  • You’re cooking a complicated biryani → half-done, masala everywhere, rice soaking
  • Someone says “quickly make tea for guests”
  • You stash the biryani mess in the fridge (nothing spoils)
  • Kitchen becomes clean → you make tea quickly
  • After guests leave → you take biryani out of fridge and continue exactly where you left off

That’s git stash.

The Stash Stack – Very Important Concept

Git keeps stashes in a stack (like a pile of plates):

  • Newest stash = stash@{0}
  • Previous one = stash@{1}
  • Older ones = stash@{2}, etc.

You can:

  • Apply the latest stash (git stash apply or git stash pop)
  • Apply an older one
  • List all stashes
  • Drop (delete) stashes you don’t need

Hands-on Example – Let’s Do It Step by Step

Create a small playground:

Bash

Make initial commit:

Bash

Now start real work:

Bash

Check status:

Bash

You should see:

text

→ Messy! Half staged, half not, new file, modified file.

Now — emergency! You need to quickly fix something on main, but you don’t want to commit this mess.

Stash everything:

Bash

Or shorter (most people do this):

Bash

You’ll see:

text

Now check:

Bash
text

→ All your changes disappeared! (but they are safe)

Look at your stashes:

Bash

Typical output:

text

Bring your work back (two common ways)

Way 1: Apply + keep the stash (safe)

Bash

→ Changes come back, stash still exists in the list

Way 2: Apply + remove from stack (most common)

Bash

→ Same as apply + then git stash drop

Now your changes are back exactly as they were — staged files still staged, unstaged still unstaged.

Useful & Common Stash Commands (2026 Daily Use)

What you want to do Command Effect
Stash everything (recommended) git stash push -m “descriptive message” Saves with message — easier to remember later
Quick stash (no message) git stash Default message: “WIP on branch…”
Stash only unstaged changes (keep staged) git stash –keep-index Useful when you want to commit staged part first
Stash including untracked files git stash push –include-untracked or -u — very common!
Stash everything including ignored files git stash push –all or -a Rare — includes .env, logs, etc.
List all stashes git stash list See the stack
Show what’s inside a stash git stash show git stash show -p -p = full diff
Apply latest stash (keep in stack) git stash apply Safe — can apply multiple times
Apply specific stash git stash apply stash@{2} Useful when you have many stashes
Apply + delete from stack git stash pop Most common daily flow
Delete a stash git stash drop stash@{1} or git stash drop → drops latest
Delete all stashes git stash clear Careful — irreversible

Real-World Scenarios Where git stash Saves You

  1. Quick bug fix on main → stash → checkout main → fix → commit → push → stash pop → continue feature
  2. Switch branch suddenly → git stash → git checkout feature/login → work → later git stash pop
  3. Pull fails because of local changes → git stash → git pull → git stash pop
  4. Want to test clean state → git stash → test → git stash pop

Common Mistakes & Gotchas

  • Forgot to stash untracked files → they stay behind → git stash -u fixes this
  • Pop on wrong branch → changes might conflict → always check git status after pop
  • Too many unnamed stashes → hard to know which is which → always use -m “message”
  • Trying to stash when nothing to stash → Git will tell you “No local changes to save”

Mini Homework – Try Right Now!

  1. Make some messy changes in any repo (edit 2 files, create 1 new file, stage 1 file)
  2. git stash push -m “half-done dark mode changes”
  3. git status → should be clean
  4. Do something else (like git log, create a new file and commit)
  5. git stash pop
  6. See your changes come back perfectly

Got the feeling? git stash is your emergency bookmark for unfinished work.

Next?

  • Want to see git stash –include-untracked vs normal?
  • How to manage many stashes?
  • Or move to conflicts when popping stashes?

Just tell me — we’ll keep going step by step. You’re doing really well! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *