Home > OS >  How to repair corrupt git repository - fatal: Not a git repository?
How to repair corrupt git repository - fatal: Not a git repository?

Time:02-11

I looked at many other questions to no avail.

My laptop froze during git reset --hard or git checkout branch. Not sure. After the hard reset I see

$ git fsck
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

I saw that .git/index was an empty file. Deleting it yields no results.

Any ideas how to restore? I have most my stuff pushed to a remote but I prefer to keep my branches and stashes. I have some useful stuff in there and even if I can track most of it remotely, would be hard.

CodePudding user response:

I figured it out. Also my .git/HEAD file was empty. I've put inside it

ref: refs/heads/master

Then I've run (be careful with these, in case you had uncommitted changes, you can copy your modified files safely somewhere else first).

git reset --hard
git clean -f

I found out what the issue is by running strace git status and noticed that it tries to find HEAD file in the directory path. Then it was easy.

P.S. after the fact I see that my branch where I tried to do git reset --hard another_branch is missing. So apparently issue happened while doing reset.

  •  Tags:  
  • git
  • Related