Git Stash

1 min

Table of contents

  1. What Is Stash?
  2. Immediate Practice
  3. Essential Commands
  4. Typical Use Case

1. What Is Stash?

Git stash = Temporarily save your UNCOMMITTED modifications.

Analogy: It is like putting your things in a secret drawer before doing something else.

Why?

  • You are coding on feature-A
  • EMERGENCY: critical bug on main
  • You do not want to commit half-finished code
  • Solution: git stash → switch → fix → come back → git stash pop

Back to table of contents

2. Immediate Practice

bash
mkdir demo-stash && cd demo-stash
git init

echo "print('v1')" > app.py
git add . && git commit -m "Initial version"

# You start coding
echo "print('work in progress')" >> app.py

# EMERGENCY! You have to switch branch
git stash                    # Save the WIP
git status                   # Clean working directory!

# Do something else...
git checkout -b hotfix
echo "print('CRITICAL FIX')" > fix.py
git add . && git commit -m "Urgent fix"

# Go back to the original work
git checkout main
git stash pop               # Recover the work in progress

Magic! Your modifications are back.

Back to table of contents

3. Essential Commands

CommandAction
git stashSave the modifications
git stash popRecover + delete from the stash
git stash listSee all stashes
git stash applyRecover WITHOUT deleting
git stash dropDelete a stash
git stash clearEmpty all stashes

Back to table of contents

4. Typical Use Case

Classic situation:

  1. You are coding peacefully
  2. Colleague: "There's a bug in prod!!!"
  3. You: git stash
  4. Quick fix and deploy
  5. You: git stash pop
  6. You continue as if nothing happened

Stash = Your best friend for interruptions!

Back to table of contents