Sub topic 5
Rebase and merge
This chapter covers the third merge method on GitHub, Rebase and merge. It assumes you can commit, branch, push, and pull, open and review a pull request, and read git log --oneline --graph output. It also assumes you know the other two methods exist: a merge commit and a squash merge.
What the method does
Rebase and merge takes each commit on the pull request's head branch and replays it, one at a time, on top of the tip of the base branch. No merge commit is created, and the commits are not combined. The base branch gets the same number of commits the pull request had, in the same order, and its history stays a straight line.
Replaying a commit means applying its diff on a new parent. A commit's SHA is a hash of its contents, its metadata, and its parent. A new parent therefore gives a new SHA. After the merge, main holds new commits that look like your originals but have different identifiers.
What gets preserved and what changes
| Property | After rebase and merge |
|---|---|
| Number of commits | Same as the pull request |
| Commit messages | Unchanged |
| Author name and date | Preserved |
| Committer | Set to the person who merged, not the original committer |
| Commit SHAs | New for every commit |
| Merge commit | None |
| Original signatures | Not carried over, because the commit content changed |
The new SHAs have practical consequences:
- A commit SHA you pasted into a ticket or a review comment from the branch no longer exists on
main. The pull request page still links to the original commits, butgit log mainwill not show them. git branch --merged mainwill not list the feature branch, because Git sees different commits. The two branches hold the same changes under different identities.- Status checks that passed on the pull request ran against the original head commit. The final
maincommit is a new one. Required checks are normally evaluated before the merge, so the exact tree that lands may never have been tested as such.
Seeing it in the log
Run the log command from the history chapter on the base branch after merging:
git log --oneline --graph --decorate mainA rebase-merged pull request with two commits looks like this:
* 7d3e9a1 (HEAD -> main, origin/main) Add retry tests
* 41b8c02 Add retry logic to the HTTP client
* 9f0a6de Update READMEThere are no |\ or |/ lines and no commit with two parents. A merge commit would add a node with a fork and a join. A squash merge would show one commit where this shows two.
How it differs from a local `git rebase`
GitHub's rebase and merge is not the same as running git rebase main on your own machine and then fast-forwarding. Two differences matter:
- GitHub always rewrites the commits. If
mainhas not moved since you branched, a local rebase followed by a fast-forward keeps your original SHAs. GitHub still creates new commits, because it updates the committer information. The SHAs change in every case. - The head branch is not touched. Your local branch and the remote head branch keep the old commits. After the merge, the head branch is a divergent copy of what is now on
main. This is why you delete the branch afterward, or at least do not keep building on it (see the stacked branches chapter).
Merge commits inside the head branch are dropped during replay. If you merged main into your feature branch to update it, those merge commits do not appear on main. Only the non-merge commits are replayed.
Conflicts and what the web cannot do
The web button is available only when the replay can be done without any manual resolution. If any commit conflicts when it is applied on top of the base branch, GitHub disables Rebase and merge for that pull request. The page tells you there are conflicts, but the conflict editor on GitHub is not the way out for this method. The web editor resolves conflicts by creating a merge commit on the head branch, and that does not make a replay possible.
A conflict can also appear in the middle of the series. A change in commit 2 may conflict with the base even though commit 1 applied cleanly. A squash merge compares only the final result, so it can succeed on a branch where rebase and merge fails. Squash is also more tolerant of a branch that edited and then reverted the same lines.
To resolve the conflict yourself, rebase locally and force-push the head branch:
git fetch origin
git switch feature
git rebase origin/main
# resolve conflicts in each stopped commit, then:
git add path/to/resolved-file
git rebase --continue
git push --force-with-lease origin feature--force-with-lease refuses the push if someone else pushed to feature after your last fetch, so it will not overwrite their work. After the push, the pull request updates and the button becomes available again.
Other limits worth knowing:
- Rebase and merge is not offered when the repository settings do not allow it. See the next chapter on settings.
- It is not offered when the head branch contains a commit that would be rewritten in a way GitHub cannot do, for example when the replayed history would require changes to workflow files and the merging identity lacks the permission for that.
- Because it replays every commit, a pull request with many commits does many individual applications. Each one must apply cleanly, so large, messy branches are more likely to fail than with a squash.
Running it from the web UI and the CLI
On the pull request page, open the merge button's drop-down, choose Rebase and merge, and confirm. There is no message box for this method, because GitHub does not create a new message. It reuses the existing ones.
From the gh command-line tool:
gh pr merge 123 --rebase
gh pr merge 123 --rebase --delete-branchThe --delete-branch flag removes the head branch after the merge, which is the safe default for this method given the divergent copy described above.
Reverting a rebase-merged pull request
There is no single merge commit to revert, so git revert -m 1 does not apply. Using -m on a commit with one parent fails with an error that the commit is not a merge. Instead, revert the commits that landed. For a pull request that added two commits, run:
git switch main
git pull --ff-only
git revert --no-commit 41b8c02^..7d3e9a1
git commit -m "Revert pull request 123"The range 41b8c02^..7d3e9a1 includes the first commit and the last one. --no-commit stages all the reverts together so you get one revert commit instead of one per original commit. Find the exact SHAs from the log, not from the pull request page, since the page may show the pre-rebase SHAs.
This is harder to do than reverting a merge commit or a squash commit. The commits of a pull request are not marked as a group, so you must identify the range by hand. Squash gives a single commit to revert, and a merge commit gives one commit that names the whole group.
Interaction with branch protection
Rebase and merge produces a linear history, so it satisfies the Require linear history branch protection rule. The rule blocks merge commits, so a repository using it must allow squash, rebase, or both. If a repository enables the rule and still shows a merge commit option, the option fails when someone selects it.
When rebase and merge fits
Choose it when each commit on the branch is meaningful on its own and you want that granularity on main. Typical cases are a carefully curated series with a refactor commit, then a feature commit, then a test commit. Bisecting works well on that history because every commit builds and passes, and git blame points to the focused commit instead of a large squashed one.
Avoid it when the branch has "fix typo" and "address review" commits. Those go onto main untouched, and every one becomes a permanent entry that may not build on its own. Rebase and merge does not clean up history. It only removes the merge commit.
Exercise
A pull request has three commits and the base branch has not moved since you branched. You use Rebase and merge. Will the three commits on main have the same SHAs as on the head branch? How would you find the commits to revert the pull request later?
Show answer
No. GitHub always creates new commits when it rebases and merges, because it sets the committer information, so all three SHAs change even without a rebase onto new work. To revert, use git log on the base branch to find the first and last of the three new commits, then run git revert --no-commit <first>^..<last> and commit the result as one revert. Do not use git revert -m 1, since no merge commit exists.
Exercise
Rebase and merge is greyed out on a pull request, but Squash and merge is available. What is the most likely cause, and how do you fix it without changing repository settings?
Show answer
One of the commits conflicts with the base branch when replayed, and a squash only needs the final result to apply. Rebase the head branch onto origin/main locally, resolve the conflicts in each stopped commit, run git rebase --continue, and push with git push --force-with-lease. The button then becomes available.