Shelving Changes Temporarily with git stash (save, pop, apply, drop)0%
Undoing Changes Safely: git restore, git reset, and git revert

Shelving Changes Temporarily with git stash (save, pop, apply, drop)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Shelving Changes Temporarily with git stash (save, pop, apply, drop)

Software development is full of interruptions: you are deep in the middle of writing an unfinished feature when an urgent production bug alert arrives. Your current code is broken and half-written—you can't commit it, but you also can't lose it. The git stash command is your temporary clipboard.


1. How Git Stash Works

git stash takes the dirty state of your working directory (all modified tracked files and staged changes) and saves it onto an internal stack of unfinished changes, returning your working directory to a clean HEAD state!

Output
[ Working Directory: Broken, half-finished code ]
|
git stash
|
v
[ Stash Stack ] <------------------ [ Clean Working Directory ]
stash@{0}: WIP on feature-cart (Ready to switch branches & fix bug!)

2. Core Stash Commands

A. Stashing Your Changes

Bash / Terminal
# Quick stash
git stash
 
# Stash with a descriptive message (Recommended!)
git stash push -m "WIP: half-implemented payment modal"

B. Including Untracked Files (-u)

By default, git stash only stashes tracked files. If you created brand new files that haven't been committed before, add the -u (or --include-untracked) flag:

Bash / Terminal
git stash -u

3. Inspecting and Restoring Stashes

A. View All Saved Stashes (git stash list)

Bash / Terminal
git stash list

Output:

Output
stash@{0}: On feature-cart: WIP: half-implemented payment modal
stash@{1}: On main: WIP: temporary database connection string

B. Restoring Changes: git stash pop vs. git stash apply

CommandRestores Changes to Working Tree?Removes Stash from Stack?
git stash popYesYes (deletes the stash item if no conflicts occur)
git stash applyYesNo (leaves the stash on the stack so you can reuse it elsewhere)
Bash / Terminal
# Apply most recent stash and remove it from stack
git stash pop
 
# Apply a specific older stash by index without removing it
git stash apply stash@{1}

4. Cleaning Up Stashes

Stashes accumulate over time and consume disk space. Clean them up regularly:

Bash / Terminal
# 1. Drop a specific stash from the stack
git stash drop stash@{0}
 
# 2. Clear ALL stashes completely (irreversible!)
git stash clear

5. Typical Real-World Scenario

  1. 1
    Working on feature-redesign, edited 3 files. Code doesn't compile yet.
  2. 2
    Manager asks for urgent hotfix on main.
  3. 3
    You stash: git stash push -m "WIP redesign".
  4. 4
    Switch to main: git switch main.
  5. 5
    Fix bug, commit, and push: git commit -am "fix: hotfix security header" -> git push.
  6. 6
    Switch back: git switch feature-redesign.
  7. 7
    Restore your work: git stash pop.
  8. 8
    Continue coding right where you left off!

Interactive Quiz

Practice Quiz

Question 1
What does git stash do?
Question 2
Why does git stash ignore newly created untracked files by default?
Question 3
What is the primary difference between git stash pop and git stash apply?
Question 4
Which command lists all stashes currently stored in the repository?
Question 5
How can you discard and delete all stashed entries from your repository completely?
Next Lesson

Undoing Changes Safely: git restore, git reset, and git revert

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext