gitatlassian-sourcetree

Retrieve deleted stash in Git using SourceTree


I am using source tree. I had created a stash of multiple changes and by mistake deleted it. Is there a way to retrieve them back?


Solution

  • The stash is saved internally as a merge commit referenced from a list of stashes.

    git fsck can find dangling objects. It will find not just your deleted stash, but probably other stuff, too... so you'll be wanting to look for commits that look like they could be your stash (git show <ID> to display relevant info about an object and decide whether it's the one you're looking for).

    Once you have that, all you need to do is re-insert it into the list of stashes. The list is stored in .git/logs/refs/stash and a line has the following format:

    <ID of previous stash commit in list or 0000000000000000000000000000000000000000 if none> <ID of merge commit> Your Name <your@email.example> <UNIX timestamp> <time zone, e.g. +0000><TAB char><description of stash>
    

    Here's a working example:

    16b9a2d400dafe7ea25592029e3e5582d025c7d8 5def7605dfe625e8b3a3152fe52a87cc36694b6a Jan Krüger <email.censored@invalid> 1374227992 +0200  WIP on master: 0dbd812 Update draft release notes to 1.8.4
    

    Just synthesize a line for the stash you want to re-insert (the name/mail/timestamp/description don't have to be accurate) and you should be able to use it normally again.

    Happy hunting!