Sub topic 9
Branches and HEAD as pointers to commits
This chapter assumes you know the commit, tree, and blob objects from earlier chapters and can use a Unix-style shell. It shows that a branch is just a small file naming a commit, and that HEAD names the branch you are on.
A branch is a file holding a commit ID
Objects never change, so Git needs something that can change to mark "the latest commit on this line of work". That something is a reference (ref). A branch ref is a plain text file under .git/refs/heads/. Its content is one commit ID.
git init -b main refs-demo && cd refs-demo
git config user.name "Demo User"
git config user.email "[email protected]"
echo one > a.txt
git add a.txt
git commit -m "one"
cat .git/refs/heads/main
git rev-parse mainBoth of the last two commands print the same 40-character ID. The git rev-parse command resolves any name to an object ID.
HEAD is a pointer to a branch
HEAD records what you have checked out. Normally it is a symbolic ref, a file that holds the name of another ref instead of an ID.
cat .git/HEAD
git symbolic-ref HEAD
git rev-parse HEADThe first command prints ref: refs/heads/main. So HEAD points to main, and main points to a commit. Git follows both links when you ask for HEAD.
Commits point to their parents. Refs point to commits. Only the refs move.
Creating and moving branches
Creating a branch writes one new small file. No files are copied.
git branch topic
cat .git/refs/heads/topic
echo two >> a.txt
git commit -am "two"
git rev-parse main topictopic still holds the ID of the first commit. main holds the ID of the new one. The commit command did three things:
- It wrote new blob, tree, and commit objects.
- It set the new commit's parent to the old
HEADcommit. - It updated the branch that
HEADnames, somainmoved forward.
You can make the same move by hand with git update-ref refs/heads/topic main.
Switching branches and detached HEAD
Switching branches rewrites HEAD and updates your working files to match the new commit.
git switch topic
cat .git/HEAD
git switch --detach main
cat .git/HEADAfter the first switch, HEAD reads ref: refs/heads/topic. After --detach, the file holds a raw commit ID. This state is called a detached HEAD. New commits you make here are not recorded by any branch. If you switch away, only the reflog or a saved ID will lead you back to them.
Gotchas
- Packed refs.
git pack-refs --all(andgit gc) can move ref files into.git/packed-refs. Thencat .git/refs/heads/mainmay fail even though the branch exists. Usegit rev-parse maininstead of reading files directly. - Default branch name. Older repositories may use
master. Usegit symbolic-ref --short HEADin scripts instead of assuming a name. - Deleting a branch removes only the ref. The commits stay in the object database until garbage collection finds them unreachable.
- Tags are refs too. They live under
refs/tags/, and the next chapters cover how they differ from branches.
Exercise
You run git branch feature while on main, then commit once on main. What do .git/refs/heads/feature and .git/refs/heads/main contain, and what does .git/HEAD contain?
Show answer
feature still holds the ID of the commit that main pointed to before the new commit. main holds the ID of the new commit. .git/HEAD still reads ref: refs/heads/main, because creating a branch does not switch to it.
Exercise
Why does a commit change a branch file but never change an existing commit object?
Show answer
A commit's ID is a hash of its content, so changing a commit would give it a new ID. Objects are therefore immutable. A branch is a ref, a mutable file, so Git updates it to the ID of each new commit.