This error occurs when Git cannot cleanly apply stashed changes because files in your working directory have been modified since you created the stash. You need to either commit, stash, or discard your current changes before applying the original stash.
When you run `git stash apply` or `git stash pop`, Git attempts to merge your stashed changes with your current working directory. If you've made changes to the same files that exist in the stash since creating it, Git cannot safely apply those stashed changes without potentially losing your current work. This is a protective mechanism. Git refuses to overwrite your uncommitted modifications rather than silently destroying your work. The stash itself remains intact and can be applied once you resolve the conflict between your current changes and the stashed changes. This commonly happens when: - You stash work, switch branches to fix something, make changes to the same files, then try to pop the stash - You stash changes, continue working on the same branch, then forget you have unstaged modifications - Multiple team members work on the same files and you stash frequently
First, identify what's causing the conflict. Check your current modifications:
git statusThen see what files are in the stash:
git stash showFiles appearing in both outputs are the source of the conflict.
The simplest fix is to stage your current changes, which clears the working tree conflict:
git add .
git stash popAfter applying the stash, you can unstage everything if needed:
git reset HEADThis approach keeps both your current changes and stashed changes, though you may need to resolve merge conflicts in the files.
If you want to keep your current changes separate, create a new stash first:
git stash push -m "current work in progress"
git stash pop stash@{1}The original stash is now at position stash@{1} because the new stash took position stash@{0}. You can later apply the newer stash when ready.
If your current changes are complete enough to commit:
git add .
git commit -m "WIP: save current progress"
git stash popAfter applying the stash, you can amend the commit or continue working. If the stash creates conflicts, resolve them and commit.
If you don't need your current changes and want to apply the stash instead:
git checkout .
git stash popWarning: This permanently discards all uncommitted changes. Make sure you don't need them before running this command.
For complex cases where nothing else works, you can force-apply the stash as a patch:
git stash show -p | git applyThis applies the stash changes on top of your current changes. If there are conflicts, the patch will fail for those specific hunks, allowing you to manually resolve them.
To include untracked files from the stash:
git stash show -p --include-untracked | git applyFor stashes that are significantly different from your current state, create a new branch:
git stash branch new-feature-branchThis creates a new branch from the commit where the stash was originally made, applies the stash, and drops it if successful. You can then merge this branch into your current work.
### Understanding Stash Internals
A Git stash is actually a special commit that stores:
1. The state of your working directory
2. The state of your staging area (index)
3. Optionally, untracked files (with -u) or all files including ignored ones (with -a)
When you apply a stash, Git performs a three-way merge between the stash commit, the original parent commit, and your current HEAD. This is why conflicts can occur.
### Viewing Stash Contents Without Applying
To safely inspect what a stash contains before applying:
# Summary of changed files
git stash show stash@{0}
# Full diff of all changes
git stash show -p stash@{0}
# Show untracked files too
git stash show -p --include-untracked stash@{0}### Selective Stash Application
Instead of applying the entire stash, you can checkout individual files:
git checkout stash@{0} -- path/to/specific/file.jsWarning: This overwrites your current version of that file with the stashed version without attempting a merge.
### Git Stash Pop vs Apply
- git stash pop: Applies the stash and removes it from the stash list (if successful)
- git stash apply: Applies the stash but keeps it in the stash list
When conflicts occur with pop, the stash is NOT removed. You must manually run git stash drop after resolving conflicts if you want to remove it.
### Preventing Future Issues
Consider these practices to avoid stash conflicts:
1. Use descriptive stash messages:
git stash push -m "feature X: partial login form"2. List stashes before applying:
git stash list3. Apply stashes promptly - the longer a stash sits, the more likely conflicts become
4. Consider feature branches instead - for longer-term work storage, a branch is often more appropriate than a stash
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