Sub topic 14
Keeping merge commits with --rebase-merges
This chapter assumes you can run a basic rebase onto origin/main and read a todo list from git rebase -i. It also assumes you know what a merge commit is: a commit with two parents.
What plain rebase does to merges
By default, git rebase drops merge commits. It takes the non-merge commits in the range and replays them one after another. The branch structure you built disappears, and the result is a single straight line.
That is often what you want. It is a problem when the merges carry meaning. One example is a feature branch that integrated two sub-branches, where reviewers rely on seeing them as separate lines of work.
Recreating merges
The --rebase-merges option (short form -r) tells Git to rebuild the merge structure on the new base instead of flattening it.
git switch feature
git fetch origin
git rebase --rebase-merges origin/mainEvery commit is still replayed and gets a new hash. The difference is that Git recreates each merge commit with the same shape, so the side branches stay side branches.
The diagram shows the result.
Reading the todo list
Run it interactively to see how Git does this.
git rebase -r -i origin/mainBesides pick, the todo list now contains three extra commands.
label onto
# Branch: side-work
reset onto
pick 1111111 Add parser
label side-work
reset onto
pick 2222222 Add cli
merge -C 3333333 side-work # Merge branch 'side-work'labelgives a name to the current commit so that later steps can refer to it.ontois the new base.resetmoves HEAD back to a labelled commit, which starts a new line of work from there.merge -C <commit> <label>creates a merge of the labelled branch and reuses the message of the original merge commit.
You can reorder or drop commits inside each section as in any interactive rebase. Keep the label, reset, and merge lines in a consistent order. If a merge line comes before its label exists, the rebase fails.
Limits to know about
- Manual edits are lost. Git recreates a merge by running a new merge, so changes you made inside the original merge commit are not carried over. These are sometimes called evil merges. Check the result with
git diff ORIG_HEADafter the rebase. - Conflicts can recur. A conflict can appear when a merge is recreated. Resolve it with the usual
git addandgit rebase --continue. - The older option is gone.
--preserve-mergeswas removed in Git 2.34. Use--rebase-merges. - The shared history rule still applies. The rewritten merges have new hashes. Push them only to branches nobody else has based work on, and use
--force-with-lease.
By default, only merges that were made inside the rebased range are recreated as merges. The mode --rebase-merges=rebase-cousins also rebases side branches that start before the range, onto the new base. The default no-rebase-cousins leaves them where they are.
Exercise
You run git rebase origin/main on a branch that contains a merge of side-work, and the history comes out as a straight line. Which option keeps the merge, and which todo-list command recreates it?
Show answer
Use git rebase --rebase-merges origin/main (or -r). The merge command in the todo list recreates the merge commit from a labelled branch.