Tour of Git
History & Undoing Changes / Lesson 3.4

git stash — Saving Work Temporarily

The git stash command saves your uncommitted changes and restores a clean working directory. It's like putting your work on a shelf to deal with later.

When to Use Stash

  • You need to switch branches but have uncommitted work
  • You want to pull updates but have local changes
  • You want to quickly test something on a clean state

Basic Commands

  • git stash — Save changes and clean working directory
  • git stash pop — Restore the most recent stash and remove it
  • git stash list — Show all stashed entries
  • git stash drop — Remove the most recent stash without applying

How It Works

Working Dir (dirty)  →  git stash  →  Working Dir (clean)
                                            ↓
                                      Stash Stack
                                     [stash@{0}]
                                     [stash@{1}]
                                     [stash@{2}]

Stashes are stored in a stack — last in, first out.

Try it!

Practice stashing and restoring:

  1. Create and commit a file: echo "base" > work.txt && git add work.txt && git commit -m "Add work"
  2. Make changes: echo "in progress" > work.txt
  3. Stash the changes: git stash
  4. Verify clean state: cat work.txt — shows "base" again
  5. Restore the stash: git stash pop
  6. Verify restored: cat work.txt — shows "in progress" again

Multiple Stashes

You can stash multiple times. Each new stash pushes onto the stack:

git stash         # saves current work as stash@{0}
git stash         # saves more work as stash@{0}, previous becomes stash@{1}
git stash list    # shows all entries
git stash pop     # restores stash@{0}
🎯

Goal

Stash your changes, verify clean state, then restore them with stash pop

Terminal
$

No commits yet. Run `git init` and create your first commit.