HEAD and refs
Overview
Commits are named by long hashes, which nobody wants to type. References (refs) give them human-friendly names. Branches, tags and the special name HEAD are all refs — and once you see them as simple pointers, a lot of Git’s behaviour makes sense.
Branches are pointers
A branch is just a name that points to one commit — the tip of that line of work:
git rev-parse main
# c91e5a0f3b8d4e2a1c7f9b6d5e4a3c2b1f0e9d8cWhen you make a commit on main, Git creates the commit (with the current tip as its parent) and then moves main forward to point at the new commit. That’s all “committing to a branch” means.
Refs are organised like paths:
| Ref | What it is |
|---|---|
refs/heads/main |
the local branch main |
refs/tags/v1.0 |
the tag v1.0 |
refs/remotes/origin/main |
your copy of main on the remote origin (chapter 7) |
You normally use the short names (main, v1.0, origin/main) and Git finds the full ref.
HEAD: where you are
HEAD answers the question “what am I working on?”. Normally it doesn’t point at a commit directly — it points at a branch:
git symbolic-ref HEAD
# refs/heads/mainSo HEAD → main → commit c9. When you commit, main moves, and HEAD comes along because it points to main. When you switch branches, HEAD changes to point at the other branch.
Tags: pointers that don’t move
A tag also names a commit, but it’s meant to stay put — to mark a release, for example:
git tag v0.1 HEAD~2 # lightweight tag: just a name
git tag -a v1.0 -m "First public release" # annotated tag: also stores a message, tagger and date
git tag # list tagsUse annotated tags for releases: they record who tagged what and when, and can be signed. Tags and releases in chapter 10 covers them fully.
Detached HEAD
You can point HEAD directly at a commit instead of a branch — to look at an old version, or to test a tag:
git switch --detach v0.1
# or: git checkout v0.1HEAD is now at 4f1a2b3 Version 1git status says:
HEAD detached at v0.1This is the detached HEAD state. Nothing is wrong — you can look around, build and run the old version. But be aware:
- If you make commits now, there’s no branch pointing at them. Only
HEADdoes. - When you switch back to
main, nothing points to those commits any more. They’re not deleted immediately, but they’re hard to find, and Git will eventually clean them up.
Git warns you when you leave:
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
9b8c7d6 Try a faster version 1Keeping work from a detached HEAD
Before switching away, give the commits a branch:
git switch -c experiment # create a branch here and switch to itNow experiment points at your new commit and it’s safe. If you’ve already switched away, the commit ID from the warning (or git reflog, coming up in Recovering with reflog) gets it back: git branch experiment 9b8c7d6.
Getting out without keeping anything
git switch main # or: git switch - (the previous branch)Where refs are stored
Traditionally, each ref is a tiny text file containing a commit ID — .git/refs/heads/main — and .git/HEAD contains ref: refs/heads/main. Repositories with many refs also use a combined packed-refs file, and newer repositories may use the reftable format (the default from Git 3.0), which stores refs in a single efficient database.
Because the storage varies, use commands rather than reading files:
git symbolic-ref HEAD # where HEAD points
git rev-parse main # the commit a ref points to
git for-each-ref # list every ref
git show-ref --tags # just the tagsRelative names revisited
Now you can read expressions like main~3 precisely: “start at the commit main points to, then follow the first parent three times”. HEAD~1 is “the parent of where I am”. These work with any ref: v1.0~2, origin/main~1.
The model so far: objects (commits, trees, blobs) never change; refs (branches, tags, HEAD) are names that point at commits, and branches move when you commit. The rest of Git is built from these two ideas.
Visit the past and bring something back
The script creates three commits on main. (1) Show what HEAD points to and which commit main points to. (2) Tag the first commit as v0.1 with an annotated tag. (3) Check out the v0.1 tag — you are now in detached HEAD state — and confirm it. (4) Make an experimental commit there. (5) Save that experiment on a new branch experiment before going back to main, so it isn’t lost. Check that main still has three commits and experiment has two.
mkdir refs && cd refs && git init -b main
for v in 1 2 3; do echo "version $v" > app.txt; git add app.txt; git commit -qm "Version $v"; done
# 1. What does HEAD point to? And main?
# 2. Annotated tag on the first commit
# 3. Go to v0.1 (detached HEAD)
# 4. Commit an experiment
# 5. Keep it on a branch, return to main, check bothmkdir refs && cd refs && git init -b main
for v in 1 2 3; do echo "version $v" > app.txt; git add app.txt; git commit -qm "Version $v"; done
# 1. What does HEAD point to? And main?
git symbolic-ref HEAD # refs/heads/main
git rev-parse main # the ID of "Version 3"
# 2. Annotated tag on the first commit
git tag -a v0.1 -m "First prototype" HEAD~2
# 3. Go to v0.1 (detached HEAD)
git switch --detach v0.1
git status # HEAD detached at v0.1
# 4. Commit an experiment
echo "version 1, but faster" > app.txt
git commit -qam "Try a faster version 1"
# 5. Keep it on a branch, return to main, check both
git switch -c experiment # a branch now points at the new commit
git switch main
git log --oneline main # 3 commits
git log --oneline experiment # 2 commits: Version 1, Try a faster version 1Summary
- A ref is a name that points to a commit: branches live under
refs/heads/, tags underrefs/tags/. - HEAD is the ref for “where you are now” — normally it points to a branch, which points to a commit.
- When you commit, the branch HEAD points to moves forward to the new commit.
- Detached HEAD means HEAD points straight at a commit; new commits there belong to no branch.
- To keep work from a detached HEAD, create a branch with
git switch -c <name>before switching away.