Introduction
When git stash pop encounters a conflict between the stashed changes and the current working directory, Git may abort the operation, leaving the stash in an uncertain state. In some cases, the pop partially applies and the stash is dropped, risking data loss:
git stash pop
# error: Your local changes to the following files would be overwritten by merge:
# src/config.js
# Please commit your changes or stash them before you merge.
# Aborting
# The stash entry is kept in case you need it again.Symptoms
git stash popfails with "would be overwritten" error- Stash is not dropped (safe) but changes are not applied
- In some cases, stash is dropped but changes were not fully applied
- Working directory has a mix of current and stashed changes
- Cannot determine which version of conflicting files is correct
Common Causes
- Both the stash and the working directory modified the same files
- Stash was created long ago and the codebase has changed significantly since
- Multiple stashes with overlapping changes
- Stash applied on a different branch than where it was created
- Working directory has uncommitted changes that conflict with the stash
Step-by-Step Fix
- 1.Check if the stash is still available after a failed pop:
- 2.```bash
- 3.git stash list
- 4.# If the stash is still listed, it was not dropped
- 5.
` - 6.Apply the stash without dropping it (safer than pop):
- 7.```bash
- 8.git stash apply stash@{0}
- 9.# This keeps the stash intact even if conflicts occur
- 10.
` - 11.Resolve conflicts in the affected files:
- 12.```bash
- 13.git status
- 14.# Shows files with conflicts
- 15.# Edit each conflicted file to resolve
- 16.git add <resolved-files>
- 17.
` - 18.If the stash was dropped but not fully applied, recover it:
- 19.```bash
- 20.# Find the stash commit via reflog
- 21.git reflog | grep stash
- 22.# Or find unreachable commits
- 23.git fsck --no-reflogs --unreachable
- 24.# Cherry-pick or merge the stash commit to recover its changes
- 25.git cherry-pick <stash-commit-hash>
- 26.
` - 27.For complex conflicts, apply the stash to a temporary branch:
- 28.```bash
- 29.git checkout -b temp-stash-recovery
- 30.git stash apply stash@{0}
- 31.# Resolve conflicts in isolation
- 32.# Then cherry-pick or copy the resolved changes back to your main branch
- 33.git checkout main
- 34.git cherry-pick temp-stash-recovery
- 35.git branch -d temp-stash-recovery
- 36.
`
Prevention
- Always use
git stash applyinstead ofgit stash popto keep the stash as a backup - Commit your working changes before popping a stash to avoid conflicts
- Create stashes frequently with descriptive messages:
git stash push -m "WIP: fixing auth bug" - Clean up old stashes regularly:
git stash drop stash@{N} - Use
git stash show -p stash@{0}to preview stash contents before applying - Avoid working on the same files across multiple branches simultaneously
- Consider using
git worktreefor parallel work instead of stashing - For critical uncommitted work, commit to a temporary branch instead of stashing