These errors should not happen when changes to the Git repository are carried out using the Git client. From the documentation, see Object Storage to learn how Git stores its objects:
- Git constructs a header that starts with the type of the object
- Git concatenates the header and the original content and then calculates
the SHA-1 checksum of that new content
- Git compresses the new content with zlib
A corrupt loose object is any object whose state deviates from this design (See also What are the “loose objects” that the Git GUI refers to?).
Besides a system reboot or a dying battery, other reasons include
- using a buggy third-party implementation of Git
- manually "cloning" using rsync, scp, or some other external tool without data integrity checks
- copying and pasting in Windows
- disk failure
- sometimes just a blue moon could do it
If you're curious, try this contrived example:
$ git init bad-apple
Initialized empty Git repository in /tmp/bad-apple/.git/
$ cd bad-apple
$ touch README; git add .; git commit -m "Add a transitional fossil"
[master (root-commit) ca3ea96] Add a transitional fossil
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
# Get the commit object id
$ git rev-parse HEAD
ca3ea9630c0f01afc286589c4072db9c43262d63
# Git tries to help you not shoot yourself in the foot
$ chmod +w .git/objects/ca/3ea9630c0f01afc286589c4072db9c43262d63
# Empty out the commit object file and you broke it
$ truncate .git/objects/ca/3ea9630c0f01afc286589c4072db9c43262d63 --size 0
error: object file .git/objects/ca/3ea9630c0f01afc286589c4072db9c43262d63 is empty
error: object file .git/objects/ca/3ea9630c0f01afc286589c4072db9c43262d63 is empty
fatal: loose object ca3ea9630c0f01afc286589c4072db9c43262d63 (stored in .git/objects/ca/3ea9630c0f01afc286589c4072db9c43262d63) is corrupt