3. Refs, HEAD & the DAG

Branches are text files. HEAD is a text file. That's most of Git.

So far the object store has given us a pile of immutable commits, all named by hash. But people don't type 40-character hashes at the prompt — they type main, feature/login, HEAD^. That layer of human names is built on top of the object store with something almost embarrassingly simple: a folder full of text files.

A branch is just a pointer

Every branch in Git is a single file inside .git/refs/heads/. The file is named after the branch, and its contents are… one line: the hash of the commit that branch currently points to.

$ ls .git/refs/heads/ main feature-login bugfix-42

$ cat .git/refs/heads/main 3e8f4c7a912b5d6e0f1a2b3c4d5e6f7a8b9c0d1e

That's the whole implementation of a branch. To "move" a branch forward after a commit, Git rewrites that one line to the new commit's hash. To delete a branch: rm .git/refs/heads/feature-login. That's what git branch -d feature-login does under the hood.

Branches are the cheapest thing in the universe

Creating a branch = writing 40 bytes into a new file. That's why Git workflows make branches the default unit — they cost nothing. The commits themselves already exist in the object store; the branch just points at one.

Tags are the same idea, with less mutation

Lightweight tags live in .git/refs/tags/ and are just pointers, same as branches. The only difference is that tags are not expected to move — a tag like v1.0 is supposed to forever name the same commit. Annotated tags (made with git tag -a) point to a special "tag object" in the object store that includes a message and signature, but lightweight tags are just text files with a hash.

Remote-tracking refs are the same, from far away

When you fetch from a remote, Git stores the remote's branches as refs under .git/refs/remotes/<remote>/. The file .git/refs/remotes/origin/main is your best knowledge of where origin's main was at last fetch. Same mechanism: a file with a hash in it.

HEAD is a pointer to a pointer

Open .git/HEAD and you'll see something like this:

$ cat .git/HEAD ref: refs/heads/main

That string — ref: refs/heads/main — is called a symbolic ref. It doesn't hold a commit hash; it holds the name of another ref. HEAD is usually a symbolic ref pointing at whatever branch you're currently on. When you make a commit, Git follows HEAD → refs/heads/main, reads that ref's commit as the parent, writes the new commit, and then updates refs/heads/main to point at the new commit.

"Being on a branch" literally means HEAD points to that branch. Nothing more.

Detached HEAD is HEAD pointing directly at a commit

If you check out a specific commit hash or a tag (git checkout v1.0), .git/HEAD stops being a symbolic ref and becomes a literal hash:

$ git checkout 3e8f4c7a Note: switching to '3e8f4c7a'. You are in 'detached HEAD' state…

$ cat .git/HEAD 3e8f4c7a912b5d6e0f1a2b3c4d5e6f7a8b9c0d1e

That's all "detached HEAD" means: HEAD contains a hash instead of a branch name. Commits you make in this state are real commits — they land in the object store — but no branch ref points at them. If you move HEAD somewhere else (by checking out a branch), those commits become unreachable by any named ref. They're still in the object store, just orphaned. We'll see how to find them again in the recovery article.

Attached HEAD

HEAD → refs/heads/main → <commit>. Commits advance the branch.

Detached HEAD

HEAD → <commit>. Commits go nowhere named. Orphan them and they're eventually garbage collected.

The commit graph is a DAG

Every commit points at zero, one, or more parents. Follow parent pointers from any commit and you trace backward through history. The whole collection of commits in a repository is a Directed Acyclic Graph — directed because edges go one way (child → parent), acyclic because a commit can never be its own ancestor.

Branches don't "contain" commits. They just point at one. The commits a branch "contains" are defined by reachability: start at the branch tip, follow parent pointers, and everything you can reach is part of that branch's history. The same commit can be reachable from many branches at once.

c0 c1 c2 c3 c4 f1 f2 b1 main feature bugfix HEAD

Three branches, three refs, one DAG. HEAD is currently pointing at feature. Commit c2 is reachable from all three branches (it's the fork point); it belongs to all of them.

Revision syntax — how to name any commit

Git lets you refer to commits with more than just hashes and branch names. The whole mini-language is documented under git help revisions; here are the ones worth committing to memory.

Cheat sheet

SyntaxWhat it means
HEADThe current commit.
HEAD^HEAD's first parent.
HEAD^^HEAD's grandparent.
HEAD~3Three commits back on the first-parent line.
HEAD^2HEAD's second parent (the merged-in side of a merge).
main..featureCommits reachable from feature but not from main — the "new on this branch" set.
main...featureSymmetric difference — commits in either but not both.
@{yesterday}HEAD as it was yesterday (from the reflog).
@{-1}The previous branch you were on.
3e8f4c7Any unique prefix of a hash — Git resolves it.

The two-dot and three-dot selectors are worth dwelling on, because they show up all over git log and git diff and mean different things in each.

Two dots: A..B

"Commits reachable from B but not A." Think of it as "what's new on B that isn't on A yet?" When you say git log main..feature, you're asking "what did I add on this feature branch?"

Three dots: A...B

"Commits reachable from either, but not both." The symmetric difference. Useful when you want to see what has happened on both sides of a fork.

How Git moves refs around

Almost every Git command is really a ref manipulation dressed up in porcelain:

CommandWhat it does to refs
git commitCreates a new commit object; updates the branch that HEAD points at.
git branch new-featureWrites a new file .git/refs/heads/new-feature containing HEAD's hash.
git checkout featureChanges HEAD to the symbolic ref ref: refs/heads/feature, updates working tree.
git merge featureCreates a merge commit (two parents); fast-forwards or advances the current branch.
git reset --hard XOverwrites the current branch's ref to point at X. Previous position kept in the reflog.
git push origin mainAsks origin to update its refs/heads/main to match ours, and uploads any missing objects.

When you see it this way, "non-fast-forward rejected" stops being mysterious. You asked the remote to point its main ref at a hash — but doing so would mean throwing away commits already reachable from the remote's current main. Git refuses unless you explicitly force it, because that's a destructive operation on a shared ref.

Packed refs — when there are too many files

If a repo has thousands of branches and tags (most of Linux's for example), storing each as a separate file gets slow. Git can batch them all into a single .git/packed-refs file:

# pack-refs with: peeled fully-peeled sorted 3e8f4c7a… refs/heads/main 1b2c3d4e… refs/heads/feature-login 7a9f31e2… refs/tags/v1.0 c3b8f4e1… refs/remotes/origin/main

When a ref is updated, Git may write the new value either as a loose ref (a file in .git/refs/...) or back into the packed file. Loose refs always take precedence if both exist. git pack-refs collapses the loose ones into the packed file; garbage collection does it for you periodically.

Try It Yourself — Ref Simulator

Build a commit graph step by step. Create branches, switch between them, and watch how each command is really just "create an object, move a ref." The .git/HEAD and .git/refs/heads/* panels update live.

Commit graph

Refs on disk

What to take away

  • Refs are text files under .git/refs/ containing a commit hash.
  • HEAD is usually a symbolic ref pointing at the current branch. Detached HEAD means it points directly at a commit.
  • Branches don't contain commits — they point at one, and "membership" is reachability via parent pointers.
  • Most Git commands boil down to "create some objects, move some refs." Once you see that, nothing in Git is magic.