Sub topic 13
Loose Objects and Packfiles
This chapter assumes you can run basic Git commands and use a Unix shell. It also assumes you have followed the earlier chapters: you know what blobs, trees and commits are, and you have a repository with a few objects in it, such as the plumbing-demo repository built earlier.
Two ways to store objects
Git can store an object in one of two forms:
- A loose object is a single file under
.git/objects/. The first two hex digits of the hash name the subdirectory, and the other 38 digits name the file. The file holds the zlib-compressed object. - A packfile is one file holding many objects. Git stores many objects as deltas, meaning "this object is the same as that one, except for these changes". A matching pack index (
.idx) file lets Git find an object inside the pack quickly.
Every object you have made with hash-object, write-tree and commit-tree started as a loose object. That is cheap to write, but wasteful when files change a little between versions.
Counting objects
git count-objects -v reports how many objects are loose and how many are packed.
git count-objects -vThe fields to watch:
| Field | Meaning |
|---|---|
| `count` | Number of loose objects |
| `size` | Disk space used by loose objects, in KiB |
| `in-pack` | Number of objects stored in packs |
| `packs` | Number of packfiles |
| `garbage` | Files in the object directory that Git does not recognize |
Packing with git gc
git gc (garbage collection) packs loose objects and tidies the repository. Run it in the demo repository and compare the counts.
git count-objects -v
git gc
git count-objects -v
git verify-pack -v .git/objects/pack/pack-*.idx
git fsck --unreachableAfter git gc, count should drop and in-pack should rise. The loose files under .git/objects/ are gone, and .git/objects/pack/ now holds a .pack file and a .idx file.
git verify-pack -v prints one line per packed object: its hash, type, size, size in the pack, and offset. Objects stored as deltas also show a delta depth and the hash of the base object they build on. In a tiny demo repository you may see no deltas, because delta compression pays off only when similar objects exist.
If you run verify-pack before packing, the glob pack-*.idx matches nothing and the command fails. Run git gc first.
Packed objects are still the same objects
Packing changes storage, not identity. The hash of an object is computed from its content, so it is the same whether the object is loose or packed. Every plumbing command you have used keeps working:
git cat-file -p HEAD
git cat-file -t "$id"git cat-file looks in both places and gives the same output. The difference shows only when you look at the files on disk. For example, find .git/objects -type f now lists a few pack files instead of one file per object.
Common errors
find .git/objects/??finds nothing after gc. This is expected. The objects moved into the pack.git verify-packreports "No such file". The pack-name glob matched nothing because no pack exists yet.- Objects you expected to see are missing after
git gc. An object that no ref can reach may be pruned. Git keeps unreachable loose objects for a grace period (two weeks by default) before deleting them. The next chapter covers reachability andgit fsckin more detail. - Pack files differ between two clones. The pack file's name and layout can differ even when both repositories contain the same objects. Compare object hashes, not pack file names.
Exercise
You run git count-objects -v and see count: 12 and in-pack: 0. After git gc, what should change, and does the hash of the first commit change?
Show answer
count should fall (usually to 0) and in-pack should rise to include those objects, with packs becoming at least 1. The commit hash does not change, because a hash is computed from the object's content and not from how the object is stored.
Exercise
Which command lists every object in a packfile along with its type and size, and why does it need the .idx file rather than the .pack file?
Show answer
git verify-pack -v .git/objects/pack/pack-*.idx. The index file records where each object starts in the pack, and the command reads the index to locate and check each object in the matching .pack file.