Most Git commands make sense once you know how Git stores data, and the model is surprisingly small: four kinds of objects in a content-addressed database, plus names that point at them. This lesson opens the .git folder and builds that model from real output: blobs, trees, commits, and tags; references and HEAD; loose objects and packfiles; and why history is tamper-evident. It finishes with git bisect, which uses that history to find the exact commit that introduced a bug.
Before you start
You should be comfortable with everything in the Basic and Intermediate levels, and with the security ideas in Git Repository Security. Follow along in a throwaway repository; the commands here only read data, except where noted.
A content-addressed database
Git's core is a key-value store. You give it content; it stores it and gives back a key computed from the content itself: a cryptographic hash. The same content always produces the same key:
$ echo "Miso soup" | git hash-object --stdin
5486348264df97a71e139963d506fef18db170f5Run that on any computer and you get the same ID. When you stage a file with that content, Git writes an object under that name:
$ echo "Miso soup" > soup.md
$ git add soup.md
$ find .git/objects -type f
.git/objects/54/86348264df97a71e139963d506fef18db170f5The first two hex characters become a directory, the rest the file name. The file holds the content, compressed with zlib, with a small header.
Repositories use SHA-1 by default. Git also supports SHA-256 repositories (git init --object-format=sha256), and its plans for Git 3.0 make SHA-256 the default for new repositories, but hosting support is still limited, so check your platform before using it. Git's SHA-1 implementation includes detection of the known collision attack technique.
The four object types
Blobs: file contents
A blob stores the bytes of a file, nothing else: no name, no permissions. Two files with identical content, anywhere in the history, share one blob.
Trees: directories
A tree lists entries: a mode (file, executable, symlink, or subdirectory), an object type, an ID, and a name:
$ git cat-file -p HEAD^{tree}
100644 blob 5486348264df97a71e139963d506fef18db170f5 soup.mdSubdirectories are trees pointing at other trees. A tree is a complete snapshot of a directory.
Commits: snapshots with history
A commit points to one root tree and records parents, author, committer, and message:
$ git cat-file -p HEAD
tree 3a03145536636b01fbc6945f8bef5cc23d787899
author Ada Example <ada@example.test> 1793523600 +0000
committer Ada Example <ada@example.test> 1793523600 +0000
Add soupThis is the first commit, so it has no parent line. A normal commit has one parent; a merge commit has two or more, as in this merge from lesson 4:
tree 0790bc17d2824d581c0987e4a99dc7faa6764b5a
parent 26142a7b0dbf364fec5aefa1a56ad22ea23c63d2
parent c50297274da601788cb0a567ddb8264491cd80b4The author wrote the change; the committer recorded it. They differ after a rebase or when a maintainer applies someone else's patch.
Tags: named, annotated pointers
An annotated tag object points at another object (usually a commit) and adds a tagger, date, message, and optional signature. Lightweight tags aren't objects at all, just references.
Why history is tamper-evident
A commit's ID is the hash of its content, which includes its tree ID and its parent IDs. Change one byte in an old file and its blob ID changes, so its tree's ID changes, so its commit's ID changes, so the next commit's parent line changes and its ID changes, all the way to the latest commit. That's why rewriting history (amend, rebase, filter-repo) produces new IDs, and why a signature on a single recent commit or tag (lesson 12) vouches for the entire history beneath it, as far as the hash function is trustworthy.
References: names for commits
Hashes are hard to remember, so Git keeps references (refs): files that contain an object ID.
$ cat .git/HEAD
ref: refs/heads/main
$ cat .git/refs/heads/main
d6dd777342b537cf164ad6080476060af394059frefs/heads/*are your branches.refs/remotes/<remote>/*are remote-tracking branches.refs/tags/*are tags.HEADis usually a symbolic ref naming the current branch. In "detached HEAD" state, it contains a commit ID directly.
Committing writes new objects and then updates the branch ref to the new commit ID. That's the whole operation. The reflog (lesson 9) is a log of these ref updates.
Use plumbing commands rather than reading files, because refs aren't always loose files: they can be packed into .git/packed-refs, and newer repositories can use the reftable format (git init --ref-format=reftable), which Git's plans for 3.0 make the default for new repositories:
git rev-parse main # the commit a ref points to
git show-ref # every ref with its ID
git for-each-ref --format='%(refname:short) %(objectname:short)' refs/headsLoose objects and packfiles
New objects are written as individual compressed files, called loose objects. After 21 commits in a small test repository, there were 63 of them:
$ git count-objects -v
count: 63
size: 252Storing every version as a full file would waste space, so Git periodically packs objects into a packfile: one file containing many objects, where similar objects are stored as deltas (differences) against each other, plus an index for fast lookup. git gc (and the automatic maintenance Git runs in the background) does this:
$ git gc
$ git count-objects -v
count: 0
size: 0
in-pack: 63
packs: 1
size-pack: 7Note that Git's snapshot model (lesson 1) is about the *logical* model: every commit is a full snapshot. Packfiles are a storage optimization underneath it. Packfiles are also what's sent over the network when you fetch or push, which is why transfers are compact.
Unreachable objects and garbage collection
An object is reachable if some ref, or the reflog, leads to it through commits, trees, and parents. After a reset or rebase, the old commits become unreachable once their reflog entries expire. git gc eventually deletes unreachable objects that are old enough. That's the precise reason the reflog can recover "lost" commits for a while, but not forever.
git fsck --unreachable # list objects nothing points to
git fsck --lost-found # write dangling commits and blobs to .git/lost-foundSearching history
The object model also makes history searchable in ways that go far beyond reading git log:
git log -S "calculateTotal" --oneline # commits that added or removed this exact text
git log -G "total\s*=" --oneline # commits whose diffs match a regular expression
git log -L :calculateTotal:src/cart.ts # the history of one function
git log --follow -- src/cart.ts # a file's history across renames
git blame -w -C src/cart.ts # who changed each line, ignoring whitespace and moved codeThe -S search (often called the "pickaxe") is the fastest way to find when a line of code appeared or disappeared, and -L traces one function through every change, including the commits where it moved. These work because every commit is a full snapshot that Git can compare with its parent on demand.
Using history to debug: git bisect
Because every commit is a complete, identifiable snapshot, Git can search history efficiently. git bisect performs a binary search between a commit you know is good and one you know is bad. Each step halves the remaining range, so a thousand commits take about ten tests.
Manually:
git bisect start
git bisect bad # the current commit has the bug
git bisect good v1.4.0 # this release didn't
# Git checks out a commit in the middle. Test it, then report:
git bisect good # or: git bisect bad
# ...repeat until Git names the first bad commit...
git bisect reset # return to where you startedBetter still, give Git a script that exits 0 for good and non-zero for bad, and it runs the whole search itself. In this example, a value in price.txt was supposed to stay below 14:
#!/bin/sh
n=$(cut -d" " -f2 price.txt)
[ "$n" -lt 14 ]$ git bisect start HEAD HEAD~19
Bisecting: 9 revisions left to test after this (roughly 3 steps)
$ git bisect run ../check.sh
running '../check.sh'
Bisecting: 4 revisions left to test after this (roughly 2 steps)
...
bf0d54089f90fee497e72668793286ffecd55edf is the first bad commit
bisect found first bad commitIn real projects the script is usually your test command for one failing test, such as npm test -- cart.test.ts. Exit code 125 tells bisect to skip a commit that can't be tested, for example one that doesn't build. Bisect works best with small commits where every commit builds, another reason for the habits from lesson 3. Once bisect names the commit, write the failing test first, then fix it (see regression testing in our testing series).
Common mistakes and troubleshooting
- Editing files inside
.gitby hand. Use plumbing commands (update-ref,rev-parse); hand edits can corrupt the repository. - Reading
.git/refsdirectly in scripts. Refs may be packed or stored in reftable; usegit rev-parseandgit for-each-ref. - Assuming deleted commits are gone immediately. They stay until unreachable and expired, which matters for both recovery and for secrets.
- Bisect stuck on commits that don't build. Have your script exit 125 for those, or use
git bisect skip. - Forgetting
git bisect reset. You'll be left on a detached HEAD in the middle of history. - "object file is empty" or "loose object is corrupt". Usually after a crash or full disk. Run
git fsck, and recover missing objects from another clone or the remote.
Practical exercise: explore and bisect
In a throwaway repository:
- Run
echo "hello" | git hash-object --stdin, then create a file with that content, stage it, and find the object under.git/objects. - Commit, then use
git cat-file -tandgit cat-file -pto walk from HEAD to the commit, its tree, and the blob. - Create an annotated tag and inspect the tag object with
git cat-file -p. - Make 20 commits, compare
git count-objects -vbefore and aftergit gc, and list.git/objects/pack. - Make one of those commits introduce a "bug" (as in the example), write a check script, and find it with
git bisect run. - Reset away three commits and find them again with
git fsck --lost-foundor the reflog.
Key takeaways
- Git is a content-addressed object database: blobs (contents), trees (directories), commits (snapshots with parents), and tags.
- IDs are hashes of content, so history is tamper-evident and rewriting creates new IDs.
- References are names for commits; HEAD names the current branch; use plumbing commands to read them.
- Loose objects are periodically packed into delta-compressed packfiles; unreachable objects are eventually garbage-collected.
git bisect, especiallygit bisect run, finds the commit that introduced a bug in logarithmic time.
References
- Pro Git: Git Internals, Git Objects (opens in a new tab)
- Pro Git: Git References (opens in a new tab)
- Pro Git: Packfiles (opens in a new tab)
- Git documentation: git bisect (opens in a new tab)
- Git documentation: hash-function-transition (opens in a new tab)
- Git: planned breaking changes for Git 3.0 (opens in a new tab)
What's next
Internals explain why some repositories get slow and large. Managing Large Codebases: Monorepos, Git LFS, Submodules, and Worktrees covers the tools for those situations.

