2

My computer suddenly restarted just after I commited my last change, and when I was back it no longer could bring the git for my repo. I checked git files all of files is there but I get different errors when I use git bash commands.

(Opening repositories: D:\Projects\Engine\Engine Git failed with a fatal error. error: bad signature 0x00000000 fatal: index file corrupt)

I did this but it totally removed all of my history and put me back on just having to add a new source control git, but I want to be able to restore the history and not lose my history cause all old commit files are there and havent touched by anything for some time. How to resolve "Error: bad index – Fatal: index file corrupt" when using Git

rm -f .git/index
git reset

This deleted all of my history and started a new git. I had backups for my .git folder before I do this command so now it back to that error again.

.git folder

Visual Studio 2019

commits

After running command: ("git fsck") these are all errors: All git errors

(Please if possible for instant respond from me email me or message me in discord, cause I've tried a lot of things but they didnt work, then we can apply the final answer to the post. Thanks for helping me. shahroozleon01@gmail.com/SHAHROOZ#2185)

Shahrooz
  • 196
  • 1
  • 14
  • Does this answer your question? [How to resolve "Error: bad index – Fatal: index file corrupt" when using Git](https://stackoverflow.com/questions/1115854/how-to-resolve-error-bad-index-fatal-index-file-corrupt-when-using-git) – prakash sellathurai Sep 05 '21 at 11:23
  • @prakashsellathurai I've already said that it didnt work, it just deleted my git and made a new one, and I lost all of my git history and everything. – Shahrooz Sep 05 '21 at 11:29
  • Do other commands, which shouldn't depend on the index, work ? For example : can you inspect the history of your repo, with a GUI or `git log --oneline --graph` ? – LeGEC Sep 05 '21 at 11:41
  • @LeGEC Fatal: your current branch appears to be broken. – Shahrooz Sep 05 '21 at 11:43
  • Ok, so you have some other files which aren't ok. What is the cont of `cat .git/HEAD` ? – LeGEC Sep 05 '21 at 11:45
  • @LeGEC ref:/ refs/heads/master – Shahrooz Sep 05 '21 at 11:48
  • Ok, that's a regular content. To assess the state of that local clone : run `git fsck`. To assess how much work is at risk : do you have other clones of that repo ? – LeGEC Sep 05 '21 at 11:51
  • @LeGEC ok, dude can we chat it seems stackover is not happy with long comments. my discrod if you have anything lese let me know thanks SHAHROOZ#2185 – Shahrooz Sep 05 '21 at 11:53
  • @LeGEC it says this [link](https://cdn.discordapp.com/attachments/677604453171462214/884044654776049744/Screenshot_2021-09-05_162653.jpg) – Shahrooz Sep 05 '21 at 11:58

3 Answers3

3

Your master ref and its log, and the HEAD ref log, have been torched by an ill-timed restart, but it's not clear yet whether anything else got damaged. Fortunately, it's not so very hard to recover this stuff, because while Git might not be bulletproof, it is near-as-dammit unkillable with proper care. Sometimes you just have to play combat medic if you're going to take it in to a war zone.

So:

rm .git/refs/heads/master       # remove damaged ancillary pointers and logs
rm .git/logs/HEAD               # .
rm .git/logs/refs/heads/master  # .
rm .git/index                   # .
                                # find the recently-added stuff:
find .git/objects/?? -ctime -2 -type f -exec ls -t {} +|awk -F/ '{print $3$4}' \
| git cat-file --batch-check

to show you everything you added to the repo in the last couple days, most recent first. The most recent commit (the first one it lists) is very likely to be the one you made; with any luck you'll be able to

git reset -q $thatsha
git update-index --refresh
git checkout -b master

and you'll be back in business, but if that doesn't work post the text transcript of what happened instead and we can continue the learn-by-doing.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks for you help dude, it didnt report any error and it opened the git But unfortunately it didnt show up any commit or history, but all of my commit files are still in .git\objects folder, but the action was as same as deleting whole git and made anew. – Shahrooz Sep 05 '21 at 21:20
  • ff1a61877255a2f5c229eb1254923a78820f1d3f commit 173 6b8d27fc62cfa29078afd07ef05c33c696d3ab35 commit 173 a9f19d3c6830fb1d2c20b9259eff3b249bb667d6 commit 273 dbff4f1ab6c1747d17d6ad54d95c76f1049664eb tree 151 7a9bb915c095332b0f78eb71f7294f43f0ba3b05 tree 9802 ... shows this after the "find .git/objects..." command. – Shahrooz Sep 05 '21 at 21:23
  • So, `git checkout -b master ff1a61877255a2f5c229eb1254923a78820f1d3f` says what? – jthill Sep 05 '21 at 21:27
  • ```git checkout -b master ff1a61877255a2f5c229eb1254923a78820f1d3f``` says fatal reference is not a tree and ```git checkout -b master $thatsha``` says switched to a new branch 'master' but I ran checkout -b master ff1a/ after $thatsha should I run that first? – Shahrooz Sep 05 '21 at 21:33
  • Okay, `git cat-file -p ff1a618` should complain that the object is corrupt, the previous added commit is 6b8d27f, try `git checkout -B master 6b8d27fc62cfa29078afd07ef05c33c696d3ab35`, notice the caps b here I'm just overriding whatever's there now. – jthill Sep 05 '21 at 21:45
  • Did the find really list three commits one afer another? Don't be afraid to edit the complete readout (or say the first ten lines,pipe it through `head`) into your question body. – jthill Sep 05 '21 at 21:47
  • it didnt fit into a comment @jthill [link](https://cdn.discordapp.com/attachments/677604453171462214/884192943831932968/Screenshot_2021-09-06_021547.jpg) – Shahrooz Sep 05 '21 at 21:48
  • ```git checkout -B master 6b8d27fc62cfa29078afd07ef05c33c696d3ab35``` says reference is not a tree. – Shahrooz Sep 05 '21 at 21:51
  • Sometime in here all the old hands here are going to want to get you onto a toolkit that doesn't treat text like an alien creature to be kept behind glass. The text at that link isn't ***at all*** what you pasted in to the comment. Hunt up how to pipe command output to the clipboard, and for now try using 1d693533 for the sha since that's what your picture shows being listed first. – jthill Sep 05 '21 at 21:54
  • it says ```error: The following untracked working tree files would be overwritten by checkout: ``` almost half of my files then ```aborting``` – Shahrooz Sep 05 '21 at 21:59
  • and VS now again shows no old history, just the one I now have to create to show the master branch. its all gone again. btw I think it showed all of my files there not half of them. – Shahrooz Sep 05 '21 at 22:02
  • 1
    Okay, this is actually good. `git reset -q 1d6935ee; git update-index --refresh; git checkout -b master; git status` (yah I typoed transcribing from your picture before) and you're almost all the way home. Ignore what VS is telling you, it's a frontend that can't interpret what it's seeing, we'll deal with it later. – jthill Sep 05 '21 at 22:16
  • Oh gosh, I lost the the track can you please again tell me these step by step in which order I should run them i'm lost. sorry After first part of your post (```rm .git/refs/heads/master # remove damaged ancillary pointers and logs rm .git/logs/HEAD # . rm .git/logs/refs/heads/master # . rm .git/index # . # find the recently-added stuff: find .git/objects/?? -ctime -2 -type f -exec ls -t {} +|awk -F/ '{print $3$4}' \ | git cat-file --batch-check```) what next? – Shahrooz Sep 05 '21 at 22:22
  • Im doing it again on a untouched .git folder by any command but still like the first place corrupt. [link](https://cdn.discordapp.com/attachments/677604453171462214/884202453111803935/Screenshot_2021-09-06_025438.jpg). if you only could come and chat it would be much easier for me. – Shahrooz Sep 05 '21 at 22:26
  • 2
    All the noise and confusion here is due to you not showing the actual evidence you're looking at. Once you showed that, it was easy. The first commit it showed you was 1d6935ee, do the `git reset -q 1d6935ee` etc sequence above. – jthill Sep 05 '21 at 22:28
  • 1
    OMG it worked, thanks soooo much youre a legend bro. I reallllllly appreciate that. THAT WAS AWESOME. – Shahrooz Sep 05 '21 at 22:40
  • 1
    btw if you want to update your answer post do it then we accept your answer. Again I am REALLY grateful. – Shahrooz Sep 05 '21 at 22:42
  • 3
    Gotta say, I appreciate the kind words. Thanks back atcha. – jthill Sep 05 '21 at 22:42
0

Fortunately, Git's index / cache is largely a temporary data structure that can be rebuilt. (When you do this, you lose any staging work you've done, but usually that's minor.) The rm -f .git/index and git reset that you tried are the way to do this rebuild.

Unfortunately, you have significantly worse damage to your repository. In particular, the refs/heads/master file in your repository is corrupt. While you can remove that file, or put a valid hash ID in it (if you can find a valid commit hash ID), this may well leave various other problems. Ideally, you might find the correct last commit and put that hash ID into the file.

This may, or may not, still leave further problems, but at this point git reset can work (it can't until you fix the refs/heads/master issue) and then git fsck can get further along.

Your best bet by far is to recover everything from some other up-to-date clone, but if you don't have any other up-to-date clone, that option is lost. Depending on what other damage, if any, there is in this clone, it may or may not be possible to repair.

torek
  • 448,244
  • 59
  • 642
  • 775
0

SOLUTION (At least for me)

  1. Clone the project in a new folder (as a backup just to be on a safe side)
  2. Delete .git/index and .git/index.lock (if present)
  3. Run git reset --keep from the root of the project's repo in question

And voila!

Waleed93
  • 1,130
  • 2
  • 15
  • 24