Sub topic 1
Branches Are Just Pointers
Prerequisites
This chapter assumes you can do the following:
- Use
git add,git commit,git switch, andgit logwithout looking them up. - Run
git rebaseon a single branch. - Push a branch to a remote and open a pull request.
- Use Git 2.38 or newer. Check with
git --version.
You do not need to know how Git stores objects internally. This chapter covers the part you need: what a branch is, and why that matters when you rebase.
Why start here
Everything later in this tutorial depends on one fact. A branch is a small file that holds one commit ID. It is not a container of commits, and it does not remember which commits "belong" to it.
Once that is clear, stacked branches make sense. So does the main problem of this tutorial: a rebase moves only the branch you are on, and every other branch stays where it was. The --update-refs option exists to fix that.
A branch is a ref
A ref is a name that points to a commit. Branches, tags, and remote-tracking branches such as origin/main are all refs. Git stores local branches under .git/refs/heads/. You can read one directly.
git init pointers-demo
cd pointers-demo
git commit --allow-empty -m "first"
git commit --allow-empty -m "second"
cat .git/refs/heads/main
git rev-parse mainBoth commands print the same 40-character commit ID. (If your default branch is called master, use that name instead. Branches can also be packed into .git/packed-refs, so on an older repository the file may not exist. git rev-parse works in either case.)
A commit is different. It records a snapshot, a message, an author, and the ID of its parent commit. A commit never records which branch it was made on. The chain of parent links is the history. A branch is only a label stuck on one end of that chain.
HEAD and the current branch
HEAD is a special ref that tells Git where you are. Usually it holds the name of a branch rather than a commit ID. You can see this with:
git symbolic-ref HEADThe output is refs/heads/main. When you run git commit, Git does two things:
- It creates a new commit whose parent is the commit that
HEADresolves to. - It moves the branch that
HEADnames to the new commit.
Only one pointer moves. Every other branch stays where it was.
Moving and creating branches
Because a branch is only a pointer, creating one is cheap. It writes 41 bytes (a 40-character ID and a newline) to a file. Moving one is just as cheap.
git branch topic # new pointer at the current commit
git branch -f topic HEAD~1 # move the pointer back one commit
git switch topic # point HEAD at the topic branchThe -f option forces git branch to move a branch that already exists. Nothing is deleted when you do this. The commit the branch used to point to is still in the repository. It is only unreachable from that name, which is why the reflog can recover it later (see the chapter on recovery).
What a rebase does to refs
A rebase rewrites history, so it is worth stating precisely what it does to pointers. Suppose main and topic have diverged:
A---B---C main
\
D---E topicRunning git rebase main while on topic does this:
- Git finds the commits that are on
topicbut not onmain:DandE. - Git creates new commits
D'andE'. They have the same changes and messages, but different parents, so they get different IDs. - Git moves the
topicpointer toE'.
A---B---C main
\
D'---E' topicD and E still exist in the repository. Nothing points at them any more, except the reflog. The key point is that a rebase moves exactly one branch pointer: the one that HEAD names.
Two branches on one chain
Now add a second branch that sits on top of the first. This is the shape of a stack, which the next chapter develops fully. Here we only need to see what it does to pointers.
git switch -c feature-1 main
git commit --allow-empty -m "parser skeleton"
git switch -c feature-2
git commit --allow-empty -m "parser tests"feature-2 was created from feature-1, so the commits line up in a single chain:
main --- P --- T
| |
feature-1 feature-2Here P is "parser skeleton" and T is "parser tests". feature-1 points at P. feature-2 points at T, and T's parent is P. The branches never mention each other. The only link is that the commit feature-1 points to happens to be an ancestor of the commit feature-2 points to.
You can confirm this with --decorate, which prints every ref that points at each commit:
git log --oneline --decorate main..feature-2The output shows feature-2 on the newest commit and feature-1 on the one before it.
Why this matters for rebasing
Rebase feature-2 onto a newer main and apply the rule from above. Git copies the commits in main..feature-2, which are P and T. It moves feature-2 to the copy of T. It does not touch feature-1.
main --- M --- P' --- T' feature-2
(old chain, still present)
main --- P --- T
|
feature-1feature-1 still points at the old P. It has no idea that P' exists, because it never knew about any relationship with feature-2. This is the stale-branch problem. The next chapters show it in detail and fix it.
You can check where each pointer ended up with git rev-parse:
git rev-parse feature-1 feature-2
git merge-base --is-ancestor feature-1 feature-2 && echo "feature-1 is still in feature-2's history" || echo "feature-1 is stale"Before the rebase, the second command prints "feature-1 is still in feature-2's history". After a plain rebase of feature-2, it prints "feature-1 is stale". That one line is a useful health check for any stack.
Useful commands for inspecting refs
These commands help you see pointers directly. You will use them throughout the tutorial.
| Command | What it shows |
|---|---|
| `git rev-parse <ref>` | The commit ID a ref points to |
| `git branch -vv` | Every local branch, its commit, and its upstream |
| `git log --decorate --oneline --graph --all` | The commit graph with every ref labeled |
| `git for-each-ref refs/heads` | Raw list of local branch refs and their commit IDs |
| `git reflog show <branch>` | Earlier positions of one branch pointer |
| `git merge-base --is-ancestor A B` | Exit status 0 if A is in B's history |
Edge cases to know
- Detached HEAD. If
HEADholds a commit ID instead of a branch name, commits you make move no branch. Switch to a branch or create one before you leave, or the commits become easy to lose. - Remote-tracking refs.
origin/mainis a ref that Git updates when you fetch. It records where the remote branch was at your last fetch. It does not update by itself. - Packed refs. Git may pack many refs into one file, so a missing file in
refs/heads/does not mean the branch is gone. - Same commit, many names. Two branches can point to the same commit. That is normal, and it is the state of a stack right after you create a new branch on top of another.
Exercise
You have feature-1 pointing at commit P and feature-2 pointing at commit T, where T's parent is P. You switch to feature-2 and run git rebase main, where main has gained a new commit. Which branch pointers move, and where does feature-1 end up?
Show answer
Only feature-2 moves, to the rewritten copy of T. feature-1 stays on the original commit P, so it is now stale and no longer in feature-2's history. Git moves only the branch that HEAD names during a rebase.
Exercise
Which command prints the commit ID that the branch feature-1 points to, and which command would show you every earlier position of that branch?
Show answer
git rev-parse feature-1 prints the commit ID. git reflog show feature-1 lists the earlier positions of the branch pointer.