This error occurs when Git cannot locate or parse a stash reference, typically due to corrupted stash files, shell escaping issues with curly braces, or damage from git filter-branch operations. The fix involves repairing or clearing the stash.
When you see the error 'stash@{0}' is not a valid reference, Git is telling you that it cannot resolve the stash reference you're trying to access. The stash in Git is implemented as a special ref stored in .git/refs/stash with a reflog at .git/logs/refs/stash that tracks multiple stashed changes. This error typically means one of several things: the stash reference file has become corrupted, the reflog for the stash is damaged, or there's a shell escaping issue preventing Git from correctly parsing the curly braces in the stash syntax. In some cases, operations like git filter-branch can corrupt existing stashes, making them unreadable. The stash@{N} syntax is Git's reflog notation, where N represents the index of the stash entry (0 being the most recent). When this reference becomes invalid, you lose the ability to apply, pop, or drop that specific stash entry through normal commands.
First, check if your stashes are visible and identify which ones might be corrupted:
git stash listIf this shows entries but you can't apply them, or if it produces errors, the stash reflog is likely corrupted. You can also check the raw reflog:
git reflog show stashNote any commit hashes shown - these may be recoverable later.
Shell escaping can cause issues with the curly braces in stash references. Try quoting the stash reference:
git stash apply 'stash@{0}'Or use the --index flag with a numeric argument:
git stash apply --index 0On Windows or MSYS2, you may need to escape differently:
git stash apply "stash@{0}"Before clearing corrupted stashes, try to recover the underlying commits:
git reflog stashThis may show commit hashes even if the stash reference is broken. If you see commits like abc1234 stash@{0}: WIP on main, you can apply them directly:
git stash apply abc1234Or create a branch from the stash commit to inspect it:
git checkout -b recovered-stash abc1234If only some stashes are corrupted, you can delete them individually using reflog:
git reflog delete --rewrite stash@{0}After each deletion, run git stash list to see the updated list. The indices will shift after each deletion.
If you cannot recover any stashes or don't need them, clear the entire stash:
git stash clearThis removes all stash entries and should resolve the invalid reference error. You can then create new stashes normally.
If git stash clear doesn't work, manually remove the corrupted files:
# Backup first (optional)
cp -r .git/refs/stash .git/refs/stash.backup 2>/dev/null
cp -r .git/logs/refs/stash .git/logs/refs/stash.backup 2>/dev/null
# Remove corrupted files
rm -f .git/refs/stash
rm -f .git/refs/stash.lock
rm -rf .git/logs/refs/stash
rm -f .git/logs/refs/stash.lockAfter removing these files, Git will recreate them when you next run git stash. Note that this will delete any existing stashes.
Cloud sync services can create duplicate files. Check for files like:
ls -la .git/refs/stash*
ls -la .git/logs/refs/stash*If you see files like stash (1) or stash (conflict), remove them:
rm '.git/refs/stash (1)'
rm '.git/logs/refs/stash (1)'The stash mechanism in Git uses a combination of a direct reference (.git/refs/stash) pointing to the most recent stash commit, and a reflog (.git/logs/refs/stash) that maintains the history of all stash entries. When either of these becomes corrupted, stash operations fail.
If you're using git filter-branch and want to preserve stashes, consider exporting them to patches first:
git stash show -p stash@{0} > stash-0.patchFor Windows users experiencing persistent shell escaping issues, consider using Git Bash instead of cmd.exe or PowerShell, or use Git's built-in stash index syntax with --index.
In CI/CD environments using tools like lint-staged that interact with stash, ensure you're using a recent Git version (1.8.5.5+) and that the CI environment properly handles shell escaping. Some tools provide workarounds or configuration options for stash-related issues.
To prevent stash corruption, avoid interrupting stash operations, ensure stable filesystem access (especially with network drives or cloud sync), and consider using branches instead of stash for important work-in-progress changes.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git