Sub topic 9
Refs and HEAD: names for commits
A commit hash is accurate but hard to remember. Git adds a second layer on top of the object database that gives hashes human-readable names. These names are called refs. This chapter shows what refs are on disk, how HEAD differs from other refs, and how to create and move them with plumbing commands.
This chapter assumes you have read the earlier chapters on the .git directory and commit objects. You should know that a commit is an object that holds a tree hash, parent hashes and metadata. You should also be comfortable with git init, git commit and git branch.
What a ref is
A ref (short for reference) is a name that points to an object hash. Most refs are plain text files under .git/refs. Each file holds one hash and a newline. Nothing else is stored in it.
The directory a ref lives in tells Git what kind of name it is:
| Path | Meaning |
|---|---|
| `refs/heads/<name>` | A local branch |
| `refs/tags/<name>` | A tag |
| `refs/remotes/<remote>/<name>` | A remote-tracking branch |
| `refs/stash` | The most recent stash |
A branch is therefore not a container of commits. It is a file that names one commit, the tip of that line of history. The rest of the history is found by following parent links from that commit.
Reading refs
Create a small repository with two empty commits. The -b main option to git init needs Git 2.28 or later.
git init -b main refs-demo
cd refs-demo
git config user.name "Demo User"
git config user.email "[email protected]"
git commit --allow-empty -m "first"
git commit --allow-empty -m "second"Now look at the branch directly and through Git:
cat .git/refs/heads/main
git rev-parse main
git show-refThe first two commands print the same 40-character hash. (In a repository that uses SHA-256, the hash is 64 characters.) git rev-parse is the plumbing command that turns any name or revision expression into a full hash. git show-ref lists every ref with its hash.
HEAD is a ref that usually points to another ref
HEAD records what you currently have checked out. It lives at .git/HEAD, not under refs/. Normally it holds the name of a branch, not a hash:
cat .git/HEAD
git symbolic-ref HEAD
git symbolic-ref --short HEAD
git rev-parse HEADThe first command prints ref: refs/heads/main. This is a symbolic ref, a ref whose content is the name of another ref. Git follows the chain: HEAD points to refs/heads/main, which points to a commit. When you run git commit, Git creates the new commit and then moves the branch that HEAD names. HEAD itself stays the same.
Detached HEAD
HEAD can also hold a hash directly. This state is called a detached HEAD. Try it:
git checkout --detach main~1
cat .git/HEAD
git symbolic-ref HEAD.git/HEAD now contains a bare hash. The symbolic-ref command fails with fatal: ref HEAD is not a symbolic ref. This failure is useful in scripts because it tells you whether you are on a branch. If you commit in this state, no branch moves. The new commit is named only by HEAD, and it becomes hard to find once you switch away.
Return to the branch before continuing:
git switch mainCreating and moving refs with update-ref
git branch and git tag are porcelain commands. The plumbing command underneath is git update-ref. Its form is git update-ref <ref> <new-value> [<old-value>]. The new value can be a hash or any revision expression.
git update-ref refs/heads/topic main~1
git branch --list
cat .git/refs/heads/topicThis creates a branch named topic that points to the first commit. It works exactly as git branch topic main~1 would.
Compare-and-swap with the old value
The optional third argument makes the update conditional. Git changes the ref only if it currently holds the old value you gave. This protects against two processes moving the same ref at once.
old=$(git rev-parse topic)
git update-ref refs/heads/topic main "$old"
git rev-parse topic mainThe update succeeds and topic now equals main. If you repeat the command with a stale $old, it fails and leaves the ref alone:
git update-ref refs/heads/topic main~1 "$old"Git reports that it cannot lock the ref because it is at a different value than expected. The value $old is the first commit, but topic already moved, so the expected value no longer matches.
Deleting a ref
git update-ref -d refs/heads/topic
git branch --listDeleting a ref removes only the name. The commit object is still in the object database, and any other ref that reaches it keeps it reachable.
Plumbing does not protect you from every mistake
update-ref accepts any ref path and does little checking of what the ref means. For example, if you run git update-ref refs/tags/oops main and later decide you meant a branch, nothing will warn you. Git does refuse to write an object that is not a commit into refs/heads/, so a branch cannot point to a blob or tree. A tag has no such restriction and may point to any object.
Changing HEAD with symbolic-ref
git symbolic-ref also writes. git symbolic-ref HEAD refs/heads/other changes which branch HEAD names. It does not touch the index or the working tree. If the new branch has a different tree, git status will report the difference as staged and unstaged changes, which is rarely what you want. Use git switch for everyday work. symbolic-ref is the right tool in the hand-built commit workflow that comes later in this tutorial, when you must point HEAD at a branch that has no commits yet.
Revision syntax
Plumbing commands accept more than names. The common suffixes work with git rev-parse and git cat-file:
| Expression | Meaning |
|---|---|
| `main^` | The first parent of `main` |
| `main^2` | The second parent, which exists for merge commits |
| `main~2` | Two first-parent steps back |
| `main^{tree}` | The tree object of the commit |
| `v0.1^{commit}` | Follow a tag until a commit is reached |
Try one:
git cat-file -p 'main^{tree}'Quote the braces so your shell does not try to expand them.
Name resolution and ambiguity
When you write main, Git searches several places in a fixed order. Among them are .git/main, refs/main, refs/tags/main, refs/heads/main and refs/remotes/main. The first match wins. If a tag and a branch have the same name, the tag wins, and Git prints a warning that the name is ambiguous. In scripts, use full names such as refs/heads/main to avoid surprises.
Packed refs
A repository with thousands of tags would need thousands of small files. Git can store them in one file instead, .git/packed-refs.
git tag v0.1 main~1
git pack-refs --all
find .git/refs -type f
cat .git/packed-refsAfter packing, the loose files for branches and tags disappear. Their names and hashes are now lines in packed-refs. Git looks for a loose file first and falls back to the packed file. When you update a packed ref, Git writes a new loose file, and that file takes priority over the packed line. HEAD is never packed.
Because of this, never assume that a missing file under .git/refs/heads means a branch does not exist. Ask Git with git show-ref or git rev-parse --verify.
The reflog
Each time a ref moves, Git can record the change in a log under .git/logs. This reflog is local, is not shared with other repositories, and expires after a time.
git reflog show HEAD
cat .git/logs/HEADThe reflog is why a commit you lost by moving a branch is usually still recoverable. The old hash appears in the log, and git update-ref or git switch -c rescue <hash> gives it a name again. You can add your own message to an entry with git update-ref -m "reason" <ref> <value>.
Common errors
- Editing
.git/HEADor ref files by hand with a wrong hash. Git then reportsfatal: bad object. Useupdate-refinstead, because it locks the ref and checks the value. - Writing a short hash into a ref file. A ref file needs the full hash.
- Confusing
HEAD~1andHEAD^. On a linear history they match. On a merge commitHEAD^2is the merged-in side, andHEAD~2is the grandparent on the first-parent line. - Committing on a detached HEAD and then switching away. The commit is not lost immediately. Find its hash with
git reflogand create a branch for it.
Exercise
You run git checkout --detach HEAD~1 and then cat .git/HEAD. What does the file contain, and what does git symbolic-ref HEAD do? Then explain which command moves the main branch to the commit you are on, without using git branch or git reset.
Show answer
.git/HEAD contains a bare 40-character commit hash, not a line starting with ref:. git symbolic-ref HEAD fails with "fatal: ref HEAD is not a symbolic ref", because HEAD no longer names a branch. To move main to the current commit, run git update-ref refs/heads/main HEAD. To make HEAD follow the branch again, run git switch main.