Pause Work with Git Stash and Get It Back Safely

You are building a chat feature on a branch and suddenly your manager says: “Urgent bug on main, fix it now.”
Your feature isn’t complete, so you don’t want to commit half-done work. This is exactly where git stash helps: it hides your local changes safely, lets you switch branches, and later you can bring those changes back.


Scenario Setup (Feature Work in Progress)

We are on feature_chat with some uncommitted edits in chat.txt.

# you edit the file, but don't commit
echo "more changes..." >> chat.txt

git status -s
#  M chat.txt

Attempting to switch:

git switch main
# error: please commit your changes or stash them

Use Git Stash to Hide Current Changes

git stash -m "WIP: chat.txt edits"

or Modern, explicit form (recommended):

git stash push -m "WIP: chat.txt edits"

Older syntax you may still see:

git stash           # same as: git stash push
# or
git stash save "WIP: chat.txt edits"

Diagram — after stashing (HEAD still on feature branch’s latest commit):

Think of the stash stack as a shelf where Git put your local changes, tied to the commit you were on.

Check what’s stashed:

git stash list
# stash@{0}: On feature_chat: WIP: chat.txt edits

Switch to Main and Fix the Bug

Now switching works because the working tree is clean:

git switch main
# success

Create a short-lived bugfix branch and fix:

git branch bugfix1
git switch bugfix1

# ...fix files...
git add .
git commit -m "Fix: urgent issue on main"

(Merging bugfix back to main is out of scope for this video; focus is on stash.)


Come Back to Your Feature and Restore the Work

Switch back to your feature branch:

git switch feature_chat

Two common ways to restore:

  1. Apply (keep it in stash for later reuse)
git stash apply stash@{0}
  1. Pop (apply and remove from stash)
git stash pop stash@{0}

After apply/pop, your chat.txt changes are back in the working directory.

Verify:

git status -s
#  M chat.txt

cat chat.txt
# ...your WIP lines are back...

Useful Stash Variations

  • Show all stashes:

    git stash list
    
  • See what a stash contains (diff):

    git stash show -p stash@{0}
    
  • Keep index (stage) while stashing:

    git stash push -k -m "WIP: keep index"
    
  • Include untracked files too:

    git stash push -u -m "WIP: include untracked"
    
  • Drop a specific stash:

    git stash drop stash@{0}
    
  • Clear all stashes (dangerous):

    git stash clear
    

Summary

  • Problem: Need to pause unfinished work and switch branches urgently.
  • Solution: git stash push -m "..." to hide local changes.
  • Switch: Move to main (or a bugfix branch), commit the fix.
  • Return: Go back to your feature and apply or pop the stash to restore edits.
  • Extras: --include-untracked (-u), --keep-index (-k), stash show -p, stash drop, stash clear.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉