Refs, HEAD and the index
Last updated
Lesson 3.1 said a branch is a label on a commit. In this lesson you'll see that label for what it is: a tiny file holding a commit's hash. HEAD is a file too, and so is the staging area. With objects from 8.1, that's all of Git, and every command from Stages 1 to 3 fits in one picture.
By the end of this lesson you can
- Explain branches and tags as refs: names for hashes
- Read HEAD, and say what it holds on a branch and when detached
- Explain the index as a file listing the next commit's blobs
- Explain packed refs and reftable, and why you shouldn't edit .git by hand
A branch is a file with a hash in it
A ref is a name for a commit's hash. In a new repository each one is a small text file under .git/refs:
- A branch is
.git/refs/heads/<name>, holding the hash of its newest commit. - A tag is
.git/refs/tags/<name>, holding a commit's hash (or, for an annotated tag, the tag object's). - A remote-tracking branch is
.git/refs/remotes/origin/<name>, updated by fetch.
That's why branches are so cheap: making one writes 41 bytes. Committing on a branch writes the new commit's hash into its file, and that's all "moving the label" ever meant.
HEAD
.git/HEAD says where you are. On a branch, it doesn't hold a hash: it names the branch.
cat .git/HEAD ref: refs/heads/main
Git follows it: HEAD → refs/heads/main → a commit. When you commit, Git moves the branch, and HEAD comes along because it points to the branch. In detached HEAD (lesson 3.5), the file holds a commit's hash directly, so a new commit moves only HEAD, and nothing remembers it once you switch away.
The index
The staging area of lesson 1.5 is a file too: .git/index. It's a list of every path in the next commit, each with the hash of a blob. git add stores a blob for the file's current contents (lesson 8.1) and writes its hash into the index. git commit turns the index into trees, makes a commit pointing to the top tree, and moves the branch.
git ls-files --stage prints the index:
git ls-files --stage 100644 f1eeaa10c29b36e4c0d086bea0e06e56ef3d9fe9 0 adobo.md 100644 4bd45c2730e3f5ede852a5411c0113e31cffc970 0 sinigang.md
100644 is an ordinary file's mode; the 0 is the stage number, which is 1, 2 or 3 only during a merge conflict (lesson 3.4), when the index holds the base, your side and their side of a file.
Look inside .git
- List what’s in
.gitwithls .git - Print HEAD with
cat .git/HEAD - Print the desserts branch’s file,
.git/refs/heads/desserts - List every ref with
git show-ref - Switch to desserts, then print HEAD again
- Print the index with
git ls-files --stage
You are in ~/kusina on main. A branch called desserts has two commits that main doesn’t.
Commit graph
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Try git switch --detach HEAD~1 and print HEAD once more: now it holds a hash.
Packed refs and reftable
One file per ref is simple, but slow for repositories with thousands of branches and tags. Git has two other ways to store refs:
- Packed refs.
git gc(which Git runs by itself from time to time) moves refs into one file,.git/packed-refs, one hash and name a line. So.git/refs/heads/mainmay not exist, even though main does.git show-refreads both. - Reftable. A newer binary format that stores all refs, and their reflogs, in a few files, built for very large repositories. You can choose it for a new repository with
git init --ref-format=reftable. Git's BreakingChanges page lists it as a planned default for Git 3.0, which hasn't been released.
Either way, commands like git show-ref, git rev-parse and git branch give the same answers. Only the files differ.
Every command in one picture
A repository is objects (8.1), refs, HEAD and the index. Each command from the first three stages changes some of them:
| Command | Objects | Index | Refs | HEAD | Your files |
|---|---|---|---|---|---|
git add | new blob | updated | |||
git commit | new trees and a commit | branch moves | (follows the branch) | ||
git branch x | new ref | ||||
git switch x | from x’s tree | points to x | from x’s tree | ||
git merge (fast-forward) | updated | branch moves | updated | ||
git merge (two parents) | new trees and a commit | updated | branch moves | updated | |
git reset --soft | branch moves | ||||
git reset (mixed) | from the commit | branch moves | |||
git reset --hard | from the commit | branch moves | from the commit | ||
git tag -a | new tag object | new ref | |||
git fetch | new objects | remote-tracking refs move |
Read the reset rows again: the three kinds of reset from lesson 2.5 are just "how far down this table to go".
Spot the trap: editing .git by hand
Joaquin wants to move main back one commit. He’s just learned that a branch is a file, so he opens .git/refs/heads/main in his editor and pastes an older hash.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Pro Git: Git References: refs, HEAD and tags as files
- Git documentation: gitrepository-layout: what each file in .git is
- Git documentation: git-show-ref
- Git documentation: git-ls-files: --stage
- Git documentation: git-pack-refs: packed-refs
- Git documentation: reftable: the newer ref storage format
- Git documentation: BreakingChanges: reftable planned as the default in Git 3.0