Git stores your history as snapshots and pointers, not as a chain of diffs. Once you know how Git stores history, you can say what rebase and reset really change, and which lost commits you can still get back. This article traces one file into .git, then follows commits, branches, the reflog and packfiles, down to a reflog bug in current releases.
The commands below ran on Git 2.50.1. What the article says about the documentation was checked against Git 2.56.1, the newest release.
What git add and git commit write into .git
Run this in an empty folder. The environment variables fix the author and the date, so your commit IDs should match the ones below. Without them, only the IDs of file contents match.
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com
export GIT_AUTHOR_DATE=2026-01-01T10:00:00Z GIT_COMMITTER_DATE=2026-01-01T10:00:00Z
git init -q -b main demo && cd demo
echo 'hello' > greeting.txt && git add greeting.txt
find .git/objects -type f
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
There is no commit yet, but git add has already written one file into .git/objects. Its name is 40 hex characters, split after the first two only to keep each directory small (gitformat-loose).
The name is the SHA-1 hash of a short header plus the file’s content. You can rebuild it without Git, and compare it with what git hash-object computes:
printf 'blob 6\0hello\n' | shasum
git hash-object greeting.txt
ce013625030ba8dba906f756967f9e9ca394464a -
ce013625030ba8dba906f756967f9e9ca394464a
The header is the word blob, a space, the size in bytes, and a null byte. Git calls each item it stores this way an object, and the hash is its object ID. An object that holds a file’s content is a blob. The file on disk is the same header and content, compressed with zlib:
python3 -c "import zlib;print(zlib.decompress(open('.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a','rb').read()))"
b'blob 6\x00hello\n'
The blob has no file name, no date and no permissions. The file name lives in a tree, as the next step shows. Now commit and print the commit object:
git commit -q -m 'Add greeting'
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'
tree 57e9529754dc514a3ec10db2ff882018fbe1fcbf
author Ada <ada@example.com> 1767261600 +0000
committer Ada <ada@example.com> 1767261600 +0000
Add greeting
100644 blob ce013625030ba8dba906f756967f9e9ca394464a greeting.txt
The commit holds no diff and no file list. Its first line names one object, a tree. A tree is a list of entries, each with a type, an object ID, a name and a mode, a number that says what kind of entry it is. This tree has one entry, and the file name greeting.txt lives here, next to the blob’s ID.
Later commits also get a parent line; the first commit has none.
Now predict: you copy greeting.txt to copy.txt and add it. How many new blobs does Git write?
cp greeting.txt copy.txt && git add copy.txt
find .git/objects -type f | sort
git commit -q -m 'Add copy'
git cat-file -p 'HEAD^{tree}'
git log --oneline
.git/objects/57/e9529754dc514a3ec10db2ff882018fbe1fcbf
.git/objects/a3/96a54b969d1311d49c71d533a69a39691378a3
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
100644 blob ce013625030ba8dba906f756967f9e9ca394464a copy.txt
100644 blob ce013625030ba8dba906f756967f9e9ca394464a greeting.txt
a430d42 Add copy
a396a54 Add greeting
Zero. After git add copy.txt, .git/objects still holds the same three objects: the blob, the first tree and the first commit. The content of copy.txt hashes to ce01362, and that object already exists. The new tree lists two names that point to one blob.
Rule: An object’s ID is the hash of its content, so Git stores each distinct content once, however many files or commits use it.
How Git stores history as four kinds of objects
Git has four object types: blob, tree, commit and tag. This article does not need the tag object. In its own file under .git/objects, every object is stored the same way: a header with its type and size, a null byte, then the data. The ID is the hash of all of that before compression, SHA-1 or SHA-256 depending on the repository (gitformat-loose, Pro Git 10.2).
A commit points to exactly one top-level tree, which is the whole snapshot of the project. It also records its parents, the author, the committer and the message. Pro Git calls this design “snapshots, not differences”. Files and directories that did not change keep the same blobs and subtrees as in the parent commit.
The number at the start of each tree entry is its mode. Git supports only five modes (git-fast-import):
| Mode | Entry |
|---|---|
100644 |
regular file |
100755 |
executable file |
120000 |
symbolic link |
040000 |
directory, a subtree |
160000 |
a submodule’s commit (a gitlink) |
There is no 100664, because the index records only 0644 or 0755 for files (gitformat-index). Git tracks whether a file is executable, and no other permission bits.
Because the ID depends only on the content, an object never changes. A changed file gets a new blob, its directory a new tree, and the commit a new ID. A commit’s content includes its parent’s ID, so changing an old commit gives every commit after it a new ID too.
A branch is a file that holds one commit ID
Look at what HEAD and main contain in the demo repository:
cat .git/HEAD
cat .git/refs/heads/main
cat .git/logs/refs/heads/main
ref: refs/heads/main
a430d422a921bf22b5747c0ce00abaa4f3f4007d
0000000000000000000000000000000000000000 a396a54b969d1311d49c71d533a69a39691378a3 Ada <ada@example.com> 1767261600 +0000 commit (initial): Add greeting
a396a54b969d1311d49c71d533a69a39691378a3 a430d422a921bf22b5747c0ce00abaa4f3f4007d Ada <ada@example.com> 1767261600 +0000 commit: Add copy
A ref is a name that points to an object ID. Branches live in .git/refs/heads, tags in refs/tags, and remote-tracking branches in refs/remotes (gitrepository-layout). A branch is not a copy of your files. Creating one writes 41 bytes: 40 hex characters and a newline (Pro Git 3.1).
HEAD is a symbolic ref, a ref that names another ref instead of an object (git-symbolic-ref). When you commit, Git moves the branch that HEAD names. When you check out a commit directly, HEAD holds a raw commit ID, and Git calls that a detached HEAD.
The third file is the branch’s reflog: one line for each time the ref moved, with the old ID, the new ID, who, when and why. Reflogs live in .git/logs/ and are on by default in repositories with a working tree (git-config, core.logAllRefUpdates). git update-ref moves a ref and appends these lines. Given an expected old value, it moves the ref only if the ref still holds that value (git-update-ref).
Git can also move refs out of their own files into one file, .git/packed-refs (git-pack-refs). So read a ref with git rev-parse main in scripts, not with cat.
This is the whole demo repository so far. The figure at the top shows the same parts.
refs: names that move objects: named by the hash of their content
HEAD ──▶ main ──▶ commit a430d42 ──▶ tree 373e0c2 ─┬─ greeting.txt ──▶ blob ce01362
┆ "Add copy" └─ copy.txt ──────▶ "hello"
┆ │ parent ▲
┆ ▼ │
┆ commit a396a54 ──▶ tree 57e9529 ─── greeting.txt ────────┘
┆ "Add greeting"
┆
reflog of main: main@{0} a430d42
main@{1} a396a54
Note: reftable. Git 2.45.0 (April 2024) added reftable, a binary format that stores all refs in a few files (reftable, RelNotes 2.45.0). You choose it with
git init --ref-format=reftable, and since 2.46.0git refs migrateconverts an existing repository (git-refs). In 2.56.1 it still cannot migrate a repository that has worktrees. Under reftable a branch is no longer a file, but it is still a name that points to a commit.
The index is the tree of your next commit
git add did two things in the first example. It wrote the blob, and it recorded the path and the blob’s ID in .git/index. The index is a binary file that lists every path for the next commit, sorted by path, with its mode and object ID (gitformat-index).
git ls-files --stage
100644 ce013625030ba8dba906f756967f9e9ca394464a 0 copy.txt
100644 ce013625030ba8dba906f756967f9e9ca394464a 0 greeting.txt
The 0 is the merge stage; during a merge conflict, one path can have entries at several stages. Each entry also stores file stat data, such as size and modification time. git commit turns the index into tree objects and writes a commit that points to the top tree.
Reset, rebase and cherry-pick add commits and move refs
Add a third commit to the demo, then drop the last two with reset --hard. Before you run it, predict whether commit “Extend greeting” still exists afterwards.
echo 'hello, world' > greeting.txt && git commit -q -am 'Extend greeting'
git reset --hard HEAD~2
git log --oneline
git reflog
HEAD is now at a396a54 Add greeting
a396a54 Add greeting
a396a54 HEAD@{0}: reset: moving to HEAD~2
42c1fdb HEAD@{1}: commit: Extend greeting
a430d42 HEAD@{2}: commit: Add copy
a396a54 HEAD@{3}: commit (initial): Add greeting
git log shows one commit, but the reflog of HEAD still lists 42c1fdb. reset deleted no objects. It moved main back to a396a54 and wrote the move into the reflogs. HEAD@{1} means “where HEAD pointed one move ago” (gitrevisions), so this moves main back:
git reset --hard HEAD@{1}
HEAD is now at 42c1fdb Extend greeting
git log did not show 42c1fdb, because no ref led to it. An object is reachable when you can get to it from a ref, by following parents from commits and entries from trees (gitglossary). The three reset modes differ only in what they reset after moving the branch (git-reset):
| Command | Moves the branch | Resets the index | Resets the working tree |
|---|---|---|---|
git reset --soft |
yes | no | no |
git reset --mixed (the default) |
yes | yes | no |
git reset --hard |
yes | yes | yes |
All three also save the old position in ORIG_HEAD, a ref that am, merge, rebase and reset write before they move HEAD (gitrevisions). git fetch writes a similar ref, FETCH_HEAD.
The other history commands are moves on the same graph:
- Cherry-pick records a new commit for each commit you pick (git-cherry-pick).
- Rebase writes a new commit for each commit it replays, then points the branch at the last one (git-rebase, Pro Git 3.6).
- A fast-forward merge moves the ref forward and writes no commit (git-merge).
git branch -ddeletes the ref and its reflog. The commits stay (git-branch).
A rebase does not move commits. In a separate run, a feature commit 66afa46 became 6df2e65 after git rebase main. Its message was the same, but its parent changed, and a commit’s hash covers its parent’s ID. The reflog of HEAD shows each step:
6df2e65 HEAD@{0}: rebase (finish): returning to refs/heads/feature
6df2e65 HEAD@{1}: rebase (pick): Feature work
29ef494 HEAD@{2}: rebase (start): checkout main
66afa46 HEAD@{3}: commit: Feature work
After git branch -D feature, git fsck --no-reflogs --unreachable listed both 66afa46 and 6df2e65 as unreachable commits, with their trees and blob. git fsck checks the object database, --unreachable lists objects that nothing reaches, and --no-reflogs stops it from counting reflog entries as references (git-fsck). Your IDs from a run like this will differ, because they depend on the author and the time.
Rule: History commands write new objects and move refs; the old objects stay until garbage collection removes them.
Building a commit by hand with plumbing commands
Git’s everyday commands, such as add, commit and reset, are called porcelain. Each of them runs several plumbing commands, low-level commands that each do one step. You can make a commit with plumbing alone:
blob=$(echo 'built by hand' | git hash-object -w --stdin)
git update-index --add --cacheinfo 100644,$blob,notes.txt
tree=$(git write-tree)
commit=$(echo 'Commit built by hand' | git commit-tree $tree -p main)
git update-ref refs/heads/byhand $commit
git log --oneline byhand
git reset -q
b0cc3b4 Commit built by hand
42c1fdb Extend greeting
a430d42 Add copy
a396a54 Add greeting
Each step is one move on the model:
hash-object -wwrites a blob, which is the first half ofgit add.update-index --cacheinfoputsnotes.txtin the index with that blob’s ID, which is the second half.write-treeturns the index into tree objects and prints the top tree’s ID (git-write-tree).commit-treewrites a commit with that tree andmainas its parent. It moves no ref (git-commit-tree), so at this point no branch reaches the commit.update-refcreates the branchbyhandand appends to its reflog (git-update-ref). Only now does the commit show up ingit log.
The last line, git reset -q, resets the index to match main again, because notes.txt is in the index but not on disk. The result is a normal commit, and nothing in it records that it was built by hand.
Deltas exist only inside packfiles
A commit does not store a diff against its parent. The first example showed that a commit stores a tree, and a tree names whole blobs. Git still saves space, but it does that in a different place.
This run writes a 108,900-byte file, appends one line, commits both versions and then packs the repository:
cd .. && git init -q -b main pack && cd pack
seq 1 20000 > big.txt && git add big.txt && git commit -q -m v1
echo 20001 >> big.txt && git commit -q -am v2
git gc -q
git verify-pack -v .git/objects/pack/pack-*.idx | grep blob
ff033aa65b35b5efef0da1a120f47baf9782388d blob 108900 43768 259
7599e0c9615053f4425667d889c445b2634f1cf9 blob 11 23 44119 1 ff033aa65b35b5efef0da1a120f47baf9782388d
Before git gc, the two versions were loose objects, each in its own file and compressed on its own, at 38,965 and 38,963 bytes. After it, both sit in one packfile, a single file that holds many objects, with a .idx file that finds each object by ID (gitformat-pack). The columns are the ID, the type, the size, the size in the pack and the offset in the pack.
The first line, ff033aa, is the newer version, the one with 20001 in it. It is stored whole: 108,900 bytes, compressed to 43,768.
The second line is the older version, and it takes 11 bytes. It is a delta, instructions to rebuild an object from another object, called its base. The last two columns give the chain depth, 1, and the base, ff033aa, which is the newer version. The IDs come from the content; the pack sizes and offsets in your run may differ.
Git does not pick delta bases by history. git pack-objects sorts objects by type, a hash of the file name, and size from large to small. Then it tries bases within a window of 10 objects, up to a chain depth of 50 (git-pack-objects, pack-heuristics). Pro Git’s packfile example shows the same result, with the later version stored whole.
Deltas exist only inside packs. The pack format has two delta entry types, but its documentation says that conceptually there are still only four object types (gitformat-pack). Commands that read an object, such as git cat-file, get the full content back.
Git packs objects when you run git gc, and automatic maintenance packs them once there are about 6,700 loose objects (git-maintenance). Since 2.54, automatic maintenance uses geometric repacking by default (RelNotes 2.54.0). A fetch or push with fewer than 100 objects is unpacked into loose objects; a larger one stays a pack (transfer.unpackLimit in git-config).
Note: other files in .git/objects. The commit-graph file (since 2.18) stores commit IDs, parents, root trees, dates and generation numbers, which speeds up
git logand merge-base searches (gitformat-commit-graph). A multi-pack index (2.20) indexes objects across several packs, and reachability bitmaps (2.0, for multi-pack indexes since 2.34) record which objects each commit reaches (bitmap-format). They are indexes over the same objects.Rule: A commit names a whole snapshot; a delta is a storage choice inside a pack, made by size and file name, not by history.
What snapshots and permanent objects cost
Every clone carries the whole history. A large file that someone added once stays in every clone, even after a later commit deletes it (Pro Git, Maintenance and Data Recovery). The Git FAQ says that storing binary dependencies and build products in Git is generally not recommended (gitfaq).
Very large files get no deltas. Files larger than core.bigFileThreshold, 512 MiB by default, are stored without delta compression (git-config). Ten versions of such a file are ten separately compressed copies.
Rewriting shared history creates work for everyone who pulled it. A rebase gives commits new IDs. Anyone who built work on the old commits must move it by hand, and the rebase docs give this its own section, “Recovering from upstream rebase” (git-rebase).
A committed secret stays. Removing a password in a new commit leaves the old blob in your packs, in your reflogs, and in every clone that already fetched it. The git filter-branch docs say that command is not recommended and point to git filter-repo for rewriting history (git-filter-branch). Even after a rewrite, the old objects stay in your repository until you run git reflog expire --expire=now --all and then git gc --prune=now. Those two commands delete your own recovery points too, and existing clones keep the secret, so treat it as leaked.
Recovery has a deadline. Lost commits survive only while a reflog entry reaches them, and then for a grace period before git gc prunes them. The next section gives the numbers, and one of them is wrong in current releases.
Getting lost commits back
git gc keeps every object reachable from refs, the index and reflogs (git-gc). So the reflog is the first place to look after a bad reset or rebase. git reflog shows HEAD‘s moves, and git reflog show main shows one branch. Any entry works as a revision, such as HEAD@{2} or main@{1}.
When no reflog entry reaches a commit, git fsck --unreachable still lists it until it is pruned. --lost-found writes the IDs of unreachable commits into .git/lost-found/ (git-fsck). The glossary calls an unreachable object that no other unreachable object reaches dangling (gitglossary).
A dropped stash entry is a commit too. The git stash docs give this recipe to list stash entries that still exist but are no longer reachable (git-stash):
git fsck --unreachable |
grep commit | cut -d\ -f3 |
xargs git log --merges --no-walk --grep=WIP
It looks for unreachable merge commits with “WIP” in their message, because Git stores each stash entry as a merge commit. Once you find the ID, git stash apply <id> restores it.
Unreachable objects do not last. Once no reflog entry reaches an object, git gc deletes it if it is older than gc.pruneExpire, 2 weeks by default (git-config). Younger unreachable objects go into a separate cruft pack, which git gc writes by default, until they reach that age (git-gc). git prune removes only loose objects (git-prune).
The reflog keeps reachable entries for 30 days, not 90
The git-config docs give two reflog defaults. gc.reflogExpire is 90 days, for entries whose commit is still reachable from the branch. gc.reflogExpireUnreachable is 30 days, for entries whose commit is not.
This script makes three commits dated 60 days ago, all on main and so all reachable. Predict how many reflog entries survive git reflog expire.
cd "$(mktemp -d)" && git init -q -b main
old="$(( $(date +%s) - 60*24*3600 )) +0000" # 60 days ago, as Unix time
for i in 1 2 3; do
echo "$i" > f.txt && git add f.txt
GIT_COMMITTER_DATE="$old" git commit -q -m "c$i"
done
git reflog | grep -c .
git reflog expire --all
git reflog | grep -c .
3
0
None. On Git 2.50.1, git reflog expire removed all three reachable entries at 60 days. The code swaps the two defaults: reachable entries expire after 30 days and unreachable ones after 90. You can see it in reflog.h lines 26–27 at v2.56.1.
The bug came in with commit 85658275 in April 2025 and shipped in 2.50.0. Commit 60cf500551 fixed it on 2026-09-24. On 2026-11-18 the fix was on master only, in no release, so Git 2.50.0 through 2.56.1 all have the bug.
With git config gc.reflogExpire 90.days added after git init, the same script prints 3 twice. Setting the value yourself overrides the swapped default.
Large files: partial clone and Git LFS
Every clone carries every blob, unless you ask for less. A partial clone skips some objects, such as large blobs, and fetches them on demand from the remote it came from, which Git calls the promisor remote (partial-clone). Its cost is that reading an old file needs that remote to be reachable.
Another option is Git LFS, a separate project with no page in Git’s own documentation, so we only name it here. For a repository of source code and text, a full clone is usually fine, because packs store small changes to text files as small deltas.
What changed in recent Git versions, and what Git 3.0 plans
| Version (date) | Change | Source |
|---|---|---|
| 2.38 (2022-10-02) | git rebase --update-refs |
RelNotes 2.38.0 |
| 2.44 (2024-02-22) | git replay, experimental |
RelNotes 2.44.0 |
| 2.45 (2024-04-29) | reftable; git reflog list |
RelNotes 2.45.0 |
| 2.46 (2024-07-29) | git refs migrate |
RelNotes 2.46.0 |
| 2.50 (2025-06-15) | git reflog drop; the reflog defaults bug |
RelNotes 2.50.0 |
| 2.51 (2025-08-17) | reftable and SHA-256 announced as Git 3.0 defaults | RelNotes 2.51.0 |
| 2.52 (2025-11-17) | main announced as the Git 3.0 default branch name |
RelNotes 2.52.0 |
| 2.54 (2026-04-19) | maintenance uses geometric repacking by default; git history, experimental |
RelNotes 2.54.0 |
| 2.56 (2026-09-27) | git history drop |
RelNotes 2.56.0 |
Git 3.0 plans to make SHA-256, reftable and main the defaults, and to require Rust. The BreakingChanges document says it has no planned release date.
SHA-256 is usable today, but no release calls it stable. Git has used a hardened SHA-1 that detects collision attacks by default since 2.13.0 in 2017 (hash-function-transition). SHA-256 repositories became usable as an experiment in 2.29 (2020), and the 2.42 release notes (2023) say no breaking changes are planned for them. In 2.56.1 a SHA-256 repository still cannot exchange objects with a SHA-1 one, git init prints a warning, and extensions.compatObjectFormat is incomplete (git-init, RelNotes 2.42.0, RelNotes 2.52.0).
A recovery cheat sheet and one setting to change
Cheat sheet: getting work back
- See where
HEADor a branch pointed:git reflog,git reflog show main.- Undo the last reset, merge or rebase:
git reset --hard ORIG_HEAD.- Go back to any earlier position:
git reset --hard HEAD@{n}. Both--hardcommands also discard uncommitted changes in your working tree.- Keep an old position without moving your branch:
git branch rescue HEAD@{n}.- Find commits no ref or reflog reaches:
git fsck --unreachable, or--lost-foundto save their IDs.- Find a dropped stash: the
git fsck --unreachable | grep commit …recipe from thegit stashdocs.- Deadline: after the reflog entry expires,
git gcremoves objects older than 2 weeks (gc.pruneExpire).
Until a release carries the fix, set the reachable reflog expiry yourself:
git config --global gc.reflogExpire 90.days
The cost is small, because these entries point to commits that a branch still holds. Once your Git includes commit 60cf500551, you can remove the setting and get the documented default back.
To see your own repository this way, start at git cat-file -p HEAD, then pass each ID it prints to git cat-file -p until you reach a blob. Then run git verify-pack -v on your largest pack in .git/objects/pack/ and look for the blobs stored as deltas.
Primary sources for further reading:
- The object and pack formats: gitformat-loose, gitformat-pack, gitformat-index and pack-heuristics
- Refs and the repository layout: gitrepository-layout, gitrevisions and reftable
- Expiry and recovery: the
gc.*settings in git-config, git-gc and git-fsck - The Git Internals chapter of Pro Git
- Where Git is going: hash-function-transition and BreakingChanges
Subscribe to the Stay Online newsletter to get the next article on how the tools you use every day work underneath.
Comments
No comments yet. Ask a question or share what worked for you.