Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Not a git repository" after OS reset

Tags:

git

windows

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)

like image 715
usr-local-ΕΨΗΕΛΩΝ Avatar asked Dec 01 '16 09:12

usr-local-ΕΨΗΕΛΩΝ


2 Answers

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.

like image 199
torek Avatar answered Oct 05 '22 11:10

torek


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.
like image 23
Raheel Hasan Avatar answered Oct 05 '22 13:10

Raheel Hasan