Sub topic 13
Loose Objects and Packfiles
This chapter assumes you can run git init, git add, and git commit, that you are comfortable in a Unix-style shell, and that you know what a hash function is. It also assumes you have seen the object format from earlier chapters: a header, a size, and zlib-compressed content stored under .git/objects.
Loose objects
A loose object is one object stored in its own file. Git writes objects this way by default. The first two hex characters of the ID become a directory name, and the remaining characters become the file name.
Each loose object is compressed alone. If you change one line in a large file, Git stores a second complete compressed copy of that file. Loose storage is simple and fast to write, but it wastes space when many versions of similar files exist. A repository with thousands of loose objects also means thousands of small files.
Packfiles
A packfile is a single file that holds many objects. Git can store an object in a pack as a delta, which means only the differences from another, similar object. The pack stores a base object in full, and later versions are stored as instructions for rebuilding them from that base.
Each pack has an index file, with the .idx extension, beside it. The index maps object IDs to byte offsets in the .pack file, so Git can find one object without reading the whole pack.
Packing changes only how objects are stored. Object IDs stay the same, because an ID is computed from the header and content, not from the storage form.
The following commands create several versions of a file, then pack them:
git init packdemo && cd packdemo
git config user.name "Demo User"
git config user.email "[email protected]"
seq 1 2000 > numbers.txt
git add numbers.txt && git commit -m "v1"
for i in 1 2 3; do
echo "extra line $i" >> numbers.txt
git commit -am "v$((i + 1))"
done
git count-objects -v
git gc
git count-objects -v
find .git/objects/pack -type fBefore git gc, the count: line shows loose objects and in-pack: is 0. After it, count: drops to 0 and in-pack: holds the objects. The find command lists a .pack file and a matching .idx file. Because the four versions of numbers.txt are nearly identical, the pack stores most of them as small deltas.
You can look inside a pack with git verify-pack:
git verify-pack -v .git/objects/pack/pack-*.idxEach line shows an object ID, its type, its size, its packed size, and its offset. Deltified objects have two extra columns: the delta depth and the ID of the base object.
When Git packs
You rarely run git gc yourself. Git runs git gc --auto after commands such as git commit and git merge, and it packs when the number of loose objects passes a threshold (about 6,700 by default). Fetching and pushing also send objects in packs, so a clone usually arrives already packed.
Common errors
- Looking for an object file that is gone. After
git gc,find .git/objectsno longer shows a file for an object you know exists. The object is in a pack. Usegit cat-file -t <id>to confirm it exists, becausecat-filereads both forms. - Assuming packing changes IDs. It does not. If an ID changed, the content or header changed.
- Deleting loose files by hand. Removing files from
.git/objectscorrupts the repository unless the objects are already safely in a pack. Letgit gcdo it.
Exercise
After running git gc, git count-objects -v shows count: 0, yet git cat-file -p HEAD still prints your latest commit. Where is that commit stored, and why do its ID and contents not change?
Show answer
The commit is stored in a packfile under .git/objects/pack, and the .idx file tells Git where to find it. Its ID is a hash of the object header and content, not of how it is stored, so packing does not change the ID or the contents.
Exercise
Why can a pack be much smaller than the same objects stored loose?
Show answer
A pack can store similar objects as deltas against a base object, so repeated content is not stored again in full. Loose objects are each compressed alone, so every version of a file takes its own full copy.