Sub topic 3
How the Upstream Argument Selects Commits
This chapter assumes you can commit, branch, switch, and read git log output. It also assumes you know a branch is a movable pointer to a commit. You don't need to have used git rebase --onto before. This chapter explains the argument that decides which commits get replayed.
Two questions, two arguments
Every rebase answers two separate questions:
- Which commits should be copied?
- Where should the copies be placed?
The full form of the command answers them with different arguments:
git rebase --onto <newbase> <upstream> <branch><upstream>answers the first question. It marks where the commits to copy end on the old side.<newbase>answers the second question. It is the commit the copies are built on top of.<branch>is the branch that gets moved. Git switches to it before starting, and the branch name is reset to the last copy when the rebase finishes.
This chapter covers <upstream>. The --onto syntax itself is the next chapter's subject, and we only touch it where needed to show the range at work.
The range is upstream..branch
Git selects commits with the same range notation that git log uses. The commits to replay are those reachable from <branch> but not reachable from <upstream>. This is written upstream..branch.
"Reachable" means you can get there by following parent links backwards from a commit. A commit is reachable from itself. So the upstream commit and everything behind it is subtracted from the set of commits you are rebasing.
This is why the argument is called the upstream even though it often has nothing to do with a remote. It is the boundary line. Everything at or before it stays where it is. Everything after it, up to the tip of the branch, is copied.
The diagram shows a repository where topic was started from next, and next was started from main. Arrows point from a commit to its parent, as Git stores them.
Reading topic backwards gives t2, t1, n2, n1, m1. Subtracting everything reachable from next removes n2, n1, and m1. What remains is t1 and t2, so those are the commits that git rebase --onto main next topic copies. Using main as the upstream instead would subtract only m1, and n1 and n2 would be copied as well.
A runnable example
This script builds the graph above. It uses one file per commit, so nothing conflicts. It needs Git 2.28 or later for git init -b.
git init -q -b main demo
cd demo
git config user.email [email protected]
git config user.name Demo
commit() { echo "$1" > "$1.txt"; git add "$1.txt"; git commit -q -m "$1"; }
commit m1
git switch -q -c next
commit n1
commit n2
git switch -q -c topic
commit t1
commit t2
git switch -q main
commit m2
commit m3
echo "--- next..topic (the range --onto would replay)"
git log --oneline next..topic
echo "--- main..topic (what a wrong upstream would select)"
git log --oneline main..topicThe first git log prints two lines, t2 and t1. The second prints four: t2, t1, n2, and n1. The hashes will differ on your machine, but the subjects and their order will match.
Now run the rebase with the right upstream and look at the result:
git rebase --onto main next topic
git log --oneline --graph --decorate main next topicAfter the rebase, topic contains m1, m2, m3, and then copies of t1 and t2. The commits n1 and n2 are not in topic any more. They still exist on next, which did not move.
The copies have new hashes. A commit's hash covers its parent, so a commit with a different parent is a different commit, even if its diff and message are identical. The old t1 and t2 are still in the repository, but nothing points at them except the reflog.
The upstream can be any commit
<upstream> does not have to be a branch name. Anything Git can resolve to a commit works:
| Upstream written as | Meaning | Range replayed |
|---|---|---|
| `next` | Current tip of the branch `next` | `next..topic` |
| `a1b2c3d` | A specific commit by hash | commits after that commit, up to the tip of `topic` |
| `v1.2` | A tag | commits after the tagged commit, up to the tip of `topic` |
| `topic~2` | The grandparent of the tip | only the last two commits of `topic` |
| `origin/main` | A remote-tracking branch | `origin/main..topic` |
The boundary commit itself is excluded, which is a common source of off-by-one mistakes. If you want to replay the last two commits of a branch, the upstream is topic~2, the commit just before them. Writing topic~1 replays only the tip.
A branch name is resolved when you run the command, not remembered. If next has moved since you started topic, the range next..topic is measured from where next points now, and it may have grown or shrunk compared with what you expect. Run the preview command below whenever you are unsure.
What Git filters out of the range
The range upstream..branch is the starting set. Git then removes some commits before replaying.
Merge commits. By default, a rebase drops merge commits and replays the ordinary commits on each side as one straight line. If your branch contains merges of other branches, the result is flattened. The option --rebase-merges keeps the merge structure, but it is outside this chapter.
Commits already applied upstream. Git computes a fingerprint of each commit's change, called a patch ID. It is based on the diff and ignores line numbers and whitespace. If a commit in your range has the same patch ID as a commit that is on the upstream side but not in your branch, Git skips it and prints a note. This handles the case where someone cherry-picked or applied your patch to another branch. If you want those commits replayed anyway, pass --reapply-cherry-picks.
Commits that become empty. If a replayed commit produces no change on its new base, Git drops it by default. For example, a commit that makes a change already present in newbase ends up empty.
Squash merges are a case where this filtering does not help you. A squash merge builds one new commit with a different diff from any of the originals, so no patch ID matches. Git then replays the originals and often hits conflicts. Choosing the original branch as the upstream avoids this. That technique is the subject of a later chapter.
Defaults when you leave arguments out
The square brackets in the real syntax show which parts are optional:
git rebase [--onto <newbase>] [<upstream> [<branch>]]- Without
<branch>, Git rebases the branch you are on (HEAD). - Without
--onto,<newbase>is the same as<upstream>. - Without
<upstream>, Git uses the upstream configured for the current branch, set withgit branch --set-upstream-toor bygit push -u. It fails if none is configured.
The second rule shows that a plain rebase is a special case of --onto. These two commands do the same thing:
# Plain rebase: the upstream is also the new base.
git switch topic
git rebase main
# The same operation written out with --onto.
git rebase --onto main main topicIn the plain form, the boundary and the destination are one commit, so you cannot separate them. --onto lets them differ: the boundary stays at next while the destination is main.
Preview the range before you run it
Because the range is just upstream..branch, you can see exactly what will be replayed before changing anything:
git log --oneline next..topicIf the list holds commits you did not write, or is longer than you expected, the upstream is wrong. Fix the command before running the rebase. The preview costs nothing, and a rebase with the wrong upstream can take time to untangle.
To count commits instead of listing them, use git rev-list --count next..topic.
Common errors
Using the destination as the upstream. With git rebase --onto main main topic, Git subtracts everything reachable from main. If topic came from next, the range includes next's own commits, and the copies of n1 and n2 land on main. You then have duplicate commits, and conflicts if next later merges. The fix is to use the commit where your work really starts as the upstream.
Off-by-one boundaries. The upstream commit is excluded. To keep three commits, the upstream is the commit before them, topic~3.
Omitting the branch while on another branch. git rebase --onto main next rebases whatever is checked out. Run git branch --show-current first, or name the branch explicitly.
Assuming a branch name still marks the old start. After next moves, next..topic no longer describes the commits you wrote. If the old start matters, save it first. For example, git branch topic-base next before next changes, or note its hash.
Expecting merge commits to survive. A rebase of a branch containing merges flattens them unless you ask otherwise. Check the result with git log --graph.
Exercise
A branch feature has five commits on top of main, oldest to newest: A, B, C, D, E. You want to replay only D and E onto a branch called release. Write the rebase command and the git log command that previews the range.
Show answer
Use the commit just before D, which is feature~2, as the upstream. The preview is git log --oneline feature~2..feature, which lists E and D. The rebase is git rebase --onto release feature~2 feature. The upstream feature~2 is C, and C is excluded from the range, so only D and E are copied onto release.
Exercise
In the example repository, you run git rebase --onto main main topic by mistake instead of git rebase --onto main next topic. Which commits does Git replay, and how would you have caught this before running it?
Show answer
Git replays the range main..topic, which is n1, n2, t1, and t2, because only m1, m2, and m3 are subtracted. Running git log --oneline main..topic first would have shown four commits instead of the expected two, which reveals the wrong upstream before any history changes.