Skip to main content
Code Matrix

Git Internals Explained: Blobs, Trees, Commits, and Why Branches Cost Bytes

Git does not store diffs between commits, and branches are not copies of your repository folder. Look inside .git/objects to understand blobs, trees, snapshots, and 41-byte refs.

Published 2026-08-25Updated 2026-10-3010 min read714 words
Table of Contents (7 sections)

For many developers, Git feels like a magical black box of incantations: git add, git commit, git checkout -b, and occasional panic when git rebase hits a merge conflict. Most guides teach surface commands while saying "Git saves your file diffs and makes a copy for each branch."

Both of those mental models are slightly wrong—and understanding what Git actually writes inside .git/ transforms Git from fragile syntax memorization into an intuitive content-addressable filesystem.

Git Stores Snapshots, Not Diffs

Subversion (SVN) and older version-control tools recorded a base file plus a sequential chain of line-by-line deltas (v1 -> diff -> v2 -> diff -> v3). Reconstructing commit #500 meant replaying 499 patches.

Git takes the opposite approach. Conceptually, every Git commit stores a snapshot of your entire repository directory at that moment in time. If a file did not change between Commit A and Commit B, Git does not copy it again—it simply points the new commit's directory tree straight at the identical hashed object already stored inside .git/objects/.

Compression (packfiles) calculates binary deltas later strictly for disk/network optimization, never changing Git's logical graph model.

The Four Object Types Inside .git/objects

At its core, .git/objects is a key-value store indexed by SHA-1 (or SHA-256) content hashes. Give Git a piece of data, and it prepends a tiny header ("<type> <byte-size>\0"), hashes the result, compresses it with zlib, and writes it under the hash path.

Object TypeWhat It RepresentsContains
BlobContents of a single fileRaw bytes only—zero filename or permissions
TreeA directory snapshotFile mode, filename, and pointers to blobs & sub-trees
CommitA point-in-time snapshot recordRoot tree hash, parent commit hash(es), author, timestamp, message
Tag (Annotated)Signed release milestoneTarget commit hash, tagger, release note, optional GPG/SSH signature

Notice a subtle consequence of Blobs having no filenames: if you copy README.md into backup/README.md with identical contents, .git/objects stores only one blob. Both directory tree entries simply point to the exact same 40-character hash. And if you rename a file, its blob hash stays 100% identical while only the parent tree entry updates!

Watch Git's Hashing Rule Live in Node.js

You can verify Git's exact blob hashing algorithm yourself—it matches git hash-object byte for byte:

ts
// git-blob-hash.ts — Reproduces `git hash-object` byte-for-byte
import { createHash } from "node:crypto";

export function computeGitBlobHash(content: string | Buffer): string {
  const payload = Buffer.isBuffer(content) ? content : Buffer.from(content, "utf8");
  const header = Buffer.from(`blob ${payload.byteLength}\0`, "utf8");
  const fullObject = Buffer.concat([header, payload]);

  return createHash("sha1").update(fullObject).digest("hex");
}

const sample = "console.log('Code Matrix');\n";
const hash = computeGitBlobHash(sample);

console.log("Computed Git SHA-1:", hash);
// Verify in shell with: printf "console.log('Code Matrix');\n" | git hash-object --stdin

Run printf "console.log('Code Matrix');\n" | git hash-object --stdin in your terminal and notice the hex string matches down to the final character. That immutable hash guarantees repository integrity: flipping a single bit anywhere in historical files alters that blob hash, which alters its parent tree hash, which alters every descendant commit hash!

Why Branches Are Cheap 41-Byte Files

Because commits form a Directed Acyclic Graph (DAG) linking backward to parent commits (commit -> parent -> grandparent), what is a branch like main or feature/payments?

Inspect .git/refs/heads/ inside any repository and you will find tiny plain-text files containing literally 41 bytes: a 40-character hexadecimal commit hash plus a newline character.

text
.git/HEAD                -> ref: refs/heads/main
.git/refs/heads/main     -> 8a3f9c2e41b07d... (41 bytes!)
.git/refs/heads/feature  -> c91d4b8820fa12... (41 bytes!)

Running git branch feature/auth does not duplicate 500 MB of source code. It simply writes one 41-byte file containing the current commit SHA! Switching branches (git switch) updates .git/HEAD and swaps modified files between the old tree snapshot and the target tree snapshot.

How detached HEAD Happens Safely

Usually .git/HEAD points symbolically to a branch file (ref: refs/heads/main). When you run git checkout <commit-hash>, .git/HEAD points straight at that raw commit SHA instead of a named branch. You are in detached HEAD mode. Any new commit you create stays intact in .git/objects, but if you switch back without tagging or creating a branch (git switch -c saved-work), no branch pointer references your new commit until garbage collection eventually prunes unreferenced objects.

Inspecting Real Git Internals Yourself

Try running these plumbing commands safely in any repository:

bash
# Inspect exact object type and contents of HEAD
git cat-file -t HEAD
git cat-file -p HEAD

# List files inside HEAD's root directory tree
git ls-tree HEAD

# Inspect the raw 40-char hash saved inside refs/heads/main
cat .git/refs/heads/main

Seeing tree <sha>, parent <sha>, and author directly strips away every mystery around git merge, git reset, and git reflog—all of which simply read trees or move 41-byte branch pointers along the commit DAG.

Key Takeaways

  1. Content addresses everything: Identical files anywhere in history share one deduplicated blob hash inside .git/objects/.
  2. Directories live in Tree objects: Renames modify tree pointers, not duplicate blobs.
  3. Branches are tiny movable pointers: Creating or deleting a branch writes or deletes a 41-byte commit reference, not a copy of your workspace.
  4. History is tamper-evident: Every commit hashes its root tree and its parent hashes, making silent corruption mathematically detectable across teams.

Code Matrix Editorial

Engineering Education & Technical Publications

Code Matrix creates visual technical guides, systems breakdowns, and structured curricula connecting software fundamentals to the physical infrastructure of the modern world.

Continue Reading

Related Deep Dives

Browse all →