~/

Back

Deleted my git stash! Please send help!

#ops
Posted on 09, Dec, 2018

I have this poor habit you know… Whenever I accumulate a lot of git stashes in some repository I just get a little maniac and start cleaning everything.

please send help

I found it sometimes even funny to open the terminal and play around with some bash to delete the stashes, look:

Terminal window
# if you're just flashing by don't copy and past this line into your terminal
for i in {1..10}; do git stash drop; done

Well, it happens that today (a few hours ago actually) I just did this and regretted the moment I did because there was this huge feature that for some reason I had locally stashed and not committed
¯\_(ツ)_/¯.

Good news!, the commits are not actually gone until git runs garbage collection, so to check the list of commits that might still be rescued just go ahead and type:

Terminal window
git fsck --unreachable

or maybe

Terminal window
git fsck --unreachable | grep commit

because you actually need to check whether your hash is somewhere referenced as a unreachable commit.

Then all you need to do once you find your stash (if you don’t know the hash I’m afraid you will have to apply stashes until you find it) is just:

Terminal window
git stash apply <hash>

Here, some cool references on git stash:

Notice that the references above are from Atlassian, which I highly recommend when it comes to quick search into a specific Git topic.

And that’s it, hope this saves you just like it saved me.

Have a nice day.

Last modified on 12, Oct, 2024