How to fix GIT object file is empty error


You might have encountered this weird git object file is empty  error when your machine/laptop ran out of power while you are working with git, boom 💣

In this case, you can recover your repository simply by cloning new from remote repository if you don’t have much changes locally. Else, you can follow below mentioned procedure to recover your local repository.

If you try to commit your local/stated changes.

When repository is corrupted you would see error something like below when you try to commit changes

Take a backup of directory .git

$ cp  -a  .git .git-old

Run, git fsck to get corrupted files/objects. i.e, git fsck –full

Now, our job is remove all empty files reported by git fsck

Once you delete all empty files reported by git fsck. git fsck will start showing up missing blob.

If you are lucky enough your problem will be solved with the following step.

NOTE: dangling blobs shown by git fsck are not error

If you delete all empty files, then run git fsck. If it is reporting “missing blob” then look checkout git reflog. It should show “fatal: bad object HEAD”, else probably your problem will be solved with just git reset

$ git reset

If your problem is not solved and git reflog is showing up “fatal: bad object HEAD”. Run the following command to fix broken HEAD

Now, update the HEAD with parent of last commit

Now, run git fsck. If you see error something like invalid sha1 pointer in cache-tree . You can fix it by removing .git/index file and resetting repo using git reset

Check the status and then commit if you required and problem is solved.





3 thoughts on “How to fix GIT object file is empty error”

Leave a Reply

Your email address will not be published. Required fields are marked *