Skip to content

Lesson 1 of 14 · 18m

A Branch Is a 41-Byte File

HEAD, refs, and the index: what git actually stores, and why every command is pointer surgery.

Run cat .git/HEAD in any repository. One line comes back: ref: refs/heads/main. Twenty-one bytes, and that's where every git command starts. HEAD is a text file containing a pointer, the branch it points at is another file containing a SHA, and the commit at that SHA is an object that names its parent. Commits, checkouts, merges, rebases — all of it is bookkeeping over three small pieces of state. Engineers who see this stop treating git as incantations and start reading commands as pointer surgery.

The object store

Everything git stores lives in .git/objects as one of four types: blob (file contents), tree (a directory listing), commit, and annotated tag. Each object is named by the SHA-1 of its own contents — git init --object-format=sha256 swaps the hash on new repos — which is what makes objects immutable: change one byte and you have a different object with a different name, not a modified one.

Read a commit raw:

git cat-file -p HEAD
tree 6f2c9e...
parent 1a4b8d...
author Priya Nair <priya@corp.example> 1731340000 -0500
committer Priya Nair <priya@corp.example> 1731340000 -0500

Throttle login attempts to 5/min per IP

A commit is that little text block: a pointer to the snapshot tree, a pointer to its parent, two identities with timestamps, and the message. There is no list of commits stored anywhere — a branch's "history" is the chain of parent pointers walked backward from one commit. The DAG is the data structure.

A branch is a 41-byte file

git branch feature writes one file: .git/refs/heads/feature, containing the SHA of the commit you were standing on. Forty hex characters and a newline. Branches are not folders of commits. They're bookmarks — which is why git branch -D is instant and why deleting one destroys nothing you wrote: the objects sit in .git/objects until garbage collection decides nothing references them. (Big repos dedupe these files into .git/packed-refs. Same idea, one file, many refs.)

HEAD is the one ref allowed to hold another ref's name instead of a SHA — a symbolic ref. git symbolic-ref HEAD prints refs/heads/main. When you git checkout v1.4 — a tag, not a branch — git rewrites .git/HEAD to hold the raw SHA directly. You are now on "detached HEAD," and the warning it prints is real: commits you make here are named by nothing. They get reflog entries, but no branch points at them. git switch main and your two commits become orphaned objects — recoverable through git reflog, invisible to git log, deleted by the next aggressive git gc.

The fix when you notice: git switch -c fix-from-tag before you walk away, or find the SHA in the reflog afterward and git branch to it. The mechanism matters more than the recipe. "Commits on detached HEAD get lost" is the wrong model — nothing is lost. Unreferenced commits are unfindable by everything except the reflog. The commits are fine; the bookmarks are missing.

The three trees

Between HEAD and your editor sits the part beginners never see: the index (.git/index), a binary file holding your next commit under construction.

  • Working tree — the actual files on disk.
  • Index — the staged snapshot; git ls-files -s dumps its blob SHAs and stage numbers.
  • HEAD tree — the last committed snapshot.

git add writes blobs into .git/objects and updates the index. git commit writes a tree plus a commit object from the index — not from your working tree — then updates whichever ref HEAD names. git status is a three-way diff report across these trees: "modified" means worktree differs from index, "staged" means index differs from HEAD.

This is what makes the undo family predictable. git reset --soft HEAD~1 moves only the ref. --mixed also rewrites the index to match the target. --hard rewrites index and worktree. One command, three scopes of surgery. And it's why a merge conflict leaves the index holding three versions of a path — stage 1 base, stage 2 ours, stage 3 theirs — until you produce one version and git add it.

Pitfalls

  • Amend does not edit. git commit --amend writes a new commit object and moves the ref; the old one becomes unreferenced. Every "edit" in git is a new object — SHAs are content names, nothing changes in place.
  • Detached HEAD is not broken. CI systems check out SHAs constantly. It's a problem only when you commit there and walk away without a branch.
  • Read .git, don't hand-edit it. cat .git/refs/heads/main, cat .git/config, ls .git/refs/remotes/origin/ — five minutes of looking makes the whole CLI legible.

Try this

In a scratch repo, git cat-file -p HEAD, then git cat-file -p the tree SHA it prints and keep walking until you hit blobs. Then git checkout an old tag, commit once, and git reflog — watch where your commit went.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.