Undo a git stash
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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)
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)
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
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
Stash Pop vs Apply vs Drop
| Command | Applies Changes | Removes from Stack | On Conflict |
git stash pop | Yes | Yes (if clean) | Stash preserved |
git stash apply | Yes | No | Stash preserved |
git stash drop | No | Yes | N/A |
git stash clear | No | Yes (all) | N/A |
Creating a Branch from a Stash
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
popwhenapplyis safer:popremoves the stash after applying. If the applied changes cause problems, the stash is already gone. Useapplyfirst, verify the changes are correct, thendropthe stash manually. - Assuming
stash clearis reversible:git stash cleardeletes all stash entries immediately. Whilegit fsckmay recover recent stashes before garbage collection runs, this is not guaranteed. Never clear stashes without reviewing them withgit stash listfirst. - Forgetting about stashed changes: Stash entries are easy to forget because they are not shown in
git statusorgit log. Regularly checkgit stash listand either apply or drop stashes to keep the stack clean. - Stashing on the wrong branch:
git stashdoes not record which branch you were on when you stashed (only which commit). Applying a stash to the wrong branch may introduce unrelated changes. Usegit stash push -m "branch-name: description"to label stashes. - Not including untracked files: By default,
git stashonly saves tracked modified files. New files that have not been added withgit addare left behind. Usegit stash push --include-untrackedto stash new files as well.
Summary
git stash popapplies and removes the stash;applykeeps it- Undo a pop by running
git stashagain orgit checkout .to discard changes - When
pophas conflicts, the stash is preserved — resolve orreset --hard - Recover dropped stashes with
git fsck --unreachablebefore garbage collection - Use
git stash branchto apply a stash to a new branch cleanly - Always use
--include-untrackedwhen stashing to capture new files
Related reading
- Undo a particular commit in Git that's been pushed to remote repos
- Undo a particular commit in Git that's been pushed to remote repos
- Undo change in git not rewriting history
- Undo git pull, how to bring repos to old state
- Undo Git pull. How to bring repositories to the old state
- Undo git stash pop that results in merge conflict
- Undo git stash pop that results in merge conflict
- Undo git update-index --assume-unchanged file
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.