Sub topic 2
Branches and HEAD as Moving Pointers
This chapter assumes you can commit, create branches, and switch between them. It also assumes you can read git log --oneline output. The previous chapter covered commits and parents. Here you will see that a branch is only a label on one commit, and that this explains what rebase does to a branch.
A branch is a file holding one hash
A branch is not a container of commits. It is a named pointer to a single commit. Git stores it as a small file containing that commit's hash. The branch's history is whatever you reach by following parent links back from that commit.
Run this in a scratch directory to see it:
git init -b main pointers-demo
cd pointers-demo
git commit --allow-empty -m "A"
git commit --allow-empty -m "B"
git switch -c feature
git commit --allow-empty -m "C"
cat .git/HEAD
cat .git/refs/heads/feature
git log --oneline --decoratecat .git/HEAD prints ref: refs/heads/feature. The next command prints the 40-character hash of commit C. The log shows HEAD -> feature beside C and main beside B. Your hashes will differ from anyone else's.
HEAD is a pointer to a pointer
HEAD marks where you are now. Normally it holds the name of a branch, not a hash. Git resolves it in two steps:
HEADnames the branchfeature.featurenames commit C.
The diagram below shows this chain.
Arrows from commits point to parents, because a commit records its parent and never its children.
What moves what
Most Git commands work by moving one of these pointers. Commits themselves never change.
| Command | What moves |
|---|---|
| `git commit` | The current branch advances to the new commit. `HEAD` follows because it names that branch. |
| `git switch main` | `HEAD` now names `main`. No branch moves. |
| `git switch -c topic` | A new branch is created at the current commit, and `HEAD` names it. |
| `git reset --hard <commit>` | The current branch is forced to point at `<commit>`. |
| `git rebase origin/main` | The current branch ends up pointing at newly created commits. |
The last row is the main idea of this tutorial. A rebase does not edit your old commits. It creates new ones and then moves the branch label onto them. The old commits still exist in the repository, but no branch points at them any more.
Detached HEAD
HEAD can also hold a commit hash directly. This is a detached HEAD.
git switch --detach HEAD~1
git log --oneline --decorateThe log now shows HEAD on its own, with no branch name. If you commit in this state, no branch advances. Once you switch away, nothing named points at those commits.
Rebase uses this state internally. It checks out the new base with a detached HEAD, applies your commits one at a time, and only at the end moves the branch to the final result. This is why git status during a conflicted rebase says you are rebasing and does not show your usual branch as checked out. It is also why git rebase --abort can put the branch back untouched: the branch label was never moved.
Common mistake
Learners often think git switch feature "loads" a copy of the branch. It only changes what HEAD names, then updates your working files to match that commit. Nothing is copied.
Exercise
You are on feature, which points at commit C. You run git reset --hard HEAD~1. Which pointers change, and what happens to commit C?
Show answer
Only the feature branch pointer changes, because HEAD names feature. It moves from C to C's parent. Commit C is not deleted. It still exists but no branch points at it, so you can recover it from the reflog.