How to recover files from a damaged git working folder

I had a bad case of getting BSOD when switching the Git branch, which I did not click on my remote repository. After rebooting the computer and logging in, I found out that my workspace is corrupted. Here are the symptoms:

  • Branch name: "(...)"
  • there is a .git directory with standard files and directories (hooks, info, logs, objects, refs)
  • the all Git command I made, besides clone and init, resulted in "fatal: not the Git repository (or any of the parent directories): git"
  • "$ Git init" did not return any error but did not fix the problem.
  • "$ Git fsck --full" resulted in "fatal: not the Git repository (or any of the parent directories) :. git"
  • Only some of the directories in the workspace show TortoiseGit icons indicating no changes, others have no icons, all files have no icons

Can someone help me bring the workspace back to working condition or restore some of the files to branches or stash?

+6
source share
3 answers

With the great help of my former colleague, I figured out how to recover from this. You will probably want to do this as a last resort, because you are likely to lose some information, and it may not work because of corruption.

Name the damaged workspace “corruptWorkspace” and the workspace that needs to be fixed to fixWorkspace. The first step you need to do is create a new workspace to recover and copy the object and refs:

  • $ mkdir fixWorkspace
  • $ cd fixWorkspace
  • $ git init
  • $ cp ../corruptWorkspace / .git / objects.git -r -a
  • $ cp ../corruptWorkspace / .git / refs.git -r -a

From here you can restore the branch / commit.

  1. Find the branch you want to restore by finding it in .git \ refs \ heads or in the .git \ logs \ HEAD file
  2. Open a text editor and you will find the last SHA commit for this 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 latest commit changes

  1. $ git show [commit SHA]

After confirming that the branch looks ok, try checking

  1. $ git checkout [branch name]

Then you can reset the branch

  1. $ git reset --hard

At this point, you have the latest perfect version of your branch. The next step is to restore the stash file.

  1. Find the backup you want to restore 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 SHA commit for this cache in the stash file or the second SHA column of your last stash entry for the branch in the stash file

List the files that are in the backup storage for recovery, from here you can get the location and file that were hidden for use to restore the file

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

Recover Saved Files

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

After you have completed the above command for all your files with files, you have finished receiving your branch and your file with files. if the configuration file is not corrupted, you can even copy the definition of origin and click on your changes.

Luck

+8
source

I have the same problem with one of my local repositories this morning. I have never seen this in all the years that I have used Git. I have been using this particular repo for 4 months, but now it tells me that it is not a git repository when there are all directories and files (including .git). I had some problems with my machine (Windows 7), closing at night for no apparent reason that could cause some corruption, as 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 that didn't help. I updated git to version 1.9.5, without help. The local repo still has local settings (git config --local -l). Fortunately, I commit and often push my branches, so recovery was as easy as re-cloning, but it still leaves me scratching. I renamed the existing local repo to another, and then re-cloned the remote repository, and now everything is fine with the new clone (the old one is still dead).

+1
source

My computer turned off due to a power failure during the check from one branch to another, when I turned it on, I found that my repository was corrupted. I cloned the repository to a new folder "new clone" From "Roy B" answer I replaced the folders of the "new clone" with a "damaged git folder" that contains branches and other information

  • ../corruptWorkspace/.git/objects
  • ../corruptWorkspace/..git/refs as well
  • .. \ corruptWorkspace.git \ logs

Repository restored

0
source

Source: https://habr.com/ru/post/980615/


All Articles