← Back to Git Course | Chapter 10: Tools & Internals | Lesson 4 of 8

Git Object Model (Internals)

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

bash
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)

bash
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)

bash
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)

bash
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)

bash
cat .git/refs/heads/main

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.