Sub topic 1
Branches as pointers into a commit graph
Before you move a branch to a new base, you need an accurate picture of what a branch is. This chapter builds that picture. Later chapters rely on it when they ask which commits a rebase will replay and where it will put them.
Prerequisites
This chapter assumes you can:
- Make commits, create branches, and switch between them with
git switch. - Read
git logoutput and run Git from the command line. - Use Git 2.28 or later. The examples use
git init -b.
You do not need to have used git rebase before.
Commits form a graph
A commit is a snapshot of your files plus some metadata. The metadata includes a pointer to the commit's parent, which is the commit it was built on. A commit never changes after it is created, and its ID is a hash of its contents, including the parent's ID.
Each commit points backward to its parent. A merge commit has two parents. Following the parent links from any commit gives you that commit's history. Together, all commits and their parent links form a directed graph with no cycles.
Git draws no arrows pointing forward. A commit does not know its children. To find them, Git starts at a branch and walks backward.
A branch is a pointer
A branch is a name that holds the ID of one commit. It is a small file or entry in the repository, not a copy of the history. When you commit on a branch, Git creates the new commit, sets its parent to the old tip, and moves the branch name to the new commit.
HEAD is a pointer to a branch, usually the one you have checked out. So HEAD points to a branch, and the branch points to a commit.
Here is a diagram of the repository you will build below.
Commit B is the point where the history split. Both main and topic reach it by following parent links.
Build the graph yourself
This script creates a repository with the shape in the diagram. The commits are empty, so the file contents do not matter. Only the graph does.
git init -b main graph-demo
cd graph-demo
git commit --allow-empty -m "A"
git commit --allow-empty -m "B"
git switch -c topic
git commit --allow-empty -m "C"
git commit --allow-empty -m "D"
git switch main
git commit --allow-empty -m "E"
git log --oneline --graph --all --decorateThe last command prints something like this. Your hashes will differ.
* 1a2b3c4 (HEAD -> main) E
| * 5d6e7f8 (topic) D
| * 9a0b1c2 C
|/
* 3d4e5f6 B
* 7a8b9c0 AHEAD -> main shows that HEAD points to the branch main. The label (topic) marks the commit topic points to.
You can read a branch pointer directly:
git rev-parse main
git rev-parse topic
git rev-parse topic~1The first two commands print the full IDs of E and D. The third prints the ID of C, because ~1 means "one parent back".
Reachability and ranges
A commit is reachable from a branch if you can get to it by following parent links from the branch's tip. Everything on a branch is whatever is reachable from its name. That includes commits it shares with other branches.
In the example, A and B are reachable from both main and topic. C and D are reachable only from topic. E is reachable only from main.
Git has a notation for "reachable from one name but not from another". It is written main..topic. You can try it now:
git log --oneline main..topicThe output lists D and C. They are the commits on topic that main does not contain. The next chapters show that this same idea decides which commits a rebase replays.
Why this matters for rebasing
Rebasing is an operation on the graph. Git picks a set of commits, copies them one by one onto a new parent, and then moves a branch pointer to the last copy. The original commits stay in the repository until Git cleans them up, which is why mistakes can often be undone.
Keep these three facts in mind for the rest of the tutorial:
- A branch is a pointer, so moving it costs nothing and does not change any commit.
- A branch's contents are defined by reachability, so two branches can share most of their commits.
- Commits are immutable, so "moving" a commit really means copying it.
Exercise
In the example repository you are on main. You run git switch topic and then git commit --allow-empty -m "F". Which pointers change, and does any existing commit change?
Show answer
HEAD now points to topic, and topic moves from D to the new commit F, whose parent is D. The branch main still points to E. No existing commit changes, because commits are immutable. Only the new commit F is created and the topic pointer is updated.