0

I was managing a git repo, as I have done thousands of times in the past, using gitg. Suddenly, the app crashed (Segmentation fault (core dumped)) and the repo got corrupted. Now git status says

No commits yet

The files seem to be with the same content as the last commit I made before this got corrupted, so I could start over from here, but all the history would be lost. If I run git branch I get

fatal: Failed to resolve HEAD as a valid ref.

I have no idea how to further diagnose what is going on, nor solve it. Any help is very appreciated.

user171780
  • 2,243
  • 1
  • 20
  • 43
  • 1
    You could be saved by the distributed nature of Git. Do you have another copy of the repo somewhere? Like, do you have a local copy and there's also a remote copy? Are they both corrupted? If not, then you can just toss the corrupted one and reproduce it with the one that isn't. With Git, no one repo is any more special than another. If the healthy repo is behind, then you just have to update it with the latest changes. – CryptoFool Mar 07 '22 at 17:25
  • 1
    What is the output of `git show-ref`? You should not have any reason to worry. No crash during a git command can actually corrupt your repo. If you haven't done that already, I would recommend you create a copy of your `.git` folder before you continue with any restoration attempts. – Chronial Mar 07 '22 at 17:27
  • 1
    what branch is mentioned above the "No commits yet" message ? check `git reflog that_branch`. If the output is not empty, you can run `git reset ` (note : without the `--hard` option, to keep the changes in your files if you have any) where `` would be the hash of the first commit mentioned in that output, and you should have returned to your last valid state. – LeGEC Mar 07 '22 at 17:37
  • 1
    `du -sh .git` will tell you if you have some history or not – Ôrel Mar 07 '22 at 17:55
  • @CryptoFool my last push was several commits ago unfortunately. – user171780 Mar 07 '22 at 18:31
  • @Chronial `git show-ref` displays this: ```eaad45f9dc71b3d520ac84eed5c53d0c481a6efd refs/remotes/origin/HEAD eaad45f9dc71b3d520ac84eed5c53d0c481a6efd refs/remotes/origin/main 1ec3f33ff89d1d3debe76e7e1b95498cf4b6446c refs/tags/Windows_machine_that_died``` I have already created a backup copy of the whole repo just in case. – user171780 Mar 07 '22 at 18:34
  • @LeGEC I don't know, any command that requires me to input the name of a branch fails. With `git reflog that_branch` I get `fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.`. – user171780 Mar 07 '22 at 18:36
  • I have tried `git reset eaad45f9dc71b3d520ac84eed5c53d0c481a6efd` and getting `error: update_ref failed for ref 'HEAD': cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/main': reference broken`. – user171780 Mar 07 '22 at 19:04

1 Answers1

-1

After trying many different things, I was saved by this answer. If someone runs into a similar kind of issue, before doing anything, create a backup copy e.g. cp -r your_repo your_repo.backup and then start experimenting within your_repo. I had to recover from the backup several times before I was able to actually solve the problem.

user171780
  • 2,243
  • 1
  • 20
  • 43
  • I'm sorry, but this answer is not really helpful to any future visitors. It doesn't contain any explanation whatsoever what was required to solve the problem, only that you managed to solve it (but not how). – knittl Mar 07 '22 at 19:32
  • Should I copy paste the linked answer? – user171780 Mar 07 '22 at 19:39