Sub topic 5
Rebase and merge
This chapter assumes you can commit, branch, push, and read git log output, and that you have opened and reviewed a pull request on GitHub. Earlier chapters covered the commit graph, the merge commit, and squash and merge. Here we look at the third method, Rebase and merge.
What it does
Rebase and merge takes each commit from the pull request branch and replays it on top of the base branch. No merge commit is created. The base branch stays a straight line, and every commit from the pull request appears in it individually.
Suppose main has commits A and B, and feature branched from A and added commits C and D:
main: A---B
\
feature: (C, D replayed on B)After the merge, main looks like this:
A---B---C'---D'C' and D' have the same changes, messages, and authors as C and D. They have new SHAs, because a commit's SHA depends on its parent, and the parent changed.
Seeing the result
Run this on the base branch after the merge:
git log --oneline --graph --decorate mainYou will see a single line with no merge bubbles. The commit SHAs differ from the ones on your feature branch. This is the quickest way to tell a rebase merge from a merge commit.
Committer versus author
Each replayed commit keeps its original author. The committer becomes the person who clicked the button, because a rebase rewrites the commit. You can see both fields:
git log -3 --format="%h %an (author) / %cn (committer) %s" mainConflicts and limits on the web
GitHub only offers Rebase and merge when the replay can finish without human help. If any commit conflicts with the base branch, the button is disabled or the merge fails. The web editor cannot resolve this for you.
Two other limits matter:
- Commits are not squashed or reworded. Messages like "fix typo" and "wip" land on
mainexactly as written. Clean them up on your branch first. - Intermediate commits are not tested by CI. CI usually ran on the branch tip. The replayed commits C' and D' are new, so they were never tested. If a middle commit is broken,
git bisectmay land on it.
To resolve conflicts, rebase locally and force-push the branch:
git fetch origin
git rebase origin/main
git push --force-with-leaseThen merge the pull request again. Use --force-with-lease instead of --force, because it refuses to overwrite commits you have not seen.
Cost: the local branch no longer matches
After the merge, your local feature branch holds C and D, but main holds C' and D'. Git sees them as different commits. If you keep working on feature and open another pull request, Git may report duplicate conflicts. Delete the branch after merging, or start new work from the updated main.
Running it from the CLI
gh pr merge 123 --rebaseAdd --delete-branch to remove the head branch afterward.
When it fits
Rebase and merge suits teams that write small, well-organized commits and want a linear history that keeps every commit. It fits poorly when branches contain noisy commits or when you need the pull request to be one revertable unit.
Exercise
A pull request with commits C and D (SHAs c1 and d1) is merged with Rebase and merge. You then run git log --oneline main. Will you see the SHAs c1 and d1? Is there a merge commit?
Show answer
No to both. The commits appear as new commits C' and D' with different SHAs, because they were replayed onto the tip of main and so have different parents. No merge commit is created, so the history is a straight line. The original authors are kept, but the committer is the person who merged.