9

This morning my Windows 10 crashed and rebooted once I lifted my laptop's screen. No special activity was progressing, so I don't think there was significant disk activity.

However one of my main Git repos crashed after that reset. Here is what I tried:

  • $ git status fatal: Not a git repository (or any of the parent directories): .git
  • $ git init Reinitialized existing Git repository in ....../.git/
  • $ git status fatal: Not a git repository (or any of the parent directories): .git
  • Loop

I don't think I have unpushed commits, so wiping and cloning from remote should work.

Still, can I ask what to do to recover an existing Git repository (.git directory still exists, chkdsk reports OK) in such cases?

[Add] read this but did not apply to my case (I can't restore the repo)

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • 3
    First copy .git to somewhere else immediately. The entire repo even. Next re-install git, see if that works and use backup if necessary. – kabanus Dec 01 '16 at 09:59
  • Can you expand on "use backup"? Should I re-clone the repository and overwrite the .git folder from the backup? Upvoted for the word "immediately" – usr-local-ΕΨΗΕΛΩΝ Dec 01 '16 at 10:02
  • You should always keep copies of your repos. With git it's as easy as `git push`. You should have made a copy of the entire folder before trying `git init` or any other command to avoid causing additional damage. Copy the folder anyway, just in case your working files are OK. – Panagiotis Kanavos Dec 01 '16 at 10:03
  • probably what you might be interested in is this - http://stackoverflow.com/questions/2129214/how-to-backup-a-local-git-repository – Naman Dec 01 '16 at 10:03
  • I'm hoping after re-install your repo will work (`git status`), but if it doesn't and you delete the `.git` folder and init again, then just copy the backup `.git` over. – kabanus Dec 01 '16 at 10:04
  • 1
    btw from a small quick answer, every clone is a backup of the repo itself. http://stackoverflow.com/questions/5578270/fully-backup-a-git-repo – Naman Dec 01 '16 at 10:05
  • @kabanus what a great advice! I think **that** is the answer. I can see I restored all (?) my checked out branches. Git fsck reported an error indeed in a single branch where I had no work unpushed. Once I think I am done with that I could expand into a community answer – usr-local-ΕΨΗΕΛΩΝ Dec 01 '16 at 10:18

5 Answers5

26

As kabanus said in a comment, you should definitely save whatever you can before proceeding (and/or use some other existing clone as a backup).

When Git complains about this, though, it often means that the file .git/HEAD has gone missing. If you create a new HEAD file with contents: ref: refs/heads/master, Git may be able to recover everything.

Since HEAD is the most active file in the repository, it's the one most likely to be clobbered by an OS error or power failure. It's also a critical file when it comes to whether Git believes a .git directory is a repository: if the directory contains a file named HEAD (along with a few other key items), it is a repository; if not, it is not a repository.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • 2
    Endorsed. I had power-failure mid-way through branching in SmartGit on MS Windows 10. After restart, couldn't open the repo. Editing the HEAD fle just worked perfectly for me. – ianmayo Feb 14 '18 at 09:16
  • 1
    Same here - computer froze, HEAD was full of nulls - as was `REBASE_HEAD` and a couple other files inside the rebase-merge folder. Was even able to complete the rebase, using the non corrupted reflog after the repository was recognised again. – Mr_and_Mrs_D Jul 23 '18 at 11:14
  • In my case machine was accidently shutdown during a git process. And HEAD file was changed into `0000 0000...`. So, putting `ref: refs/heads/master` in `.git/HEAD` and `git reset --hard` then `git fetch --all` solved my problem. Thanks! – hmd Aug 14 '21 at 03:33
5

I had multiple branches corrupt due to OS error (bloody windows sleep function!!). So I had to manually do the following:

  1. .git/HEAD (set content to ref: refs/heads/master)
  2. $> git branch -v (this will tell you all the corrupt branches)
  3. .git/logs/HEAD (Read the file for last checksum of the commits and merge of corrupt branches)
  4. .git/refs/heads/{corrupt branch file} (change the checksum to the last working checksum from the log file.
  5. merge the branches again as per need.
Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70
1

In my case, the HEAD file was indeed corrupted due to a system crash.

I just cloned the repo again into a new folder, switched to the branch I was in, then replaced the .git folder with the one I just created from the clone.

From there, it was like nothing happened.

Will Strohl
  • 1,646
  • 2
  • 15
  • 32
0

Another workaround for this, Solved for me while OS crashed on GIT MERGE operation

  1. Get the working HEAD,FETCH_HEAD files under .git/ directory(of your project) from some other contributor
  2. Replace the existing HEAD,FETCH_HEAD files with new ones(taken from other contributor).
  3. Delete the INDEX under .git/ directory.
  4. Then Do a git pull.
Bond9
  • 1
  • 2
0

In my case, The ownership of the repo was mismatched after OS reset. I tried @raheel-hasan's instructions, after entering git branch -v command I got a suggestion.

enter image description here

git config --global --add safe.directory 'direactory path' this command solved the issue :)

(Use powershell to run this command)

You shoult reload your IDE or code editor after running this command.

Lojith Vinsuka
  • 906
  • 1
  • 10
  • 8