Skip to content

goodguydaniel.com

Deleted my git stash! Please send help!

Git1 min read

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

source: https://me.me/i/please-send-help-none-c0081e69a4f4419a824e82a234ac09e6

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

1# if you're just flashing by don't copy and past this line into your terminal
2for 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:

1git fsck --unreachable

or maybe

1git 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:

1git 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. If you're looking for a more in depth look into Git, I would recommend Pro Git by Ben Straub and Scott Chacon.

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

Have a nice day.