@@ -259,11 +259,10 @@ include::merge-strategies.txt[]
259259
260260NOTES
261261-----
262- When you rebase a branch, you are changing its history in a way that
263- will cause problems for anyone who already has a copy of the branch
264- in their repository and tries to pull updates from you. You should
265- understand the implications of using 'git-rebase' on a repository that
266- you share.
262+
263+ You should understand the implications of using 'git-rebase' on a
264+ repository that you share. See also RECOVERING FROM UPSTREAM REBASE
265+ below.
267266
268267When the git-rebase command is run, it will first execute a "pre-rebase"
269268hook if one exists. You can use this hook to do sanity checks and
@@ -398,6 +397,127 @@ consistent (they compile, pass the testsuite, etc.) you should use
398397after each commit, test, and amend the commit if fixes are necessary.
399398
400399
400+ RECOVERING FROM UPSTREAM REBASE
401+ -------------------------------
402+
403+ Rebasing (or any other form of rewriting) a branch that others have
404+ based work on is a bad idea: anyone downstream of it is forced to
405+ manually fix their history. This section explains how to do the fix
406+ from the downstream's point of view. The real fix, however, would be
407+ to avoid rebasing the upstream in the first place.
408+
409+ To illustrate, suppose you are in a situation where someone develops a
410+ 'subsystem' branch, and you are working on a 'topic' that is dependent
411+ on this 'subsystem'. You might end up with a history like the
412+ following:
413+
414+ ------------
415+ o---o---o---o---o---o---o---o---o master
416+ \
417+ o---o---o---o---o subsystem
418+ \
419+ *---*---* topic
420+ ------------
421+
422+ If 'subsystem' is rebased against 'master', the following happens:
423+
424+ ------------
425+ o---o---o---o---o---o---o---o master
426+ \ \
427+ o---o---o---o---o o'--o'--o'--o'--o' subsystem
428+ \
429+ *---*---* topic
430+ ------------
431+
432+ If you now continue development as usual, and eventually merge 'topic'
433+ to 'subsystem', the commits from 'subsystem' will remain duplicated forever:
434+
435+ ------------
436+ o---o---o---o---o---o---o---o master
437+ \ \
438+ o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
439+ \ /
440+ *---*---*-..........-*--* topic
441+ ------------
442+
443+ Such duplicates are generally frowned upon because they clutter up
444+ history, making it harder to follow. To clean things up, you need to
445+ transplant the commits on 'topic' to the new 'subsystem' tip, i.e.,
446+ rebase 'topic'. This becomes a ripple effect: anyone downstream from
447+ 'topic' is forced to rebase too, and so on!
448+
449+ There are two kinds of fixes, discussed in the following subsections:
450+
451+ Easy case: The changes are literally the same.::
452+
453+ This happens if the 'subsystem' rebase was a simple rebase and
454+ had no conflicts.
455+
456+ Hard case: The changes are not the same.::
457+
458+ This happens if the 'subsystem' rebase had conflicts, or used
459+ `\--interactive` to omit, edit, or squash commits; or if the
460+ upstream used one of `commit \--amend`, `reset`, or
461+ `filter-branch`.
462+
463+
464+ The easy case
465+ ~~~~~~~~~~~~~
466+
467+ Only works if the changes (patch IDs based on the diff contents) on
468+ 'subsystem' are literally the same before and after the rebase
469+ 'subsystem' did.
470+
471+ In that case, the fix is easy because 'git-rebase' knows to skip
472+ changes that are already present in the new upstream. So if you say
473+ (assuming you're on 'topic')
474+ ------------
475+ $ git rebase subsystem
476+ ------------
477+ you will end up with the fixed history
478+ ------------
479+ o---o---o---o---o---o---o---o master
480+ \
481+ o'--o'--o'--o'--o' subsystem
482+ \
483+ *---*---* topic
484+ ------------
485+
486+
487+ The hard case
488+ ~~~~~~~~~~~~~
489+
490+ Things get more complicated if the 'subsystem' changes do not exactly
491+ correspond to the ones before the rebase.
492+
493+ NOTE: While an "easy case recovery" sometimes appears to be successful
494+ even in the hard case, it may have unintended consequences. For
495+ example, a commit that was removed via `git rebase
496+ \--interactive` will be **resurrected**!
497+
498+ The idea is to manually tell 'git-rebase' "where the old 'subsystem'
499+ ended and your 'topic' began", that is, what the old merge-base
500+ between them was. You will have to find a way to name the last commit
501+ of the old 'subsystem', for example:
502+
503+ * With the 'subsystem' reflog: after 'git-fetch', the old tip of
504+ 'subsystem' is at `subsystem@\{1}`. Subsequent fetches will
505+ increase the number. (See linkgit:git-reflog[1].)
506+
507+ * Relative to the tip of 'topic': knowing that your 'topic' has three
508+ commits, the old tip of 'subsystem' must be `topic~3`.
509+
510+ You can then transplant the old `subsystem..topic` to the new tip by
511+ saying (for the reflog case, and assuming you are on 'topic' already):
512+ ------------
513+ $ git rebase --onto subsystem subsystem@{1}
514+ ------------
515+
516+ The ripple effect of a "hard case" recovery is especially bad:
517+ 'everyone' downstream from 'topic' will now have to perform a "hard
518+ case" recovery too!
519+
520+
401521Authors
402522------
403523Written by Junio C Hamano <gitster@pobox.com> and
0 commit comments