Course contentsShow
← Computer Science
Lesson 2206 of 2,87249. Version Control with GitFree lesson

Git Stash Management

Temporarily save uncommitted changes with git stash and manage multiple stashed states.

Git Stash Management

You'll learn how to temporarily shelve uncommitted changes and juggle multiple saved work states without committing them.

The Core Idea

Imagine you're halfway through painting a room when someone urgently asks you to fix a leak in another room. You can't just abandon your paint supplies everywhere, but you also don't want to clean up completely because you're coming right back. Git stash is like a temporary storage shelf where you can safely set aside your work-in-progress, switch contexts, then retrieve everything exactly as you left it.

When you run git stash, Git saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the last commit. This lets you switch branches or pull updates without committing half-finished work.

Managing Multiple Stashes

Git maintains a stack of stashed states. Each time you stash, it goes on top:

  • git stash or git stash push — save current changes
  • git stash list — view all stashed states (numbered 0, 1, 2...)
  • git stash apply stash@{n} — restore a specific stash without removing it
  • git stash pop — restore the most recent stash and remove it from the stack
  • git stash drop stash@{n} — delete a specific stash
  • git stash clear — delete all stashes

You can also name your stashes for clarity: git stash push -m "description".

Practical Use

Use git stash show to preview what's in a stash before applying it. If you've stashed untracked files, add the -u flag: git stash -u. Think of the stash list as a clipboard history—you can peek at old "copies" and restore any of them.

Key Takeaway: Git stash provides a temporary holding area for uncommitted work, allowing you to context-switch cleanly and manage multiple saved states on a stack.