Search code examples
gitrecoveryblue-screen-of-death

recovering a git repo after a Windows blue screen?


I just got a blue screen, I have all my work of the last week or so in the stash, but now after that event it is gone... well, I can see the .git folder in explorer but doing git status or fsck it says it is not a git repo... here is a little play on powershell

PS C:\Users\tyoc\Documentos\wtf> git status fatal: Not a git repository (or any of the parent directories): .git PS C:\Users\tyoc\Documentos\wtf> cd .git PS C:\Users\tyoc\Documentos\wtf\.git> git status fatal: Not a git repository (or any of the parent directories): .git PS C:\Users\tyoc\Documentos\wtf\.git> cd .. PS C:\Users\tyoc\Documentos\wtf> git init Reinitialized existing Git repository in C:/Users/tyoc/Documentos/wtf/.git/ PS C:\Users\tyoc\Documentos\wtf> git status fatal: Not a git repository (or any of the parent directories): .git PS C:\Users\tyoc\Documentos\wtf> git fsck --no-reflog fatal: Not a git repository (or any of the parent directories): .git PS C:\Users\tyoc\Documentos\wtf> git stash fatal: Not a git repository (or any of the parent directories): .git

It will be nice to get the stash back, because by fortune the repo has a central repository, so all is OK there... but my work is not, how is like if I havent made anything for a week... :S.


I add this for clarification:

The state of the repo before the blue screen was that my work was on the stash (I have like 7 old stashes and this last was my work in the week), because my client requested me to modify something (so I make a stash of what I was working on) did the change and pushed... some seconds before I did restore the stash I got the blue screen, then after reebot, Im unable to do any git command even the .git dir is in there... (I dont know if it contains something or is only the structure).

So, the state of the files was a clean working copy (because the change requested at the moment) and my actual-real-work was/IS on the stash...

And well I was using sourcetree as front end... dont know if is the fault of sourcetree or the bluescreen at the end of the day.. but the thing it is that I lost my job in the stash...


Solution

  • I made a backup of my trashed git repo first, then I cloned the repo as suguested by @equivalent8 but did not replace it all, I only used this files inside .git:

    • config
    • FETCH_HEAD
    • HEAD
    • index
    • packed-refs
    • sourcetreeconfig

    That allowed sourcetree to be able to open and git status to work (ie. it was again a git repo).

    Then from https://stackoverflow.com/a/91795/682603 I modify the search a little like this

    git fsck --no-reflog | select-string 'dangling commit' | foreach { $bits = $_ -split ' '; echo $bits[2]; git show $bits[2]}

    SO that I can see each diff and inspected commit by commit, if it was a candidate I copied the hash that is above on the first lines, and advanced to the next mach pressing q.

    Then for each candidate I did do

    git diff 52abca5cba3ff37b4954fd2c2e12c867dceb9481 --color=never > 52abca.patch git diff 48f7110fa9d507226ce31593a0fc957e465d6c91 --color=never > 48f711.patch

    SO that I dont lost them again, and checked the candidates, my work was still there!!! (I think)

    But I can't apply them to check each one, I got error: unrecognized input each time I do git apply 48f711.patch, guess I will need to copy paste/delete each file by hand???