This error occurs when running git stash apply or git stash pop, and Git detects uncommitted local changes that conflict with the stashed changes. Git refuses to proceed to prevent data loss.
When you run `git stash apply` or `git stash pop`, Git attempts to merge the stashed changes back into your working directory. However, if you have uncommitted modifications to files that are also modified in the stash, Git cannot safely merge them without potentially overwriting your current work. This is a safety mechanism. Git is telling you that applying the stash would overwrite uncommitted changes in your working directory. Rather than silently destroying your work, Git stops and asks you to deal with the conflicting changes first. The error typically appears when you stashed some changes, then made additional modifications to the same files (or received them via pull/checkout), and now want to restore your stash. Git sees that both your current working directory and the stash have changes to the same files, and it cannot automatically reconcile them.
The simplest solution is to stage your current changes, which allows git stash apply to proceed:
# Stage all current changes
git add .
# Now apply the stash
git stash apply
# Unstage if you don't want to commit yet
git reset HEADThis works because staging moves your changes to the index, giving Git a clean working tree to apply the stash to.
If your current changes are ready to be committed, commit them first:
# Commit current work
git add .
git commit -m "WIP: save current progress"
# Apply the stash
git stash apply
# Optional: reset the WIP commit but keep changes
git reset --soft HEAD~1This preserves both sets of changes and lets you decide how to combine them.
If your current uncommitted changes are not important, you can discard them:
# WARNING: This permanently discards uncommitted changes
git checkout .
# Now apply the stash
git stash applyCaution: Only use this if you are certain you don't need your current changes. This action cannot be undone.
Use git stash branch to create a new branch with your stashed changes applied cleanly:
# Create a new branch from the stash
git stash branch my-stashed-work
# This creates a branch at the commit where you originally stashed,
# applies the stash, and drops it from the stash listThis is especially useful when your current branch has diverged significantly from when you created the stash.
As a last resort, you can force the stash to apply using a patch:
# Show stash as a patch and apply it
git stash show -p | git apply
# Or with the 3-way merge option for conflict markers
git stash show -p | git apply --3wayThe --3way option will create conflict markers in files where automatic merge isn't possible, allowing you to resolve conflicts manually.
### Understanding Stash Internals
A stash in Git is actually a special commit that stores the state of your working directory and index. When you run git stash apply, Git performs a three-way merge between:
1. The commit where the stash was created
2. The current HEAD
3. The stash contents
This merge can fail when there are uncommitted changes, because Git has no committed state to use as a basis for merging your current changes.
### Stash Apply vs Stash Pop
- git stash apply: Applies the stash but keeps it in the stash list
- git stash pop: Applies the stash and removes it from the list (only if successful)
When either command fails with conflicts, the stash is NOT removed from the list, so your stashed work is safe.
### Handling Untracked Files
If the error is about untracked files, the stash may have been created with git stash -u or git stash --include-untracked. In this case, you may need to move or delete the conflicting untracked files before applying.
### Using --index Flag
The --index flag attempts to reinstate the staged state from when you created the stash:
git stash apply --indexThis can sometimes work when the regular apply fails, particularly if the conflicts are in the staging area rather than the working directory.
warning: 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