Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover files from a corrupted git working folder

Tags:

git

corruption

I had the unfortunate incident of getting a BSOD while switching my Git branch that I hadn't pushed to my remote repository. After the computer rebooted and logged back in, I found out that my workspace is corrupted. Here are the symptoms:

  • The branch name is "(...)"
  • the .git directory exists with the standard files and directories (hooks, info, logs, objects, refs)
  • all git command that I did except clone and init resulted in "fatal: Not a git repository (or any of the parent directories): .git"
  • "$ git init" did not throw any error, but did not fix the problem
  • "$ git fsck --full" resulted in "fatal: Not a git repository (or any of the parent directories): .git"
  • Only some of the directories in the workspace show TortoiseGit icons indicating no change, others do not have the icons, all files do not have the icons

Can someone help me get the workspace to a working state again or recover some of the files in the branch or stash?

like image 410
Roy B Avatar asked Jan 06 '15 20:01

Roy B


2 Answers

I am having the same issue with one of my local repositories this morning. I have never seen this in all the years I've been using Git. I have been using this particular repo for 4 months but now it tells me it's not a git repository when all the directories and files (including .git) are there. I have had some issues with my machine (Windows 7) shutting down at night for no apparent reason which could have caused some corruption since I tend to leave Intellij open all the time. My git version is 1.9.4. I tried "git init" to reinitialize the local repository, but it didn't help. I updated Git to version 1.9.5, no help. The local repo still has my local settings (git config --local -l). Luckily I commit and push my branches often so recovery was as easy as re-cloning, but it still leaves me scratching. I renamed my existing local repo to something else then re-cloned the remote repository and all is good now with the new clone (the old one is still dead).

like image 27
afenkner Avatar answered Nov 15 '22 06:11

afenkner


With a lot of help from my former colleague, I figured out how to recover from this. You probably want to do this as the last resort because you'll probably lose some information and it is possible that this might not work because of corruption.

Let's name the corrupted workspace is "corruptWorkspace" and the workspace to be fixed on is "fixWorkspace" First step you need to do is create a new workspace to do your recovery and copy the object and refs:

  1. $ mkdir fixWorkspace
  2. $ cd fixWorkspace
  3. $ git init
  4. $ cp ../corruptWorkspace/.git/objects .git -r -a
  5. $ cp ../corruptWorkspace/.git/refs .git -r -a

From here you can recover a branch/commit.

  1. Find the branch you want to recover by finding it in .git\refs\heads or in .git\logs\HEAD file
  2. Open in a text editor and you will find the last commit SHA for that branch in the branch file or the second SHA column of your last record for the branch in the HEAD file

This command should be readable and show the last commit changes

  1. $ git show [commit SHA]

After confirming that the branch looks ok, try to check it out

  1. $ git checkout [branch name]

Then you can reset the branch

  1. $ git reset --hard

At this point you have the latest committed version of your branch. The next step is to recover the stash file.

  1. Find the stash you want to recover by finding it in .git\refs\stash or in .git\logs\stash file
  2. Open in a text editor and you will find the last commit SHA for that stash in the stash file or the second SHA column of your last stash record for the branch in the stash file

List the files that are in the stash for you to recover, from here you can get the location and file that were stashed to be used for restoring the file

  1. $ git show --name-only [stash SHA]

Recover the stashed files

  1. $ git show [stash SHA]:[full path of file] > [full path of file]

After you've done the above command for all your stashed files, you have finished getting your branch and your stashed file. if the config file is not corrupted, you might even be able to copy the "origin" definition and push your changes.

Good Luck

like image 172
Roy B Avatar answered Nov 15 '22 08:11

Roy B