2

My computer crashed and now I encounter problems with my local git repository.

git status 

result in

shows fatal: Not a git repository (or any of the parent directories): .git

I am in the correct repository and I can also see the .git folder which contains the following files / folders.

enter image description here

I tried

git init 

which resulted in

Reinitialized existing Git repository in c:/dev/myproject/.git/

However it still does not work (git status result in the error message mentioned above).

Is there any way to repair my local repository? I have some work in there which I haven't pushed yet and will lose otherwise.

Paul
  • 547
  • 4
  • 13
  • 30
  • 2
    Why not just clone again to another folder and diff tool to move your changes in? Or even just overwrite the new clone files with your working folder files, using copy command. – Caius Jard Oct 13 '19 at 14:02
  • Thanks, but the moment my repository crashed I wasn't on the branch where my chenges reside in. – Paul Oct 13 '19 at 15:51
  • 1
    Is `git fsck` working? – sbat Oct 13 '19 at 18:44
  • 1
    Back up your broken repo before you start messing with it – Caius Jard Oct 13 '19 at 19:36
  • 1
    The image shows a file named `HEAD` is present, but check to see if its contents are valid. (Incidentally, using images in questions is something of an anti-pattern: see, e.g., https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – torek Oct 13 '19 at 20:01
  • @sbat: No, it didn't. Did also result in "shows fatal: Not a git repository (or any of the parent directories): .git" – Paul Oct 14 '19 at 05:46
  • @torek. Many thanks. The file head looked something like NUL|NUL|NUL... I copied ref: refs/heads/master in it and it was working again. – Paul Oct 14 '19 at 05:49
  • Link (accepted answer is not the one for here, but there is one in there that matches): https://stackoverflow.com/q/4630704/1256452 – torek Oct 14 '19 at 05:58

2 Answers2

2

In the .git folder, there should be a file HEAD. In that file, type

ref: refs/heads/master

If the file does not exist, you can create it

Bar
  • 56
  • 4
1

The problem was a corrupt file HEAD in the .git folder. After editing it so it contains a reference to a valid branch like this

refs/heads/master

the repository is working again.

Paul
  • 547
  • 4
  • 13
  • 30