git
version control
stash
tutorial
git stash

Undo a git stash

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Git stash saves uncommitted changes to a stack and reverts the working directory to a clean state. "Undoing" a stash can mean different things: restoring stashed changes back to the working directory (stash pop or stash apply), reverting changes that were already applied from a stash (git checkout .), or recovering a stash that was accidentally dropped. Each scenario has a different command. Understanding the stash stack and how pop vs apply work is essential for safely managing stashed changes.

Applying a Stash (Restore Changes)

bash
1# Apply the most recent stash and remove it from the stash stack
2git stash pop
3
4# Apply the most recent stash but KEEP it in the stack
5git stash apply
6
7# Apply a specific stash (by index)
8git stash list
9# stash@{0}: WIP on main: abc1234 Fix bug
10# stash@{1}: WIP on feature: def5678 Add login
11
12git stash apply stash@{1}  # apply the second stash
13git stash pop stash@{1}    # apply and remove the second stash

pop = apply + drop (removes the stash entry after applying). apply = restore changes but keep the stash entry. Use apply when you might need to apply the same stash to multiple branches.

Undoing a Stash Pop (Reverting Applied Changes)

bash
1# Scenario: you ran "git stash pop" and want to undo it
2
3# If you haven't made any other changes since the pop:
4git checkout .
5# This reverts all unstaged modifications back to the last commit
6
7# But the stash was already removed by pop!
8# The changes are gone from both working directory and stash stack.
9
10# Better approach: use "git stash" again to re-stash
11git stash
12# This saves the popped changes back to the stash stack
13
14# If there were merge conflicts during pop:
15# The stash is NOT dropped when conflicts occur
16git stash list
17# stash@{0} is still there — resolve conflicts or:
18git reset --hard HEAD
19# Then the stash is still available to re-apply later

When git stash pop produces merge conflicts, the stash entry is preserved (not dropped). You can resolve the conflicts, or run git reset --hard HEAD to discard the conflicted state and try again.

Recovering a Dropped Stash

bash
1# If you accidentally dropped a stash with "git stash drop"
2# Git keeps the commit object — you can recover it
3
4# Find the dangling stash commit
5git fsck --unreachable | grep commit
6# unreachable commit abc123def456...
7# unreachable commit 789xyz...
8
9# Or search the reflog (if recent)
10git log --graph --oneline --all $(git fsck --no-reflogs --unreachable 2>/dev/null | grep commit | cut -d' ' -f3)
11
12# Inspect a candidate
13git show abc123def456
14
15# Re-apply the recovered stash
16git stash apply abc123def456
17
18# Or create a branch from it
19git branch recovered-stash abc123def456

Git garbage collects unreachable objects after a period (default 30 days). A recently dropped stash can be recovered with git fsck if garbage collection has not yet run.

Managing the Stash Stack

bash
1# List all stashes
2git stash list
3
4# Show the diff of a stash
5git stash show -p stash@{0}
6
7# Drop a specific stash
8git stash drop stash@{0}
9
10# Clear ALL stashes (irreversible!)
11git stash clear
12
13# Stash with a descriptive message
14git stash push -m "work in progress on login page"
15
16# Stash only specific files
17git stash push -m "partial stash" -- src/login.js src/auth.js
18
19# Stash including untracked files
20git stash push --include-untracked
21
22# Stash including ignored files too
23git stash push --all

Stash Pop vs Apply vs Drop

bash
1# pop = apply + drop (if no conflicts)
2git stash pop
3# Equivalent to:
4git stash apply && git stash drop
5
6# If pop has conflicts, the stash is NOT dropped
7# You must resolve conflicts, then manually drop:
8git stash drop stash@{0}
9
10# apply = restore changes, keep stash
11git stash apply stash@{0}
12
13# drop = remove stash without applying
14git stash drop stash@{0}
CommandApplies ChangesRemoves from StackOn Conflict
git stash popYesYes (if clean)Stash preserved
git stash applyYesNoStash preserved
git stash dropNoYesN/A
git stash clearNoYes (all)N/A

Creating a Branch from a Stash

bash
1# Create a new branch and apply the stash to it
2git stash branch new-feature stash@{0}
3
4# This is equivalent to:
5git checkout -b new-feature
6git stash pop stash@{0}
7
8# Useful when the stash conflicts with the current branch
9# but would apply cleanly to a new branch from the original commit

git stash branch creates a branch from the commit where the stash was originally created, then applies the stash. This avoids conflicts that arise from applying a stash to a branch that has diverged.

Common Pitfalls

  • Using pop when apply is safer: pop removes the stash after applying. If the applied changes cause problems, the stash is already gone. Use apply first, verify the changes are correct, then drop the stash manually.
  • Assuming stash clear is reversible: git stash clear deletes all stash entries immediately. While git fsck may recover recent stashes before garbage collection runs, this is not guaranteed. Never clear stashes without reviewing them with git stash list first.
  • Forgetting about stashed changes: Stash entries are easy to forget because they are not shown in git status or git log. Regularly check git stash list and either apply or drop stashes to keep the stack clean.
  • Stashing on the wrong branch: git stash does not record which branch you were on when you stashed (only which commit). Applying a stash to the wrong branch may introduce unrelated changes. Use git stash push -m "branch-name: description" to label stashes.
  • Not including untracked files: By default, git stash only saves tracked modified files. New files that have not been added with git add are left behind. Use git stash push --include-untracked to stash new files as well.

Summary

  • git stash pop applies and removes the stash; apply keeps it
  • Undo a pop by running git stash again or git checkout . to discard changes
  • When pop has conflicts, the stash is preserved — resolve or reset --hard
  • Recover dropped stashes with git fsck --unreachable before garbage collection
  • Use git stash branch to apply a stash to a new branch cleanly
  • Always use --include-untracked when stashing to capture new files

Course illustration
Course illustration

All Rights Reserved.