Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2 · Branches are just files: refs, HEAD and the reflog

In one sentence: a branch is a 41-byte file holding one commit id, HEAD says which of those files you’re on, and every time one of them moves, git writes the old and new id in a log, so moving a branch doesn’t delete a commit.

The problem it solves

Lesson 1 left the store full of commits that are never edited. But you don’t type ids: you type main, HEAD, HEAD~2. And after a git reset --hard HEAD~2, git log shows one commit where there were three:

8de11ef one

If reset doesn’t remove objects, where did two and three go? Nowhere. What changed is a name.

How it works

A ref is a file holding an id. A branch called main is the file .git/refs/heads/main:

cat .git/refs/heads/main
wc -c < .git/refs/heads/main
785c39812d3433a188dc61c2616b1dd81e00dce3
      41

40 hex characters and a newline. That’s the whole branch. A commit made on main writes a new commit object, then rewrites this file with the new id. git log starts from the id in the file and follows parent lines backwards. A commit is “on” a branch only if you can reach it that way.

RefFileMoves when
branch main.git/refs/heads/mainyou commit, merge, reset or rebase on it
tag v1.git/refs/tags/v1never, unless forced
remote-tracking origin/main.git/refs/remotes/origin/main (a clone writes the remote’s branches as lines in packed-refs, below)you fetch or push

git branch feature writes one more file with the same id in it. Nothing is copied.

HEAD is a ref to a ref. .git/HEAD normally holds a line naming the current branch, not an id:

ref: refs/heads/main

git switch feature rewrites that line to ref: refs/heads/feature (and updates your working folder). When you commit, git follows HEAD to the branch file and moves that.

reset --hard rewrites the branch file. That’s all it does to history: after git reset --hard HEAD~2,

8de11ef710973cb3b8a3e5f0e24fb4379ebb64e3

is in .git/refs/heads/main, and the old tip is still in the store:

git cat-file -t 785c39812d3433a188dc61c2616b1dd81e00dce3
commit

(In this repo feature also still points at it, but the commit would be there even without that.)

The reflog: every place a ref has been

Each time a ref moves, git appends one line to its reflog, a plain text file under .git/logs/:

git reflog main
8de11ef main@{0}: reset: moving to HEAD~2
785c398 main@{1}: commit: three
6c13f26 main@{2}: commit: two
8de11ef main@{3}: commit (initial): one

main@{1} means “where main was one move ago”. So undoing the reset is one command:

git reset --hard main@{1}
git log --oneline
785c398 three
6c13f26 two
8de11ef one

HEAD has its own reflog, which also records every switch between branches. It’s the one to read when you don’t remember which branch you were on:

git reflog
785c398 HEAD@{0}: reset: moving to main@{1}
8de11ef HEAD@{1}: reset: moving to HEAD~2
785c398 HEAD@{2}: checkout: moving from feature to main
785c398 HEAD@{3}: checkout: moving from main to feature
785c398 HEAD@{4}: commit: three
6c13f26 HEAD@{5}: commit: two
8de11ef HEAD@{6}: commit (initial): one

Reflogs are local. They live in your .git and are never pushed or fetched. A fresh clone’s reflog has one line, clone: from ..., and nothing from anyone else’s history. (git-reflog) They also don’t last forever; lesson 3 covers when entries expire.

Detached HEAD

Check out a commit rather than a branch and .git/HEAD holds the id itself:

git checkout --detach HEAD
cat .git/HEAD
HEAD is now at 785c398 three
785c39812d3433a188dc61c2616b1dd81e00dce3

Commits you make now move HEAD and no branch. When you switch away, the only record of them is HEAD’s reflog: the classic way to “lose” work.

Try it on your computer

Practical 0 · A commit with no git commit: build blobs, trees and commits by hand, make a branch by writing its ref, then corrupt an object on purpose and read what git fsck says.

Go deeper: packed-refs

A repository with thousands of tags would mean thousands of tiny files. git pack-refs --all (which git gc runs) moves refs into one file, .git/packed-refs, and removes the loose files:

# pack-refs with: peeled fully-peeled sorted 
785c39812d3433a188dc61c2616b1dd81e00dce3 refs/heads/feature
8de11ef710973cb3b8a3e5f0e24fb4379ebb64e3 refs/heads/main

After that .git/refs/heads is empty, yet git rev-parse main still works. When the branch moves again, git writes a loose file, and the loose file wins over the stale line in packed-refs:

grep main .git/packed-refs
cat .git/refs/heads/main
git rev-parse main
8de11ef710973cb3b8a3e5f0e24fb4379ebb64e3 refs/heads/main
785c39812d3433a188dc61c2616b1dd81e00dce3
785c39812d3433a188dc61c2616b1dd81e00dce3

So never read a branch with cat: ask git rev-parse. (git-pack-refs)

Go deeper: the reflog file

git reflog formats .git/logs/refs/heads/main, which is one line per move: old id, new id, who, when (seconds and time zone), a tab, and why:

0000000000000000000000000000000000000000 8de11ef710973cb3b8a3e5f0e24fb4379ebb64e3 Ada <ada@example.com> 1767261600 +0000	commit (initial): one
...
785c39812d3433a188dc61c2616b1dd81e00dce3 8de11ef710973cb3b8a3e5f0e24fb4379ebb64e3 Ada <ada@example.com> 1767261600 +0000	reset: moving to HEAD~2

40 zeros means “the ref didn’t exist yet”. Because each line has a time, you can also ask by date: main@{yesterday}, main@{2.hours.ago} (gitrevisions). Deleting a branch deletes its reflog file too, which is why HEAD’s reflog is the one that saves you after git branch -D (lesson 3).

Common misunderstandings

  • “git reset --hard deletes commits.” It moves a branch. The commits stay in the store; only their name goes. It does throw away uncommitted edits in your working folder: edits you never staged were never objects and are gone, but edits you git added are blobs in the store, and lesson 3 gets them back.
  • “A branch contains its commits.” It contains one id. “The commits on a branch” is everything reachable from that id through parent lines.
  • “Detached HEAD is an error.” It’s a normal state: HEAD holds an id instead of a branch name. The only risk is committing there and switching away without making a branch.

Check yourself

1. You ran git reset --hard HEAD~3 on main and want it undone. What do you type, and why does it work? git reset --hard main@{1} (or HEAD@{1} if you made no other move since). The reset added a line to main's reflog whose old id is the tip you had; @{1} names it. The commits were never removed, so pointing the branch back is enough.
2. A teammate deleted a remote branch and asks you to look in your reflog for it. You cloned this morning and never checked that branch out. Can you help? Not from your reflog: reflogs are local, and yours records only what your own refs did. But if the branch existed when you cloned, your origin/<branch> still points at the commit the branch had when you cloned (or last fetched), until a git fetch --prune removes it. Every teammate who fetched it has the same origin/<branch>.
3. cat .git/refs/heads/release says "No such file", but git log release works. What's going on? The ref was packed: it's a line in .git/packed-refs. Git reads the loose file first and falls back to packed-refs, so the branch exists; there's just no loose file for it.

Next: Practical 0 · A commit with no git commit, then lesson 3 · Getting lost work back, and when it really is gone.