Sub topic 1
Branches as pointers into a commit graph
Every git rebase --onto command depends on a precise picture of what a branch is. This chapter builds that picture and shows how to inspect it in a real repository. Later chapters use it to explain which commits a rebase copies and where it puts them.
Prerequisites
This chapter assumes you can do the following:
- Run Git from a terminal and use
git commit,git switch,git branch,git log, andgit merge. - Read the output of
git log --oneline. - Make commits in a scratch repository without worrying about breaking anything.
You do not need to have used git rebase before. You need Git 2.28 or later for git init -b. Any recent version works for everything else.
Commits form a graph
A commit is an immutable snapshot of your project. It records four things:
- A pointer to a snapshot of the files (a tree).
- The author and committer, with timestamps.
- The commit message.
- Zero or more pointers to parent commits.
The parent pointers are the important part. A normal commit has one parent, the commit it was made on top of. A merge commit has two. The first commit in a repository has none. Together, these pointers form a directed acyclic graph: arrows run from each commit to its parents, and following them never leads back to where you started.
The arrows point backwards in time. A commit knows its parents, but a parent has no record of its children. This matters later, because Git can walk from a tip back to the root but cannot walk forward without help.
A commit's ID is a hash computed from all of its contents, including the parent pointers. If any part of a commit changes, the result is a different commit with a different ID. This is why Git never edits history in place. Any operation that appears to change a commit actually creates a new one.
A branch is a name for one commit
A branch is a name that points at exactly one commit, the tip of that line of work. It is not a container and it does not hold a list of commits. When you make a commit on a branch, Git does three things:
- It creates the new commit with the current tip as its parent.
- It moves the branch name to the new commit.
- It leaves everything else alone.
HEAD is a second pointer. Normally it points at a branch name, and that branch is your current branch. This is why git commit knows which name to move. When HEAD points directly at a commit instead of a branch, you are in detached HEAD state. Commits you make there are not tracked by any branch name.
The following table summarizes the moving parts.
| Thing | What it points to | Changes when |
|---|---|---|
| Commit | Its parent commits and a snapshot | Never. It is immutable. |
| Branch | One commit (the tip) | You commit, merge, reset, or rebase on it. |
| HEAD | Usually a branch, sometimes a commit | You switch branches or check out a commit. |
See it in a real repository
Build a small repository so you can look at the pointers directly. The commits are empty because only the graph matters here. The commands work in any shell with Git installed.
git init -b main graph-demo
cd graph-demo
git commit --allow-empty -m "A: first commit"
git switch -c topic
git commit --allow-empty -m "B: topic work"
git commit --allow-empty -m "C: more topic work"
git switch main
git commit --allow-empty -m "D: main moves on"
git log --oneline --graph --allIf Git complains that it does not know who you are, set user.name and user.email with git config first. The final command prints a graph like the one below. Your hashes will differ.
* 3f1c9a2 (HEAD -> main) D: main moves on
| * b7d40e1 (topic) C: more topic work
| * 8ac25f6 B: topic work
|/
* 51e0d7b A: first commitThe same history, drawn the way the rest of this tutorial thinks about it, looks like this.
Commits B and C have the same parent chain back to A, and D branches off A separately. The names topic and main are the only things that distinguish the two lines of work.
Inspect the pointers
Git stores branches as tiny files, and you can read them. This makes the "a branch is just a pointer" claim concrete.
# HEAD is a pointer to a branch name.
cat .git/HEAD
# A branch is a file holding one commit ID.
cat .git/refs/heads/topic
# Ask Git to resolve names to commit IDs.
git rev-parse main topic
# Look inside a commit: it lists its parent.
git cat-file -p topicThe first command prints ref: refs/heads/main. The second prints the full 40-character ID of commit C. The last command prints something like this:
tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
parent 8ac25f6...
author You <[email protected]> 1764863575 +0000
committer You <[email protected]> 1764863575 +0000
C: more topic workThe parent line is the arrow from C to B. Run git cat-file -p on that parent and you get B, whose parent is A. That chain of lookups is how every history command works.
The .git/refs/heads/ files exist only for loose references. After git gc or a clone, Git may pack branch names into .git/packed-refs. Use git rev-parse in scripts, not cat.
What "a branch contains commits" means
Git commands say a branch "contains" commits, but the branch stores only its tip. A commit is reachable from a branch if you can get to it from the tip by following parent pointers. The history of topic is the set of commits reachable from topic: C, B, and A.
Notice that A is reachable from both topic and main. Commits do not belong to a single branch. They are shared by every branch that can reach them. This is why creating a branch costs almost nothing and why deleting one does not delete commits. Deleting a branch only removes the name. If no other name or reflog entry reaches the commits, Git eventually garbage-collects them, but not immediately.
Ranges: the commits one tip has and another lacks
You often need "the commits on topic that are not on main". Git has a notation for it, the two-dot range:
git log --oneline main..topicIt prints commits reachable from topic and not reachable from main:
b7d40e1 C: more topic work
8ac25f6 B: topic workCommit A is missing from the output because main can reach it. Commit D is missing because topic cannot reach it. Read X..Y as "everything Y has that X does not".
Two related commands help you find where lines of work diverge:
# The most recent commit reachable from both tips.
git merge-base main topic
# Commits only on main: the range written the other way round.
git log --oneline topic..maingit merge-base main topic prints the ID of A. That commit is the fork point of the two branches, and it is the starting point for reasoning about what a rebase will do. The range main..topic is exactly the work that began at the fork point and sits on topic.
This range notation is the most important idea in the chapter. When you reach git rebase --onto <newbase> <upstream> <branch>, the <upstream>..<branch> range decides which commits get copied, and <newbase> decides where they land. Everything else in the tutorial builds on that.
Moving a branch changes the graph, not the commits
Because commits are immutable and branches are movable names, two kinds of operations exist.
Moving a name. Commands such as git commit, git merge (fast-forward), and git reset change which commit a branch name points at. No existing commit is modified. In a fast-forward merge, the name moves forward along existing parent pointers:
git switch -c scratch main
git merge --ff-only topic # fails here: main and topic have divergedThis fails because topic is not a descendant of main. A fast-forward is possible only when the new tip can reach the old tip.
Creating new commits. Rebasing is different. To put B and C on top of D, Git cannot change their parent pointers, because that would change their IDs. It creates new commits B' and C' with the same changes and messages, but different parents, and then moves the branch name to C'. The originals are untouched and still exist until nothing refers to them.
You can observe this now without a full rebase:
git switch topic
git rev-parse topic # note this ID
git rebase main # copies B and C onto D
git rev-parse topic # a different ID
git rev-parse ORIG_HEAD # the old tip is still reachable hereAfter the rebase, git log --oneline --graph --all shows a straight line A, D, B', C'. The old commits B and C are no longer reachable from any branch name, though ORIG_HEAD and the reflog still point to them. Later chapters rely on this to explain recovery. If you want to continue with this repository, run git reset --hard ORIG_HEAD to put topic back as it was.
Common mistakes in this mental model
- Thinking a branch is a line of commits. A branch is one pointer. The "line" is whatever you reach by following parents from it, and it overlaps with other branches.
- Thinking rebase moves commits. It copies them. The old versions remain in the object database until they are collected.
- Expecting a parent to know its children. Arrows point from child to parent only. Git finds descendants by starting at branch tips and walking back.
- Treating the range
A..Bas symmetric.main..topicandtopic..mainare different sets. Swapping them is one of the most common ways to give the wrong argument to a rebase.
Exercises
Exercise
In the demo repository, main points to D and topic points to C, with B and C on the topic line and D on the main line, all starting from A. What does git log --oneline topic..main print, and why?
Show answer
It prints only commit D. The range topic..main means commits reachable from main that are not reachable from topic. From main you reach D and A. From topic you reach C, B, and A. Removing the shared commit A leaves D.
Exercise
You run git branch -d topic after merging it, and later realize you need the commits back. Why is this usually recoverable, and which Git feature would you use to find the old tip?
Show answer
Deleting a branch removes only the name that points at the tip. The commits stay in the repository until garbage collection removes unreachable ones, which does not happen immediately. The reflog records where HEAD and branches pointed, so git reflog shows the old tip's ID. You can then recreate the branch with git branch topic <id>.
Exercise
A teammate says "rebasing moves my commits onto the new base." Using what you know about commit IDs and parents, explain what actually happens.
Show answer
A commit's ID depends on its contents, including its parent pointers. Changing the parent therefore means a different commit. Rebase creates new commits with the same changes and messages but new parents and new IDs, then moves the branch name to the last new commit. The original commits are not modified.