This error occurs when running git stash pop or git stash apply and Git cannot restore untracked files because files with the same names already exist in your working directory. Git refuses to overwrite existing files to prevent data loss.
When you stash changes with `git stash -u` or `git stash --include-untracked`, Git saves both tracked and untracked files to the stash. The stash internally creates a special commit structure where untracked files are stored in a separate "third parent" commit (stash^3). When you later try to apply or pop this stash, Git attempts to restore all the stashed content. However, if any of the previously untracked files now exist in your working directory (perhaps because they were pulled from a remote branch, created manually, or generated by a build process), Git cannot safely restore them without potentially overwriting important data. Unlike tracked file conflicts that can be merged, untracked files have no merge history. Git has no way to reconcile the stashed version with the existing file, so it aborts the entire untracked file restoration while the tracked changes may partially apply.
First, examine the error output to identify which files are causing the conflict:
# The error message typically shows:
# error: could not restore untracked files from stash
# Already exists, no checkout: path/to/file.txt
# Already exists, no checkout: another/file.js
# List the untracked files in your stash
git stash show --include-untracked stash@{0}
# Or show only the untracked portion
git show --name-only stash^3Note which files exist both in your working directory and in the stash.
If the existing files in your working directory are not needed (e.g., they're duplicates or regeneratable), remove them first:
# Delete the specific conflicting files
rm path/to/file.txt another/file.js
# Now apply the stash
git stash popThis is the simplest solution when the local files can be discarded.
If you need both versions, manually extract the stashed untracked files to a different location:
# Show what untracked files are in the stash
git show --name-only stash^3
# Extract specific untracked files to a different path
git show stash^3:path/to/file.txt > path/to/file.txt.stashed
# Or extract to a backup directory
mkdir -p ~/stash-backup
git checkout stash^3 -- . 2>/dev/null || true
# This may fail but you can also use:
git stash show -p --include-untracked | git apply --include-untracked
# Apply tracked changes only (skip untracked)
git stash show -p | git applyCompare the versions and manually merge if needed.
Create a new branch from the stash's original commit, which cleanly applies all stashed changes:
# Create a branch from the stash
git stash branch recovered-changes stash@{0}
# This creates a new branch at the commit where the stash was created
# and applies all stashed changes (tracked and untracked) cleanly
# Now you can merge or cherry-pick changes to your target branch
git checkout main
git merge recovered-changes
# or selectively copy filesThis approach avoids conflicts entirely by restoring the exact state when the stash was created.
Preserve existing files by moving them before restoring the stash:
# Create a backup directory
mkdir -p ~/backup-before-stash
# Move conflicting files
mv path/to/file.txt ~/backup-before-stash/
mv another/file.js ~/backup-before-stash/
# Apply the stash
git stash pop
# Compare and reconcile files
diff ~/backup-before-stash/file.txt path/to/file.txtThis preserves both versions for manual comparison.
After successfully recovering your changes, clean up the stash if it wasn't automatically dropped:
# Check if the stash still exists
git stash list
# If the stash is still there after applying, drop it manually
git stash drop stash@{0}Remember: git stash pop only drops the stash if the apply succeeds completely. If there were errors, the stash remains.
Understanding stash internals:
A stash with untracked files creates three commits:
- stash (or stash@{0}): The working directory state
- stash^1: The HEAD commit when stash was created
- stash^2: The index state
- stash^3: The untracked files (only exists if -u or -a was used)
You can access the untracked files directly:
# List untracked files in stash
git ls-tree -r stash^3 --name-only
# View content of a specific untracked file
git show stash^3:filename.txt
# Extract all untracked files to current directory
git checkout stash^3 -- .Known Git bug (versions 2.33.1 - 2.34.x):
There was a bug in Git versions 2.33.1 through 2.34.x where untracked files in a stash were not recovered properly during rebase operations when merge conflicts occurred. This was fixed in Git 2.35.0. If you're affected, upgrade Git:
# Check Git version
git --version
# Ubuntu/Debian
sudo add-apt-repository ppa:git-core/ppa
sudo apt update && sudo apt install git
# macOS
brew upgrade gitPreventing this issue:
Consider adding commonly generated files to .gitignore to avoid stashing them:
# Build outputs
dist/
build/
*.o
*.pyc
# Dependencies
node_modules/
vendor/Or use git stash push with specific paths instead of -u:
# Stash only specific files
git stash push -m "my changes" path/to/specific/file.txtAlternative workflow:
Instead of stashing untracked files, consider committing them to a WIP (work in progress) branch:
git checkout -b wip-feature
git add -A
git commit -m "WIP: saving progress"
git checkout main
# Later...
git checkout wip-featurewarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server