How Git works · lesson 3 of 6
Commands and ideas in this lesson
A.git/refs/heads/maina branch, as a file
B.git/HEADwhich branch you're on
Cgit show-reflist every ref and its hash
Dgit ls-files --stagewhat's in the index

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.

Terminal
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:

Terminal
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

Git sandbox
  1. List what’s in .git with ls .git
  2. Print HEAD with cat .git/HEAD
  3. Print the desserts branch’s file, .git/refs/heads/desserts
  4. List every ref with git show-ref
  5. Switch to desserts, then print HEAD again
  6. 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

  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe
  11. 1f359a2 Add sinigang recipe
  12. 601c654 Add 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/main may not exist, even though main does. git show-ref reads 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:

What each command changes
CommandObjectsIndexRefsHEADYour files
git addnew blobupdated
git commitnew trees and a commitbranch moves(follows the branch)
git branch xnew ref
git switch xfrom x’s treepoints to xfrom x’s tree
git merge (fast-forward)updatedbranch movesupdated
git merge (two parents)new trees and a commitupdatedbranch movesupdated
git reset --softbranch moves
git reset (mixed)from the commitbranch moves
git reset --hardfrom the commitbranch movesfrom the commit
git tag -anew tag objectnew ref
git fetchnew objectsremote-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

Spot the trapJoaquin

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

What is in .git/refs/heads/main?
You’re on main. What does .git/HEAD hold?
What does git add adobo.md change?

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.

All sources for Simple Git