0

I am using Git for a project. Up to yesterday night, the repository was up and running and I shut my computer down by the end of the day as I always do.

This morning, when trying to run git status from my project folder as I always do, I get this error: fatal: not a git repository (or any of the parent directories): .git

I know that nothing happened between the moment I shut down my computer and boot it again this morning, so I'm confused about why the repository would vanish like that. It's worth nothing that the .git folder and .gitignore file are still present in the project folder.

Now my problem here is that even if I could do a fresh checkout of the repository in a new folder, I still have code that I stashed yesterday (using git stash), including a script of SQL queries that took me several hours to write and other pieces of code I don't even exactly remember what it is because I use stashing a lot to store unfinished work that's not yet ready to be committed.

I searched the Internet and specifically this place and found no one having the same issue, which makes it even more strange.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Gabriel S.
  • 1,961
  • 2
  • 20
  • 30
  • 1
    You're in the .git dir it seems. Do cd .. Then git status – Andrew T Finnell Nov 09 '12 at 10:52
  • I'm already in my repository folder (i.e. `.git/..`) – Gabriel S. Nov 09 '12 at 11:08
  • If you are willing to share a zip of the whole directory I'd take a look. Based on the information you have provided I am not sure I could be of any more help. The stashed code is stored in your repo, it's just a matter of manually getting to it or fixing the .git dir. – Andrew T Finnell Nov 09 '12 at 12:27
  • Thanks for the proposition, but it's job-related and confidential code, so I'll pass. I've resolved to rewriting those lost changes and be less silly about having backups ! – Gabriel S. Nov 09 '12 at 12:46
  • Side question: did you have a remote that you had pushed recent work to that would at least give a partial backup. – Philip Oakley Nov 12 '12 at 14:27

4 Answers4

2

If you still have the \.git sub directory, then you have a chance. There is probably one entry that has been corrupted. I can't remember what specific contents that Git checks for to confirm if the folder is a proper repo, but these things can usually be recovered with a little perseverance.

Take a back up first. Then take another using an alternate method just in case (.git usually being a hidden file)

What is you .git directory structure - does it have all the expected content?

Did you have a remote that you'd push most of your content to to look for comparisons?

Update: see The presence of .git/HEAD is the secondary check for a git repository

Community
  • 1
  • 1
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
1

Obligatory question: Do you have a backup?

If not: bad.

If yes: Restore the backup of the repository to a different directory, check out the latest branch / commit you know you were working on, if there's a stash in that backup as well, commit it to a new branch. Then overwrite the source files in that branch with the source files of your damaged repository. This gives you the difference of the last undamaged, backed up state and whatever you did in the meantime. You can stash it as well.

For this very reason I normally commit early, often and push to an external repository as backup.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Unfortunately, I have no backup. All I have is my project folder in the same state file-wise as when the git repo was working yesterday. But it's just not a repo anymore... So to make it clear, I lost no code, apart from what's in the stashes (unless there's a way to retrieve that). – Gabriel S. Nov 09 '12 at 11:11
  • @Gaël: A repository doesn't replace a backup. Having your SCM repository lost is almost as bad as loosing working tree code! Take this as a lession and always keep backups. Especially with a tool like Git, where it is so simple to push the whole tree into a remote location. – datenwolf Nov 09 '12 at 12:23
  • @Gaël: I need to reemphase this *in some situations loosing your SCM is even worse than loosing your working tree*. For example when developing medical application software the track of development records is even more important, than the one specfic version your might have lingering around. – datenwolf Nov 09 '12 at 12:52
1

I have the same problem, but what happened is my PC is turned off unexpectedly while doing git merge.

Apparently, the issue is that the HEAD file is corrupted after the shut down. The solution is to check .git/HEAD file and replace the content with

ref: refs/heads/develop

Note that develop is my current branch before my PC shuts down. Also, this might not work on all cases, but worth a try.

onyotzki
  • 23
  • 5
  • I'm not sure what caused my issue but sure enough, the HEAD file was blank. I added `ref: refs/heads/master` and that fixed it for me. Though, now I'm concerned that some other files are corrupted and I'm just not aware of it. Thanks!! – Chris Leyva Dec 27 '18 at 15:39
0

To fix this problem just move into your ".git/HEAD file", you will notice it is empty. It is not been referenced to the main/master. So inside of your .git/HEAD just type this "ref: refs/heads/master". Then you can go back to the parent directory.

Theo
  • 3
  • 1
  • This is a duplicate of an of existing answer, by @onyotzki. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers. Remember to review all existing answers first. – tjheslin1 Apr 12 '22 at 20:33