Git Object Model (Internals)
In this page:
The .git Folder Internals
Behind the scenes, Git is a simple, content-addressable key-value database. All project files, histories, and commits are stored inside the .git/objects directory as compressed files identified by their SHA-1 hashes.
Example: The .git Folder Internals
ls .git/objects/
Blobs (File Content)
A blob (binary large object) stores only raw file contents. It does not store filename, directory path, or modification date data. If two files have identical contents, they share the same blob.
Example: Blobs (File Content)
git hash-object -w file.txt
git cat-file -p a1b2c3d
Trees (Directories)
A tree is Git's representation of a directory listing: for each entry it records a name, a mode (file vs. directory), and the SHA-1 of either a blob (a file) or another tree (a subdirectory) — nested trees are how Git represents an entire directory structure as a chain of hashes.
Example: Trees (Directories)
git cat-file -p HEAD^{tree}
Commits (Snapshots)
A commit object doesn't store file contents directly — it points to one tree hash representing the entire project's state at that moment, records one or more parent commit hashes (more than one for a merge), and carries the author, committer, timestamp, and message.
Example: Commits (Snapshots)
git cat-file -p HEAD
References (Refs)
A ref is nothing more than a small text file containing a 40-character commit hash — .git/refs/heads/main for the main branch, .git/refs/tags/v1.0 for a tag. A branch "moving forward" when you commit is really just that one file being overwritten with a new hash.
Example: References (Refs)
cat .git/refs/heads/main
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: