The Git Stash

Temporarily save messy, uncommitted work so you can safely switch context.

Version Control6 min readConcept 27 of 29

The Temporary Shelf

git stash takes the dirty state of your working directory (modified tracked files and staged changes) and saves it onto a stack of unfinished changes.

It then cleans your working directory so it exactly matches the HEAD commit, giving you a clean slate.

Why it matters

Web developers constantly juggle tasks. You might be halfway through building a complex React component when an urgent bug report requires you to switch branches immediately.

You cannot commit broken code, and you cannot switch branches with conflicting uncommitted changes. The stash allows you to save this messy work temporarily, fix the bug, and seamlessly restore your progress later.

How it works

**Pushing to the Stack:** Running git stash packages your current changes into a temporary commit and pushes it onto a local stack (a Last-In, First-Out queue).

**Popping/Applying:** When you return, git stash pop applies the most recent stashed changes to your working directory and removes them from the stack. git stash apply does the same but leaves a copy on the stack.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What happens to your working directory immediately after you run `git stash`?
  2. 2What is the key difference between `git stash pop` and `git stash apply`?
  3. 3Which command is the modern, recommended way to save your changes with a descriptive message?

Remember this

  • Stash saves uncommitted work and cleans your working directory.
  • It allows you to safely switch branches mid-task.
  • Use stash pop to apply and delete, or stash apply to just apply.
  • Always name your stashes using stash push -m.

Done with this concept?

Mark it complete to track your progress. No login needed.