How to fix corrupted git objects

Table of Contents

Tags:

If you ever encountered the following issue, keep reading to fix it:

1$ git status
2error: object file .git/objects/42/123456789abcde123456789abcde is empty
3error: object file .git/objects/42/123456789abcde123456789abcde is empty
4fatal: loose object 123456789abcde123456789abcde (stored in .git/objects/42/123456789abcde123456789abcde) is corrupt
5$ git pull
6error: object file .git/objects/42/123456789abcde123456789abcde is empty
7fatal: loose object 123456789abcde123456789abcde (stored in .git/objects/42/123456789abcde123456789abcde) is corrupt
  1. Navigate to the root of your git repo directory (the root should contain a .git folder, check ls -la and look for the .git folder at the top): ls-la

  2. Run the following command:

    1find .git/objects -size 0 -delete
    

    find looks for all files in the directory .git/objects with size 0 and deletes them

  3. Run git pull and check if the output is correct. If it is, you removed all corrupted objects and therefore fixed the issue.

Tip

If this error keeps happening, consider making a backup of the files in your repo without the .git directory and clone a clean repo from the remote. After that replace the contents with your backup and commit + push the changes to the remote.