How Git Stores Data: Packfiles, Garbage Collection, SHA-256, Reftable and Git 3.0
How Git keeps repositories small and fast: loose objects and packfiles with delta compression, pack indexes, git gc and when unreachable objects are really deleted, fsck, SHA-256 repositories, the reftable ref format, and what Git 3.0 changes by default.
kvi5hnuCode Sprout team member8 min readOn this page
The object model (article 48) says every version of every file is a complete object. Stored naively, that would be enormous. In practice, a repository with years of history is often smaller than its working tree, because Git packs objects together and stores similar ones as deltas. This article covers how that storage works, how garbage collection decides what to delete, and the two big format changes in progress: SHA-256 object IDs and the reftable format for refs, both planned as defaults in Git 3.0.
Loose objects and packfiles
New objects are written as loose objects: one zlib-compressed file each, under .git/objects/xx/. That's quick to write, but wasteful for thousands of objects. Packing combines them into a packfile:
git init -q trail-guide && cd trail-guide
for i in $(seq 1 20); do
seq 1 200 | sed "s/^$i\$/line $i changed/" > trails.txt # one line differs per version
git add .; git commit -q -m "Version $i"
done
echo "loose: $(git count-objects)"
git gc -q
git count-objects -v | grep -E '^(count|in-pack|packs|size-pack)'
ls .git/objects/pack
echo "blob versions stored as deltas: $(git verify-pack -v .git/objects/pack/*.idx | awk '$2 == "blob" && NF > 5' | wc -l) of 20"
loose: 60 objects, 240 kilobytes
count: 0
in-pack: 60
packs: 1
size-pack: 7
pack-7de28b614f6d6cff47307db002fcb2dd11f999fc.idx
pack-7de28b614f6d6cff47307db002fcb2dd11f999fc.pack
pack-7de28b614f6d6cff47307db002fcb2dd11f999fc.rev
blob versions stored as deltas: 19 of 20
- Sixty loose objects (20 commits, trees and blobs) became a single pack of about 7 KB.
- In a pack, an object can be stored as a delta: instructions for rebuilding it from another, similar object, its base. Here 19 of the 20 file versions are deltas, and only one is stored in full. Git chooses bases by similarity, not by history, so any two similar objects can share data.
- Each
.packhas an.idxindex for finding objects by ID, and a.revreverse index. Larger repositories also use a multi-pack index (multi-pack-index) across several packs, and reachability bitmaps that speed up clones and fetches. - Packs are also what goes over the network:
fetch,pushandclonesend a pack containing just what the other side is missing, and a bundle is a pack with a header (article 46).
The newest version is usually stored complete and older ones as deltas against it, since recent versions are read most often. git verify-pack -v shows each object's type, size, packed size and delta depth.
Garbage collection
git gc packs loose objects, combines packs, packs refs into packed-refs, expires old reflog entries, and deletes unreachable objects that are old enough. After commands such as commit, merge and fetch, Git runs git maintenance run --auto, which does nothing unless there are many loose objects (about gc.auto, 6700 by default) or packs. With the default geometric strategy (Git 2.54), it repacks incrementally. With maintenance.strategy=gc, it runs git gc --auto instead. With scheduled maintenance (article 44), most of this happens in the background instead.
When are objects really deleted?
An object is safe as long as it's reachable from a ref, or from a reflog entry. That's what makes recovery possible after a reset or branch deletion (article 26). The defaults:
| Setting | Default | Meaning |
|---|---|---|
gc.reflogExpire | 90 days | reflog entries for commits still reachable from the ref |
gc.reflogExpireUnreachable | 30 days | reflog entries for commits no longer on the branch, such as after a reset or rebase |
gc.pruneExpire | 2 weeks | how old unreachable loose objects must be before deletion |
So a commit you lost by resetting can normally be recovered for about 30 days. To delete unreachable data immediately, such as after removing a secret from history (article 47):
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "MAP_API_KEY=sk_live_4f9a2c" > config.env; git add .; git commit -q -m "Add config by mistake"
git reset -q --hard HEAD~1 # the commit is no longer on main
echo "objects: $(git count-objects -v | awk '/^count|^in-pack/ {n += $2} END {print n}')"
git gc -q
echo "after gc: $(git count-objects -v | awk '/^count|^in-pack/ {n += $2} END {print n}') (kept: the reflog still reaches it)"
git reflog expire --expire=now --all
git gc -q --prune=now
echo "after expiring reflogs and pruning: $(git count-objects -v | awk '/^count|^in-pack/ {n += $2} END {print n}')"
git cat-file -e HEAD@{1} 2>/dev/null || echo "the commit is gone"
objects: 6
after gc: 6 (kept: the reflog still reaches it)
after expiring reflogs and pruning: 3
the commit is gone
This only cleans your repository. Clones, forks and the server keep their own copies.
Checking integrity: fsck
git fsck verifies every object's hash and structure, and reports broken links and dangling objects: unreachable commits or blobs, which are normal after resets and are what git fsck --lost-found recovers (article 26). git fsck --full checks packed objects too, which it does by default. Servers run similar checks on everything pushed when receive.fsckObjects is set.
SHA-256 repositories
SHA-1, Git's original hash, is weakened: practical collision attacks exist, and since 2.13 Git uses a hardened SHA-1 that detects them. The long-term fix is SHA-256, with 64-character IDs:
git init -q --object-format=sha256 trail-guide-256 && cd trail-guide-256
git commit -q --allow-empty -m "Start project"
git rev-parse HEAD
git rev-parse --show-object-format
cd ..
git init -q --ref-format=reftable trail-guide-reftable && cd trail-guide-reftable
git commit -q --allow-empty -m "Start project"
git branch maps
git rev-parse --show-ref-format
ls .git/reftable | sed "s/-[0-9a-f]*\.ref$/-<random>.ref/" # table files: an update range plus a random suffix
git for-each-ref --format='%(refname)'
9c772d3ca93848523b8df3da82b8e07eff2dc1dce20e565196216f6ead18cf9f
sha256
reftable
0x000000000001-0x000000000003-<random>.ref
tables.list
refs/heads/main
refs/heads/maps
git init --object-format=sha256creates a SHA-256 repository. The hash is fixed at creation: an existing repository can't be converted in place, only rewritten into a new one.- SHA-1 and SHA-256 repositories can't exchange objects directly yet. Work on interoperability, where a repository keeps both IDs (
compatObjectFormat), is ongoing. - Hosting support for SHA-256 repositories is still limited. Check before creating one for a project that will be hosted.
Reftable: a new storage format for refs
The traditional files format stores refs as loose files plus packed-refs. It struggles with repositories that have hundreds of thousands of refs, can't update many refs atomically, and has problems on case-insensitive file systems, where Fix and fix collide. Reftable stores all refs and reflogs in compact, sorted binary tables, listed in tables.list:
git init --ref-format=reftablecreates a reftable repository, as above.git refs migrate --ref-format=reftableconverts an existing one, including reflogs since Git 2.48.- With reftable,
.git/HEADcontains a placeholder (ref: refs/heads/.invalid) kept for older tools, and the realHEADis in the tables. This is one more reason scripts should usegit rev-parseandgit for-each-refinstead of reading files.
Git 3.0
Git 3.0 is the next major version, and its planned changes have been announced in release notes since Git 2.47. Each can already be tried today:
| Change in Git 3.0 | Try it now |
|---|---|
| SHA-256 becomes the default for new repositories | git init --object-format=sha256 |
| reftable becomes the default ref format | git init --ref-format=reftable |
git init creates main instead of master as the first branch | git config --global init.defaultBranch main |
git whatchanged is removed. Use git log --raw | |
git pack-redundant is removed | |
| symbolic links used as symbolic refs stop being supported | |
| Rust becomes required to build Git, enabled by default since Git 2.55 |
Existing repositories keep working: the defaults apply to new repositories, and SHA-1 and files repositories remain supported.
What's current
- Git 2.51 announced that reftable and SHA-256 will become defaults in Git 3.0, and Git 2.52 that
mainwill be the default initial branch. git maintenanceuses geometric repacking by default since Git 2.54, which avoids rewriting all packs at once (article 44).git refs migrateconverts between ref formats, including reflogs since Git 2.48.
Common questions
Why is my .git folder smaller than my project's files?
Packfiles compress everything with zlib and store similar versions as deltas, so years of history often take less space than one checkout.
Does git gc delete my lost commits?
Not while a reflog entry reaches them: unreachable reflog entries expire after 30 days by default, and then unreachable objects older than two weeks are pruned. Run git reflog and recover anything you need before then.
Should I create new repositories with SHA-256 today? For local or self-hosted projects, it works well. For repositories on a hosting service, check that the service supports it first, because conversion later means rewriting the repository.
Will Git 3.0 break my repositories? No. The changes are to defaults for new repositories, and the removal of a few long-deprecated commands. Existing repositories keep their formats.
Summary
- Objects start loose.
git gcand maintenance pack them, with deltas against similar objects, indexes, multi-pack indexes and bitmaps. - Unreachable objects are deleted only after their reflog entries expire (30 or 90 days) and the prune delay (2 weeks) passes.
reflog expire --expire=now --allwithgc --prune=nowdeletes them immediately. git fsckverifies the database and finds dangling objects.- SHA-256 repositories (
--object-format=sha256) and reftable (--ref-format=reftable,git refs migrate) are available now. - Git 3.0 plans SHA-256, reftable and
mainas defaults, removeswhatchangedandpack-redundant, and requires Rust to build.
Practice questions
You reset away a commit by mistake three weeks ago. Is it still recoverable?
Answer
Probably yes. The reflog entry for an unreachable commit is kept for 30 days by default (gc.reflogExpireUnreachable), and the object can't be pruned while that entry exists. Look in git reflog, then create a branch at the commit. After the entry expires, the object can be deleted at the next garbage collection.
Why can a packfile store version 1 of a file as a delta of version 20?
Answer
Git chooses delta bases by similarity, not by history order. Recent versions are read most often, so storing the newest version in full and older ones as deltas against it makes common operations fast. Any similar objects can share data, even different files.
A teammate's script reads .git/refs/heads/main to find the current commit. Why will it break, and what should it use?
Answer
The ref may be in packed-refs rather than a loose file, and with reftable, the default in Git 3.0, there are no ref files at all. Use git rev-parse refs/heads/main (or git rev-parse HEAD), which works with every storage format.
Review quiz
Test yourself on Part 11 with the Git Part 11 review quiz.
Previous: Refs, HEAD and Plumbing Commands: Building a Commit by Hand

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.