How to fix Git fatal index file smaller than expected

Recently, I encountered this git error “fatal: index file smaller than expected” when my local workstation stopped working and did forced reboot. When I tried to preview the changes I did, I got this message:


$ git status
fatal: index file smaller than expected

 

To fix this without losing our local changes, we need to remove the corrupted index and reset back to the HEAD using this two commands:


$ rm .git/index
$ git reset HEAD .

 

After running those commands, everything is fixed.


$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   resources/views/header.php
        modified:   resources/views/footer.php

no changes added to commit (use "git add" and/or "git commit -a")