10

Git pull command is working fine, but git stash is giving the following error:

$git stash
error: unable to resolve reference refs/stash: No such file or directory
fatal: Cannot lock the ref 'refs/stash'.
Cannot save the current status

How do i resolve this so that i don't loose my changes?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
skadoosh
  • 471
  • 1
  • 6
  • 15
  • Does your user have write permissions in the `.git`/`.git/refs` directory? – knittl Aug 28 '13 at 05:48
  • 1
    What OS and what git version are you using? – VonC Aug 28 '13 at 05:48
  • git version 1.7.9.5, Linux OS. .git and .git/refs have read and execute permissions – skadoosh Aug 28 '13 at 06:26
  • 1
    @schipitch: You must be able to write to the directory, otherwise you cannot create files – knittl Aug 28 '13 at 06:59
  • @knittl i have been using git for quite some time, and it has been working with the same permissions, it is only today that this error occurred. – skadoosh Aug 28 '13 at 09:45
  • I deleted the stash file in .git/logs/refs and .git/refs, now it is working fine. Thank you. – skadoosh Aug 28 '13 at 10:16
  • @schipitch: Maybe you have issued a git command as another user (e.g. root). This could result in wrong permissions for your user. – knittl Aug 28 '13 at 10:40

3 Answers3

22

I was having a very similar issue, after a corrupted filesystem.

The solution was to remove the file or directory at .git/logs/refs/stash

After deleting the file or directory, please use git stash to create a new ref, and that's all.

This will delete any stash you might have, so please be careful.

git stash
error: refs/stash does not point to a valid object!
rm -r .git/logs/refs/stash
git stash
Saved working directory and index state WIP on master: e1d74d8 fixing get_thumbnail_list
Dangas56
  • 849
  • 1
  • 8
  • 32
Neoecos
  • 569
  • 4
  • 16
19

I had to delete following file also to get it working.

rm -f .git/refs/stash
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Eržika Iťák
  • 191
  • 1
  • 3
  • 2
    Computer crashed during git stash operation and seemingly I lost all my stashes. TL;DR: deleting only this bad `.git/refs/stash` allowed me to recover my stash list. In case it helps anyone else: My stash seemed ok in `.git/logs/refs/stash` (so didn't want to delete this). Followed first step of: https://dev.to/meduzen/recover-a-lost-git-stash-in-two-steps-569 where the `git fsck` command reported: `error: refs/stash: invalid sha1 pointer 0000000000000000000000000000000000000000 error: bad ref for refs/stash` but stopped here and did another search to find this SO answer :D. – ErrCode Dec 30 '19 at 02:49
  • Didn't work fully - seems like all it did was allow `git stash list` to return, but trying to run `git stash apply` doesn't work at all. `warning: Log for ref refs/stash unexpectedly ended on Tue, 17 Dec 2019 22:27:25 +0900.` Guess I have to abandon this repo and start again. Other git operations don't seem to work properly either. – ErrCode Dec 30 '19 at 03:14
  • I had to delete `.git/refs/stash.lock` as well. After that `git stash` started to work well. – Andrii Tykhonov Apr 13 '21 at 17:35
2

You can remove the stash and add new stash folder if the problem still remained after remove the stash:

rm -r .git/logs/refs/stash

mkdir .git/logs/refs/stash

ninjahoahong
  • 2,624
  • 22
  • 20