6

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?

Roy B
  • 569
  • 5
  • 11
  • I did not read this, but may be it will give you some help http://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery – d.k Jan 06 '15 at 20:11
  • Did I understand you correctly, that you use Windows, some sort of bash on Windows, and none of the commands (except `git init`) did not work correctly, though you have `.git` folder in you project's folder? – d.k Jan 06 '15 at 20:15
  • That is correct, I'm running on Windows7, downloaded and installed the git client binaries (which has a bash emulator) and also a couple of GUIs (TortoiseGit and Git Extensions). Yes there is a .git – Roy B Jan 06 '15 at 22:36
  • sorry, I have no idea how to deal with this. I've never heard about anything similar. As a last resort I would suggest you to find some information similar to the link I posted above and try to do something with the stuff remaining in your .git forlder – d.k Jan 06 '15 at 23:44
  • Did you try to clone your current local (and damaged) repo, just to check if the new local clone would exhibit the same errors? – VonC Jan 07 '15 at 07:45
  • yes we did, it did not recognize the source as a git repo – Roy B Jan 09 '15 at 19:56

3 Answers3

9

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

Roy B
  • 569
  • 5
  • 11
  • Another potential cause is that your index file is corrupted. The solution for that is [here](http://stackoverflow.com/questions/1115854/index-file-corrupt) – Roy B Jan 13 '15 at 00:51
  • Thanks a lot Roy B, I could recover all my feature branches. – Parag Jul 17 '15 at 05:16
1

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).

afenkner
  • 458
  • 1
  • 7
  • 10
1

My Computer shut down due to power failure during checking out from one branch to another when i power it on i found my repository was corrupted. I cloned repository into new folder "new clone" From "Roy B" answer I have replaced folders of "new clone" with "corrupt git folder" that contains branches and other information

  • ../corruptWorkspace/.git/objects
  • ../corruptWorkspace/..git/refs and also
  • ..\corruptWorkspace.git\logs

Repository restored

Maaz Anzar
  • 131
  • 1
  • 8