How to Use git stash pop: Restore Stashed Changes Easily
Use
git stash pop to apply the most recent stashed changes back to your working directory and remove them from the stash list. This command restores your saved work so you can continue editing without losing progress.Syntax
The basic syntax of git stash pop is simple and can include an optional stash reference.
git stash pop: Applies the latest stash and removes it from the stash list.git stash pop stash@{n}: Applies a specific stash by its indexnand removes it from the stash list.
bash
git stash pop [<stash>]Example
This example shows how to save changes with git stash, then restore them using git stash pop.
bash
git status
# Shows modified files
git stash
# Saves changes and cleans working directory
git stash list
# Lists saved stashes
git stash pop
# Applies the latest stash and removes it
git status
# Shows working directory with restored changesOutput
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: example.txt
Saved working directory and index state WIP on main: abc1234 Commit message
stash@{0}: WIP on main: abc1234 Commit message
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: example.txt
Common Pitfalls
Common mistakes when using git stash pop include:
- Not resolving conflicts if changes clash with current files.
- Assuming stash is kept after pop;
popremoves the stash, unlikegit stash apply. - Using
popwithout checking stash list can cause unexpected results.
Always check your stash list with git stash list before popping.
bash
git stash pop # If conflicts occur, git will pause and ask you to resolve them # Wrong way: ignoring conflicts # Right way: git status # See conflicted files # Edit files to fix conflicts git add <fixed-files> git commit -m "Resolve stash pop conflicts"
Quick Reference
| Command | Description |
|---|---|
| git stash pop | Apply latest stash and remove it from stash list |
| git stash pop stash@{2} | Apply stash number 2 and remove it |
| git stash apply | Apply stash without removing it |
| git stash list | Show all saved stashes |
| git stash drop stash@{n} | Remove a specific stash without applying |
Key Takeaways
Use
git stash pop to restore and remove the latest stash in one step.Check your stash list with
git stash list before popping to avoid surprises.Resolve any conflicts immediately after popping to keep your code clean.
git stash pop removes the stash; use git stash apply if you want to keep it.Always commit or stash your changes before switching branches to avoid losing work.