Sub topic 9
Refs and HEAD: names for commits
This chapter assumes you know how to make a commit with git commit and that you have seen blobs, trees and commits as objects. A commit is an object with a 40-character hash, and nobody wants to type that hash. Refs give those hashes readable names.
What a ref is
A ref is a name that holds one object hash. Branches live under refs/heads/, and tags live under refs/tags/. Each ref is usually a small file whose content is a hash and a newline. Moving a branch means overwriting that file with another hash.
HEAD is a special ref in .git/HEAD. It usually holds the name of a branch instead of a hash. This is called a symbolic ref.
Create a branch with plumbing
The commands below create a repository with one commit and no porcelain commands except init. They need user.name and user.email to be set. The commit uses the empty tree, so no files are involved yet.
git init -b main refs-demo
cd refs-demo
tree=$(git mktree </dev/null)
commit=$(echo 'root' | git commit-tree "$tree")
git update-ref refs/heads/main "$commit"
cat .git/HEAD
cat .git/refs/heads/main
git rev-parse HEADgit update-ref writes the hash into the ref file. It is safer than editing the file yourself, because it takes a lock and can record a reflog entry. git rev-parse HEAD follows HEAD to the branch and then to the hash, so its output matches the content of .git/refs/heads/main.
Switch and detach HEAD
Changing the branch means rewriting .git/HEAD. The plumbing commands for this are symbolic-ref and update-ref.
git update-ref refs/heads/topic "$commit"
git symbolic-ref HEAD refs/heads/topic
cat .git/HEAD
git symbolic-ref --short HEAD
git update-ref --no-deref HEAD "$commit"
cat .git/HEAD
git symbolic-ref HEAD || echo "HEAD is detached"
git symbolic-ref HEAD refs/heads/main
cat .git/HEADAfter the first block, .git/HEAD contains ref: refs/heads/topic. The --no-deref option tells update-ref to overwrite HEAD itself instead of the branch it points to. HEAD then holds a raw hash, which is a detached HEAD. git symbolic-ref HEAD fails in that state, which is how scripts can detect it. The last command reattaches HEAD to main.
Listing refs
git for-each-ref
git show-refBoth commands list every ref with its hash and type. Do not rely on ls .git/refs for a full list. After git gc or git pack-refs, Git moves many refs into the single file .git/packed-refs. The ref files disappear, and the commands above still find them.
Common errors
- Editing
.git/HEADby hand with a branch that does not exist. Git then treats the branch as unborn, andgit logreports that the branch has no commits. Create the ref first withupdate-ref. - Assuming a branch is a copy of history. A branch is one hash. Deleting it with
git update-ref -d refs/heads/topicremoves the name and leaves the commits in the object database untilgcprunes them. - Forgetting
--no-deref.git update-ref HEAD <hash>whileHEADis symbolic moves the branch, notHEAD.
Exercise
You run git update-ref HEAD <hash> while .git/HEAD contains ref: refs/heads/main. What changes, and which option would change .git/HEAD itself?
Show answer
The command follows the symbolic ref and updates refs/heads/main to the new hash, so .git/HEAD keeps its text. Adding --no-deref makes update-ref overwrite HEAD directly, which detaches it.