Sub topic 1
Branches are just pointers
Prerequisites
This chapter assumes you can use git add, git commit, git switch, and git log. You should know how a pull request and a remote branch relate. You should also have rebased a single branch before. Git 2.38 or newer is needed later in the tutorial, so check it now with git --version.
What a branch is
A branch is a small file that holds one commit ID. Git stores it under .git/refs/heads/, and the name of the file is the name of the branch. A name that points at an object, such as a branch, a tag, or HEAD, is called a ref.
Each commit stores the ID of its parent. The branch only says where the newest commit is. History is the chain of parent links that starts there.
git switch -c demo main
echo one > a.txt && git add a.txt && git commit -m "Add a.txt"
cat .git/refs/heads/demo
git rev-parse demo
git rev-parse HEADAll three commands print the same 40-character ID. When you commit, Git creates the new commit and then writes its ID into the file for the current branch. This is the only way a branch "moves".
Moving a branch without committing
Because a branch is only a pointer, you can set it to any commit directly. git branch -f does this:
git branch -f demo main
git log --oneline --decorate -3The demo branch now points at the same commit as main. The commit you made earlier still exists, but no branch reaches it. You can still find it through the reflog, which a later chapter covers.
What a rebase does to refs
git rebase copies commits and then moves a ref. Suppose feature has two commits on top of an old main:
- Git finds the commits that are on
featurebut not on the new base. - It replays each one on top of the new base. Each copy has a new ID, because the parent changed.
- It moves
featureto the last copy.
The original commits are not changed. They stay in the object database until garbage collection removes them. Only the one ref that you rebased moves.
Why this matters for stacks
A stack is a chain of branches where each branch is built on the one before it. Every branch in the chain is its own pointer. When you rebase the top branch, Git moves that one pointer. The pointers below it are separate files, and the rebase does not touch them.
You can see the pointers directly with git for-each-ref:
git for-each-ref --format='%(refname:short) %(objectname:short)' refs/headsRun this before and after a rebase. The branch you rebased shows a new ID. The other branches show the same IDs as before.
Common mistakes
- Thinking a branch "contains" commits. It only points at one, and other branches can reach the same commits.
- Assuming a rebase changes commits in place. It always creates copies with new IDs.
- Deleting a branch and believing its commits are gone. They remain reachable through the reflog until Git prunes them.
Exercise
You run git rebase main while feature-2 is checked out. Another branch, feature-1, is an ancestor of feature-2. Which refs change, and why does feature-1 not change?
Show answer
Only feature-2 moves, to the last of the newly copied commits. feature-1 is a separate file that holds one commit ID, and a plain rebase updates only the branch that is checked out. feature-1 still points at the old commit.
Exercise
What command shows the commit ID that the branch demo points at, without reading any file by hand?
Show answer
git rev-parse demo. It prints the full commit ID stored in the demo ref.