Refs, branches and HEAD

Intermediate · 8 min

A branch is a 41-byte file. Look at .git/refs, HEAD as a symbolic ref, detached HEAD, packed-refs, and the reflog files, using only cat and find.

Before thisObjects, blobs, trees and commitsWhat a branch is

CodeLucky · 8 min

Refs, branches and HEAD

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodeLuckyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A ref is a file whose content is a commit hash: branches live in `.git/refs/heads/`, tags in `.git/refs/tags/` and remote-tracking branches in `.git/refs/remotes/`.
  • `HEAD` is normally a symbolic ref, a file containing `ref: refs/heads/<branch>`, so committing moves the branch that `HEAD` names.
  • A detached `HEAD` contains a raw commit hash, and commits made there are on no branch, so switching away leaves them reachable only by hash or reflog.
  • `git pack-refs` moves loose ref files into `.git/packed-refs`, and Git reads both, with a loose file taking precedence.
  • `git update-ref`, `git symbolic-ref` and `git rev-parse` are the plumbing commands for creating, reading and resolving refs.

Objects never move; names do

The last lesson showed that commits are immutable objects. If a branch is “a line of commits”, something must mark where the line currently ends and change as you commit. That is a ref: a name that points at a commit hash.

Everything below is from a real jot repository with three commits, a branch search, and a tag v1, on Git 2.54. Hashes will differ for you.

A branch is a file

$ cat .git/refs/heads/main
66dc92b2f4b39458e5d48802b339641e556840c4
$ cat .git/refs/heads/search
66dc92b2f4b39458e5d48802b339641e556840c4
$ find .git/refs -type f | sort
.git/refs/heads/main
.git/refs/heads/search
.git/refs/tags/v1

That is the whole branch. 40 hex digits and a newline. git branch search created a new file with the current commit’s hash, nothing more, which is why branching is instant and costs almost no space. Committing on search rewrites that one file with the new hash. A tag is the same kind of file in refs/tags, but nothing rewrites it. Both branches here point to the same commit because I created search from main and made no commits on either.

HEAD is a pointer to a pointer

$ cat .git/HEAD
ref: refs/heads/main
$ git switch -q search
$ cat .git/HEAD
ref: refs/heads/search
$ git symbolic-ref HEAD
refs/heads/search
$ git symbolic-ref --short HEAD
search

HEAD normally isn’t a hash. It is a symbolic ref that says “I mean whatever search points to”. When you commit, Git creates the commit, then updates the branch that HEAD names. Switching branches just edits this one line (and updates your files).

Detached HEAD

Check out a commit directly rather than a branch, and the line changes shape:

$ git switch -q --detach HEAD~1
$ cat .git/HEAD
5b8902e3d5fb101ffd76522caea5520ac27a6b91
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref
$ git status | head -1
HEAD detached at 5b8902e

Now HEAD holds a raw hash. That is fine for looking around, running an old version, or a bisect. The risk comes from committing there:

$ git commit --allow-empty -m "detached work"
$ git switch main
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  6279ae2 detached work

No branch points at 6279ae2. Nothing is deleted, but you must remember the hash or find it in the reflog. The warning even tells you to create a branch (git branch <name> 6279ae2) to keep it. A detached HEAD is the same as a branch you haven’t given a name yet.

Making and listing refs by hand

$ git update-ref refs/heads/hand main~2
$ git branch -v
  hand   ffe5734 Note 1
* main   66dc92b Note 3
  search 66dc92b Note 3
$ git show-ref
ffe573410d07... refs/heads/hand
66dc92b2f4b3... refs/heads/main
66dc92b2f4b3... refs/heads/search
66dc92b2f4b3... refs/tags/v1

update-ref wrote a branch file. git branch and git switch -c do essentially this plus bookkeeping. main~2 and HEAD~1 are expressions that git rev-parse resolves to hashes. The trailing ~N means “N first-parents back”, and ^ is the parent.

Packed refs

A repository with thousands of branches and tags would have thousands of tiny files. Git can pack them:

$ git pack-refs --all
$ find .git/refs -type f | sort
$ cat .git/packed-refs
# pack-refs with: peeled fully-peeled sorted
ffe573410d07... refs/heads/hand
66dc92b2f4b3... refs/heads/main
...

The loose files are gone, and one text file lists them all. Branches still work. After packing I made a commit on main, and Git wrote a fresh loose .git/refs/heads/main that now overrides the packed entry. The loose file wins, and a later git gc packs it again. This is why you can’t assume a branch has a file. Git 2.54 still defaults to this “files” ref format, which I confirmed with git rev-parse --show-ref-format. A table-based “reftable” format exists as an opt-in.

The reflog files

.git/logs/HEAD and .git/logs/refs/heads/<branch> are the files behind git reflog. One line per move, with old hash, new hash, who, when and why. That log, and how it saves you, is covered in the reflog and recovering lost work.

What the video covers

CodeLucky’s “Git Internals: Demystifying References, Branches & HEAD” is about eight minutes and was published around 2025. From its title it covers refs, branches and HEAD. I judge English from the title, description and channel only, and I could not read a transcript. It comes from the same series as the objects video, so the two follow on. A longer tour of .git/ from SRKMasterStack is linked as a runner-up.

Where this fits

Objects and refs are the two halves of the database. Next, how Git keeps that database small: packfiles, garbage collection and repo size.

Check yourself

  1. 01What is inside the file `.git/refs/heads/main`?

    The 40-character hash of the commit at the tip of `main`, followed by a newline.

  2. 02How does Git know which branch you are on?

    `.git/HEAD` contains `ref: refs/heads/<branch>`, a symbolic reference to the branch.

  3. 03What does a detached HEAD look like in `.git/HEAD`, and what is the danger?

    It holds a raw commit hash, not a branch name. Commits you make aren't on any branch, so after switching away they can only be found by their hash or in the reflog.

  4. 04You can't find `.git/refs/heads/feature` after cloning, yet the branch exists. Why?

    Refs can be packed into `.git/packed-refs`, so the loose file may not exist.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark