1 <?xml version="1.0" encoding="UTF-8"?>
\r
2 <!DOCTYPE sect2 SYSTEM "../../../dtd/dblite.dtd">
\r
4 <sect2 lang="en" id="git-rebase(1)">
\r
5 <title>git-rebase(1)</title>
\r
7 <primary>git-rebase(1)</primary>
\r
9 <simplesect id="git-rebase(1)__name">
\r
11 <simpara>git-rebase - Reapply commits on top of another base tip</simpara>
\r
13 <simplesect id="git-rebase(1)__synopsis">
\r
14 <title>SYNOPSIS</title>
\r
16 <literallayout><emphasis>git rebase</emphasis> [-i | --interactive] [<options>] [--exec <cmd>]
\r
17 [--onto <newbase> | --keep-base] [<upstream> [<branch>]]
\r
18 <emphasis>git rebase</emphasis> [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
\r
19 --root [<branch>]
\r
20 <emphasis>git rebase</emphasis> (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)</literallayout>
\r
23 <simplesect id="git-rebase(1)__description">
\r
24 <title>DESCRIPTION</title>
\r
25 <simpara>If <branch> is specified, <emphasis>git rebase</emphasis> will perform an automatic
\r
26 <emphasis>git switch <branch></emphasis> before doing anything else. Otherwise
\r
27 it remains on the current branch.</simpara>
\r
28 <simpara>If <upstream> is not specified, the upstream configured in
\r
29 branch.<name>.remote and branch.<name>.merge options will be used (see
\r
30 <xref linkend="git-config(1)" /> for details) and the <emphasis>--fork-point</emphasis> option is
\r
31 assumed. If you are currently not on any branch or if the current
\r
32 branch does not have a configured upstream, the rebase will abort.</simpara>
\r
33 <simpara>All changes made by commits in the current branch but that are not
\r
34 in <upstream> are saved to a temporary area. This is the same set
\r
35 of commits that would be shown by <emphasis>git log <upstream>..HEAD</emphasis>; or by
\r
36 <emphasis>git log 'fork_point'..HEAD</emphasis>, if <emphasis>--fork-point</emphasis> is active (see the
\r
37 description on <emphasis>--fork-point</emphasis> below); or by <emphasis>git log HEAD</emphasis>, if the
\r
38 <emphasis>--root</emphasis> option is specified.</simpara>
\r
39 <simpara>The current branch is reset to <upstream>, or <newbase> if the
\r
40 --onto option was supplied. This has the exact same effect as
\r
41 <emphasis>git reset --hard <upstream></emphasis> (or <newbase>). ORIG_HEAD is set
\r
42 to point at the tip of the branch before the reset.</simpara>
\r
43 <simpara>The commits that were previously saved into the temporary area are
\r
44 then reapplied to the current branch, one by one, in order. Note that
\r
45 any commits in HEAD which introduce the same textual changes as a commit
\r
46 in HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
\r
47 with a different commit message or timestamp will be skipped).</simpara>
\r
48 <simpara>It is possible that a merge failure will prevent this process from being
\r
49 completely automatic. You will have to resolve any such merge failure
\r
50 and run <emphasis>git rebase --continue</emphasis>. Another option is to bypass the commit
\r
51 that caused the merge failure with <emphasis>git rebase --skip</emphasis>. To check out the
\r
52 original <branch> and remove the .git/rebase-apply working files, use the
\r
53 command <emphasis>git rebase --abort</emphasis> instead.</simpara>
\r
54 <simpara>Assume the following history exists and the current branch is "topic":</simpara>
\r
55 <screen> A---B---C topic
\r
57 D---E---F---G master</screen>
\r
58 <simpara>From this point, the result of either of the following commands:</simpara>
\r
59 <literallayout class="monospaced">git rebase master
\r
60 git rebase master topic</literallayout>
\r
61 <simpara>would be:</simpara>
\r
62 <screen> A'--B'--C' topic
\r
64 D---E---F---G master</screen>
\r
65 <simpara><emphasis role="strong">NOTE:</emphasis> The latter form is just a short-hand of <emphasis>git checkout topic</emphasis>
\r
66 followed by <emphasis>git rebase master</emphasis>. When rebase exits <emphasis>topic</emphasis> will
\r
67 remain the checked-out branch.</simpara>
\r
68 <simpara>If the upstream branch already contains a change you have made (e.g.,
\r
69 because you mailed a patch which was applied upstream), then that commit
\r
70 will be skipped. For example, running <emphasis>git rebase master</emphasis> on the
\r
71 following history (in which <emphasis>A'</emphasis> and <emphasis>A</emphasis> introduce the same set of changes,
\r
72 but have different committer information):</simpara>
\r
73 <screen> A---B---C topic
\r
75 D---E---A'---F master</screen>
\r
76 <simpara>will result in:</simpara>
\r
77 <screen> B'---C' topic
\r
79 D---E---A'---F master</screen>
\r
80 <simpara>Here is how you would transplant a topic branch based on one
\r
81 branch to another, to pretend that you forked the topic branch
\r
82 from the latter branch, using <emphasis>rebase --onto</emphasis>.</simpara>
\r
83 <simpara>First let's assume your <emphasis>topic</emphasis> is based on branch <emphasis>next</emphasis>.
\r
84 For example, a feature developed in <emphasis>topic</emphasis> depends on some
\r
85 functionality which is found in <emphasis>next</emphasis>.</simpara>
\r
86 <screen> o---o---o---o---o master
\r
88 o---o---o---o---o next
\r
90 o---o---o topic</screen>
\r
91 <simpara>We want to make <emphasis>topic</emphasis> forked from branch <emphasis>master</emphasis>; for example,
\r
92 because the functionality on which <emphasis>topic</emphasis> depends was merged into the
\r
93 more stable <emphasis>master</emphasis> branch. We want our tree to look like this:</simpara>
\r
94 <screen> o---o---o---o---o master
\r
98 o---o---o---o---o next</screen>
\r
99 <simpara>We can get this using the following command:</simpara>
\r
100 <literallayout class="monospaced">git rebase --onto master next topic</literallayout>
\r
101 <simpara>Another example of --onto option is to rebase part of a
\r
102 branch. If we have the following situation:</simpara>
\r
103 <screen> H---I---J topicB
\r
107 A---B---C---D master</screen>
\r
108 <simpara>then the command</simpara>
\r
109 <literallayout class="monospaced">git rebase --onto master topicA topicB</literallayout>
\r
110 <simpara>would result in:</simpara>
\r
111 <screen> H'--I'--J' topicB
\r
115 A---B---C---D master</screen>
\r
116 <simpara>This is useful when topicB does not depend on topicA.</simpara>
\r
117 <simpara>A range of commits could also be removed with rebase. If we have
\r
118 the following situation:</simpara>
\r
119 <screen> E---F---G---H---I---J topicA</screen>
\r
120 <simpara>then the command</simpara>
\r
121 <literallayout class="monospaced">git rebase --onto topicA~5 topicA~3 topicA</literallayout>
\r
122 <simpara>would result in the removal of commits F and G:</simpara>
\r
123 <screen> E---H'---I'---J' topicA</screen>
\r
124 <simpara>This is useful if F and G were flawed in some way, or should not be
\r
125 part of topicA. Note that the argument to --onto and the <upstream>
\r
126 parameter can be any valid commit-ish.</simpara>
\r
127 <simpara>In case of conflict, <emphasis>git rebase</emphasis> will stop at the first problematic commit
\r
128 and leave conflict markers in the tree. You can use <emphasis>git diff</emphasis> to locate
\r
129 the markers (<<<<<<) and make edits to resolve the conflict. For each
\r
130 file you edit, you need to tell Git that the conflict has been resolved,
\r
131 typically this would be done with</simpara>
\r
132 <literallayout class="monospaced">git add <filename></literallayout>
\r
133 <simpara>After resolving the conflict manually and updating the index with the
\r
134 desired resolution, you can continue the rebasing process with</simpara>
\r
135 <literallayout class="monospaced">git rebase --continue</literallayout>
\r
136 <simpara>Alternatively, you can undo the <emphasis>git rebase</emphasis> with</simpara>
\r
137 <literallayout class="monospaced">git rebase --abort</literallayout>
\r
139 <simplesect id="git-rebase(1)__configuration">
\r
140 <title>CONFIGURATION</title>
\r
148 Unused configuration variable. Used in Git versions 2.20 and
\r
149 2.21 as an escape hatch to enable the legacy shellscript
\r
150 implementation of rebase. Now the built-in rewrite of it in C
\r
151 is always used. Setting this will emit a warning, to alert any
\r
152 remaining users that setting this now does nothing.
\r
162 Default backend to use for rebasing. Possible choices are
\r
163 <emphasis>apply</emphasis> or <emphasis>merge</emphasis>. In the future, if the merge backend gains
\r
164 all remaining capabilities of the apply backend, this setting
\r
175 Whether to show a diffstat of what changed upstream since the last
\r
176 rebase. False by default.
\r
186 If set to true enable <emphasis>--autosquash</emphasis> option by default.
\r
196 When set to true, automatically create a temporary stash entry
\r
197 before the operation begins, and apply it after the operation
\r
198 ends. This means that you can run rebase on a dirty worktree.
\r
199 However, use with care: the final stash application after a
\r
200 successful rebase might result in non-trivial conflicts.
\r
201 This option can be overridden by the <emphasis>--no-autostash</emphasis> and
\r
202 <emphasis>--autostash</emphasis> options of <xref linkend="git-rebase(1)" />.
\r
209 rebase.missingCommitsCheck
\r
213 If set to "warn", git rebase -i will print a warning if some
\r
214 commits are removed (e.g. a line was deleted), however the
\r
215 rebase will still proceed. If set to "error", it will print
\r
216 the previous warning and stop the rebase, <emphasis>git rebase
\r
217 --edit-todo</emphasis> can then be used to correct the error. If set to
\r
218 "ignore", no checking is done.
\r
219 To drop a commit without warning or error, use the <emphasis>drop</emphasis>
\r
220 command in the todo list.
\r
221 Defaults to "ignore".
\r
227 rebase.instructionFormat
\r
231 A format string, as specified in <xref linkend="git-log(1)" />, to be used for the
\r
232 todo list during an interactive rebase. The format will
\r
233 automatically have the long commit hash prepended to the format.
\r
239 rebase.abbreviateCommands
\r
243 If set to true, <emphasis>git rebase</emphasis> will use abbreviated command names in the
\r
244 todo list resulting in something like this:
\r
246 <screen> p deadbee The oneline of the commit
\r
247 p fa1afe1 The oneline of the next commit
\r
249 <simpara>instead of:</simpara>
\r
250 <screen> pick deadbee The oneline of the commit
\r
251 pick fa1afe1 The oneline of the next commit
\r
253 <simpara>Defaults to false.</simpara>
\r
258 rebase.rescheduleFailedExec
\r
262 Automatically reschedule <emphasis>exec</emphasis> commands that failed. This only makes
\r
263 sense in interactive mode (or when an <emphasis>--exec</emphasis> option was provided).
\r
264 This is the same as specifying the <emphasis>--reschedule-failed-exec</emphasis> option.
\r
270 <simplesect id="git-rebase(1)__options">
\r
271 <title>OPTIONS</title>
\r
275 --onto <newbase>
\r
279 Starting point at which to create the new commits. If the
\r
280 --onto option is not specified, the starting point is
\r
281 <upstream>. May be any valid commit, and not just an
\r
282 existing branch name.
\r
284 <simpara>As a special case, you may use "A...B" as a shortcut for the
\r
285 merge base of A and B if there is exactly one merge base. You can
\r
286 leave out at most one of A and B, in which case it defaults to HEAD.</simpara>
\r
295 Set the starting point at which to create the new commits to the
\r
296 merge base of <upstream> <branch>. Running
\r
297 <emphasis>git rebase --keep-base <upstream> <branch></emphasis> is equivalent to
\r
298 running <emphasis>git rebase --onto <upstream>… <upstream></emphasis>.
\r
300 <simpara>This option is useful in the case where one is developing a feature on
\r
301 top of an upstream branch. While the feature is being worked on, the
\r
302 upstream branch may advance and it may not be the best idea to keep
\r
303 rebasing on top of the upstream but to keep the base commit as-is.</simpara>
\r
304 <simpara>Although both this option and --fork-point find the merge base between
\r
305 <upstream> and <branch>, this option uses the merge base as the <emphasis>starting
\r
306 point</emphasis> on which new commits will be created, whereas --fork-point uses
\r
307 the merge base to determine the <emphasis>set of commits</emphasis> which will be rebased.</simpara>
\r
308 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
317 Upstream branch to compare against. May be any valid commit,
\r
318 not just an existing branch name. Defaults to the configured
\r
319 upstream for the current branch.
\r
329 Working branch; defaults to HEAD.
\r
339 Restart the rebasing process after having resolved a merge conflict.
\r
349 Abort the rebase operation and reset HEAD to the original
\r
350 branch. If <branch> was provided when the rebase operation was
\r
351 started, then HEAD will be reset to <branch>. Otherwise HEAD
\r
352 will be reset to where it was when the rebase operation was
\r
363 Abort the rebase operation but HEAD is not reset back to the
\r
364 original branch. The index and working tree are also left
\r
365 unchanged as a result. If a temporary stash entry was created
\r
366 using --autostash, it will be saved to the stash list.
\r
376 Use applying strategies to rebase (calling <emphasis>git-am</emphasis>
\r
377 internally). This option may become a no-op in the future
\r
378 once the merge backend handles everything the apply one does.
\r
380 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
385 --empty={drop,keep,ask}
\r
389 How to handle commits that are not empty to start and are not
\r
390 clean cherry-picks of any upstream commit, but which become
\r
391 empty after rebasing (because they contain a subset of already
\r
392 upstream changes). With drop (the default), commits that
\r
393 become empty are dropped. With keep, such commits are kept.
\r
394 With ask (implied by --interactive), the rebase will halt when
\r
395 an empty commit is applied allowing you to choose whether to
\r
396 drop it, edit files more, or just commit the empty changes.
\r
397 Other options, like --exec, will use the default of drop unless
\r
398 -i/--interactive is explicitly specified.
\r
400 <simpara>Note that commits which start empty are kept (unless --no-keep-empty
\r
401 is specified), and commits which are clean cherry-picks (as determined
\r
402 by <emphasis>git log --cherry-mark ...</emphasis>) are detected and dropped as a
\r
403 preliminary step (unless --reapply-cherry-picks is passed).</simpara>
\r
404 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
416 Do not keep commits that start empty before the rebase
\r
417 (i.e. that do not change anything from its parent) in the
\r
418 result. The default is to keep commits which start empty,
\r
419 since creating such commits requires passing the --allow-empty
\r
420 override flag to <emphasis>git commit</emphasis>, signifying that a user is very
\r
421 intentionally creating such a commit and thus wants to keep
\r
424 <simpara>Usage of this flag will probably be rare, since you can get rid of
\r
425 commits that start empty by just firing up an interactive rebase and
\r
426 removing the lines corresponding to the commits you don't want. This
\r
427 flag exists as a convenient shortcut, such as for cases where external
\r
428 tools generate many empty commits and you want them all removed.</simpara>
\r
429 <simpara>For commits which do not start empty but become empty after rebasing,
\r
430 see the --empty flag.</simpara>
\r
431 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
436 --reapply-cherry-picks
\r
439 --no-reapply-cherry-picks
\r
443 Reapply all clean cherry-picks of any upstream commit instead
\r
444 of preemptively dropping them. (If these commits then become
\r
445 empty after rebasing, because they contain a subset of already
\r
446 upstream changes, the behavior towards them is controlled by
\r
447 the <emphasis>--empty</emphasis> flag.)
\r
449 <simpara>By default (or if <emphasis>--no-reapply-cherry-picks</emphasis> is given), these commits
\r
450 will be automatically dropped. Because this necessitates reading all
\r
451 upstream commits, this can be expensive in repos with a large number
\r
452 of upstream commits that need to be read.</simpara>
\r
453 <simpara><emphasis>--reapply-cherry-picks</emphasis> allows rebase to forgo reading all upstream
\r
454 commits, potentially improving performance.</simpara>
\r
455 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
460 --allow-empty-message
\r
464 No-op. Rebasing commits with an empty message used to fail
\r
465 and this option would override that behavior, allowing commits
\r
466 with empty messages to be rebased. Now commits with an empty
\r
467 message do not cause rebasing to halt.
\r
469 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
478 Restart the rebasing process by skipping the current patch.
\r
488 Edit the todo list during an interactive rebase.
\r
494 --show-current-patch
\r
498 Show the current patch in an interactive rebase or when rebase
\r
499 is stopped because of conflicts. This is the equivalent of
\r
500 <emphasis>git show REBASE_HEAD</emphasis>.
\r
513 Use merging strategies to rebase. When the recursive (default) merge
\r
514 strategy is used, this allows rebase to be aware of renames on the
\r
515 upstream side. This is the default.
\r
517 <simpara>Note that a rebase merge works by replaying each commit from the working
\r
518 branch on top of the <upstream> branch. Because of this, when a merge
\r
519 conflict happens, the side reported as <emphasis>ours</emphasis> is the so-far rebased
\r
520 series, starting with <upstream>, and <emphasis>theirs</emphasis> is the working branch. In
\r
521 other words, the sides are swapped.</simpara>
\r
522 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
527 -s <strategy>
\r
530 --strategy=<strategy>
\r
534 Use the given merge strategy.
\r
535 If there is no <emphasis>-s</emphasis> option <emphasis>git merge-recursive</emphasis> is used
\r
536 instead. This implies --merge.
\r
538 <simpara>Because <emphasis>git rebase</emphasis> replays each commit from the working branch
\r
539 on top of the <upstream> branch using the given strategy, using
\r
540 the <emphasis>ours</emphasis> strategy simply empties all patches from the <branch>,
\r
541 which makes little sense.</simpara>
\r
542 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
547 -X <strategy-option>
\r
550 --strategy-option=<strategy-option>
\r
554 Pass the <strategy-option> through to the merge strategy.
\r
555 This implies <emphasis>--merge</emphasis> and, if no strategy has been
\r
556 specified, <emphasis>-s recursive</emphasis>. Note the reversal of <emphasis>ours</emphasis> and
\r
557 <emphasis>theirs</emphasis> as noted above for the <emphasis>-m</emphasis> option.
\r
559 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
564 --rerere-autoupdate
\r
567 --no-rerere-autoupdate
\r
571 Allow the rerere mechanism to update the index with the
\r
572 result of auto-conflict resolution if possible.
\r
581 --gpg-sign[=<keyid>]
\r
588 GPG-sign commits. The <emphasis>keyid</emphasis> argument is optional and
\r
589 defaults to the committer identity; if specified, it must be
\r
590 stuck to the option without a space. <emphasis>--no-gpg-sign</emphasis> is useful to
\r
591 countermand both <emphasis>commit.gpgSign</emphasis> configuration variable, and
\r
592 earlier <emphasis>--gpg-sign</emphasis>.
\r
605 Be quiet. Implies --no-stat.
\r
618 Be verbose. Implies --stat.
\r
628 Show a diffstat of what changed upstream since the last rebase. The
\r
629 diffstat is also controlled by the configuration option rebase.stat.
\r
642 Do not show a diffstat as part of the rebase process.
\r
652 This option bypasses the pre-rebase hook. See also <xref linkend="githooks(5)" />.
\r
662 Allows the pre-rebase hook to run, which is the default. This option can
\r
663 be used to override --no-verify. See also <xref linkend="githooks(5)" />.
\r
673 Ensure at least <n> lines of surrounding context match before
\r
674 and after each change. When fewer lines of surrounding
\r
675 context exist they all must match. By default no context is
\r
676 ever ignored. Implies --apply.
\r
678 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
693 Individually replay all rebased commits instead of fast-forwarding
\r
694 over the unchanged ones. This ensures that the entire history of
\r
695 the rebased branch is composed of new commits.
\r
697 <simpara>You may find this helpful after reverting a topic branch merge, as this option
\r
698 recreates the topic branch with fresh commits so it can be remerged
\r
699 successfully without needing to "revert the reversion" (see the
\r
700 <ulink url="https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html"><citetitle>revert-a-faulty-merge How-To</citetitle></ulink> for
\r
701 details).</simpara>
\r
713 Use reflog to find a better common ancestor between <upstream>
\r
714 and <branch> when calculating which commits have been
\r
715 introduced by <branch>.
\r
717 <simpara>When --fork-point is active, <emphasis>fork_point</emphasis> will be used instead of
\r
718 <upstream> to calculate the set of commits to rebase, where
\r
719 <emphasis>fork_point</emphasis> is the result of <emphasis>git merge-base --fork-point <upstream>
\r
720 <branch></emphasis> command (see <xref linkend="git-merge-base(1)" />). If <emphasis>fork_point</emphasis>
\r
721 ends up being empty, the <upstream> will be used as a fallback.</simpara>
\r
722 <simpara>If <upstream> is given on the command line, then the default is
\r
723 <emphasis>--no-fork-point</emphasis>, otherwise the default is <emphasis>--fork-point</emphasis>.</simpara>
\r
724 <simpara>If your branch was based on <upstream> but <upstream> was rewound and
\r
725 your branch contains commits which were dropped, this option can be used
\r
726 with <emphasis>--keep-base</emphasis> in order to drop those commits from your branch.</simpara>
\r
727 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
732 --ignore-whitespace
\r
735 --whitespace=<option>
\r
739 These flags are passed to the <emphasis>git apply</emphasis> program
\r
740 (see <xref linkend="git-apply(1)" />) that applies the patch.
\r
743 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
748 --committer-date-is-author-date
\r
755 These flags are passed to <emphasis>git am</emphasis> to easily change the dates
\r
756 of the rebased commits (see <xref linkend="git-am(1)" />).
\r
758 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
767 Add a Signed-off-by: trailer to all the rebased commits. Note
\r
768 that if <emphasis>--interactive</emphasis> is given then only commits marked to be
\r
769 picked, edited or reworded will have the trailer added.
\r
771 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
783 Make a list of the commits which are about to be rebased. Let the
\r
784 user edit that list before rebasing. This mode can also be used to
\r
785 split commits (see SPLITTING COMMITS below).
\r
787 <simpara>The commit list format can be changed by setting the configuration option
\r
788 rebase.instructionFormat. A customized instruction format will automatically
\r
789 have the long commit hash prepended to the format.</simpara>
\r
790 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
798 --rebase-merges[=(rebase-cousins|no-rebase-cousins)]
\r
802 By default, a rebase will simply drop merge commits from the todo
\r
803 list, and put the rebased commits into a single, linear branch.
\r
804 With <emphasis>--rebase-merges</emphasis>, the rebase will instead try to preserve
\r
805 the branching structure within the commits that are to be rebased,
\r
806 by recreating the merge commits. Any resolved merge conflicts or
\r
807 manual amendments in these merge commits will have to be
\r
808 resolved/re-applied manually.
\r
810 <simpara>By default, or when <emphasis>no-rebase-cousins</emphasis> was specified, commits which do not
\r
811 have <emphasis><upstream></emphasis> as direct ancestor will keep their original branch point,
\r
812 i.e. commits that would be excluded by <xref linkend="git-log(1)" />'s
\r
813 <emphasis>--ancestry-path</emphasis> option will keep their original ancestry by default. If
\r
814 the <emphasis>rebase-cousins</emphasis> mode is turned on, such commits are instead rebased
\r
815 onto <emphasis><upstream></emphasis> (or <emphasis><onto></emphasis>, if specified).</simpara>
\r
816 <simpara>The <emphasis>--rebase-merges</emphasis> mode is similar in spirit to the deprecated
\r
817 <emphasis>--preserve-merges</emphasis> but works with interactive rebases,
\r
818 where commits can be reordered, inserted and dropped at will.</simpara>
\r
819 <simpara>It is currently only possible to recreate the merge commits using the
\r
820 <emphasis>recursive</emphasis> merge strategy; Different merge strategies can be used only via
\r
821 explicit <emphasis>exec git merge -s <strategy> [...]</emphasis> commands.</simpara>
\r
822 <simpara>See also REBASING MERGES and INCOMPATIBLE OPTIONS below.</simpara>
\r
834 [DEPRECATED: use <emphasis>--rebase-merges</emphasis> instead] Recreate merge commits
\r
835 instead of flattening the history by replaying commits a merge commit
\r
836 introduces. Merge conflict resolutions or manual amendments to merge
\r
837 commits are not preserved.
\r
839 <simpara>This uses the <emphasis>--interactive</emphasis> machinery internally, but combining it
\r
840 with the <emphasis>--interactive</emphasis> option explicitly is generally not a good
\r
841 idea unless you know what you are doing (see BUGS below).</simpara>
\r
842 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
854 Append "exec <cmd>" after each line creating a commit in the
\r
855 final history. <cmd> will be interpreted as one or more shell
\r
856 commands. Any command that fails will interrupt the rebase,
\r
859 <simpara>You may execute several commands by either using one instance of <emphasis>--exec</emphasis>
\r
860 with several commands:</simpara>
\r
861 <literallayout class="monospaced">git rebase -i --exec "cmd1 && cmd2 && ..."</literallayout>
\r
862 <simpara>or by giving more than one <emphasis>--exec</emphasis>:</simpara>
\r
863 <literallayout class="monospaced">git rebase -i --exec "cmd1" --exec "cmd2" --exec ...</literallayout>
\r
864 <simpara>If <emphasis>--autosquash</emphasis> is used, "exec" lines will not be appended for
\r
865 the intermediate commits, and will only appear at the end of each
\r
866 squash/fixup series.</simpara>
\r
867 <simpara>This uses the <emphasis>--interactive</emphasis> machinery internally, but it can be run
\r
868 without an explicit <emphasis>--interactive</emphasis>.</simpara>
\r
869 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
878 Rebase all commits reachable from <branch>, instead of
\r
879 limiting them with an <upstream>. This allows you to rebase
\r
880 the root commit(s) on a branch. When used with --onto, it
\r
881 will skip changes already contained in <newbase> (instead of
\r
882 <upstream>) whereas without --onto it will operate on every change.
\r
883 When used together with both --onto and --preserve-merges,
\r
884 <emphasis>all</emphasis> root commits will be rewritten to have <newbase> as parent
\r
887 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
899 When the commit log message begins with "squash! …" (or
\r
900 "fixup! …"), and there is already a commit in the todo list that
\r
901 matches the same <emphasis>...</emphasis>, automatically modify the todo list of rebase
\r
902 -i so that the commit marked for squashing comes right after the
\r
903 commit to be modified, and change the action of the moved commit
\r
904 from <emphasis>pick</emphasis> to <emphasis>squash</emphasis> (or <emphasis>fixup</emphasis>). A commit matches the <emphasis>...</emphasis> if
\r
905 the commit subject matches, or if the <emphasis>...</emphasis> refers to the commit's
\r
906 hash. As a fall-back, partial matches of the commit subject work,
\r
907 too. The recommended way to create fixup/squash commits is by using
\r
908 the <emphasis>--fixup</emphasis>/<emphasis>--squash</emphasis> options of <xref linkend="git-commit(1)" />.
\r
910 <simpara>If the <emphasis>--autosquash</emphasis> option is enabled by default using the
\r
911 configuration variable <emphasis>rebase.autoSquash</emphasis>, this option can be
\r
912 used to override and disable this setting.</simpara>
\r
913 <simpara>See also INCOMPATIBLE OPTIONS below.</simpara>
\r
925 Automatically create a temporary stash entry before the operation
\r
926 begins, and apply it after the operation ends. This means
\r
927 that you can run rebase on a dirty worktree. However, use
\r
928 with care: the final stash application after a successful
\r
929 rebase might result in non-trivial conflicts.
\r
935 --reschedule-failed-exec
\r
938 --no-reschedule-failed-exec
\r
942 Automatically reschedule <emphasis>exec</emphasis> commands that failed. This only makes
\r
943 sense in interactive mode (or when an <emphasis>--exec</emphasis> option was provided).
\r
949 <simplesect id="git-rebase(1)__incompatible_options">
\r
950 <title>INCOMPATIBLE OPTIONS</title>
\r
951 <simpara>The following options:</simpara>
\r
960 --committer-date-is-author-date
\r
970 --ignore-whitespace
\r
984 <simpara>are incompatible with the following options:</simpara>
\r
1003 --allow-empty-message
\r
1043 --reapply-cherry-picks
\r
1053 --root when used in combination with --onto
\r
1057 <simpara>In addition, the following pairs of options are incompatible:</simpara>
\r
1061 --preserve-merges and --interactive
\r
1066 --preserve-merges and --signoff
\r
1071 --preserve-merges and --rebase-merges
\r
1076 --preserve-merges and --empty=
\r
1081 --keep-base and --onto
\r
1086 --keep-base and --root
\r
1091 --fork-point and --root
\r
1096 <simplesect id="git-rebase(1)__behavioral_differences">
\r
1097 <title>BEHAVIORAL DIFFERENCES</title>
\r
1098 <simpara>git rebase has two primary backends: apply and merge. (The apply
\r
1099 backend used to be known as the <emphasis>am</emphasis> backend, but the name led to
\r
1100 confusion as it looks like a verb instead of a noun. Also, the merge
\r
1101 backend used to be known as the interactive backend, but it is now
\r
1102 used for non-interactive cases as well. Both were renamed based on
\r
1103 lower-level functionality that underpinned each.) There are some
\r
1104 subtle differences in how these two backends behave:</simpara>
\r
1105 <section id="git-rebase(1)__empty_commits">
\r
1106 <title>Empty commits</title>
\r
1107 <simpara>The apply backend unfortunately drops intentionally empty commits, i.e.
\r
1108 commits that started empty, though these are rare in practice. It
\r
1109 also drops commits that become empty and has no option for controlling
\r
1110 this behavior.</simpara>
\r
1111 <simpara>The merge backend keeps intentionally empty commits by default (though
\r
1112 with -i they are marked as empty in the todo list editor, or they can
\r
1113 be dropped automatically with --no-keep-empty).</simpara>
\r
1114 <simpara>Similar to the apply backend, by default the merge backend drops
\r
1115 commits that become empty unless -i/--interactive is specified (in
\r
1116 which case it stops and asks the user what to do). The merge backend
\r
1117 also has an --empty={drop,keep,ask} option for changing the behavior
\r
1118 of handling commits that become empty.</simpara>
\r
1120 <section id="git-rebase(1)__directory_rename_detection">
\r
1121 <title>Directory rename detection</title>
\r
1122 <simpara>Due to the lack of accurate tree information (arising from
\r
1123 constructing fake ancestors with the limited information available in
\r
1124 patches), directory rename detection is disabled in the apply backend.
\r
1125 Disabled directory rename detection means that if one side of history
\r
1126 renames a directory and the other adds new files to the old directory,
\r
1127 then the new files will be left behind in the old directory without
\r
1128 any warning at the time of rebasing that you may want to move these
\r
1129 files into the new directory.</simpara>
\r
1130 <simpara>Directory rename detection works with the merge backend to provide you
\r
1131 warnings in such cases.</simpara>
\r
1133 <section id="git-rebase(1)__context">
\r
1134 <title>Context</title>
\r
1135 <simpara>The apply backend works by creating a sequence of patches (by calling
\r
1136 <emphasis>format-patch</emphasis> internally), and then applying the patches in sequence
\r
1137 (calling <emphasis>am</emphasis> internally). Patches are composed of multiple hunks,
\r
1138 each with line numbers, a context region, and the actual changes. The
\r
1139 line numbers have to be taken with some fuzz, since the other side
\r
1140 will likely have inserted or deleted lines earlier in the file. The
\r
1141 context region is meant to help find how to adjust the line numbers in
\r
1142 order to apply the changes to the right lines. However, if multiple
\r
1143 areas of the code have the same surrounding lines of context, the
\r
1144 wrong one can be picked. There are real-world cases where this has
\r
1145 caused commits to be reapplied incorrectly with no conflicts reported.
\r
1146 Setting diff.context to a larger value may prevent such types of
\r
1147 problems, but increases the chance of spurious conflicts (since it
\r
1148 will require more lines of matching context to apply).</simpara>
\r
1149 <simpara>The merge backend works with a full copy of each relevant file,
\r
1150 insulating it from these types of problems.</simpara>
\r
1152 <section id="git-rebase(1)__labelling_of_conflicts_markers">
\r
1153 <title>Labelling of conflicts markers</title>
\r
1154 <simpara>When there are content conflicts, the merge machinery tries to
\r
1155 annotate each side's conflict markers with the commits where the
\r
1156 content came from. Since the apply backend drops the original
\r
1157 information about the rebased commits and their parents (and instead
\r
1158 generates new fake commits based off limited information in the
\r
1159 generated patches), those commits cannot be identified; instead it has
\r
1160 to fall back to a commit summary. Also, when merge.conflictStyle is
\r
1161 set to diff3, the apply backend will use "constructed merge base" to
\r
1162 label the content from the merge base, and thus provide no information
\r
1163 about the merge base commit whatsoever.</simpara>
\r
1164 <simpara>The merge backend works with the full commits on both sides of history
\r
1165 and thus has no such limitations.</simpara>
\r
1167 <section id="git-rebase(1)__hooks">
\r
1168 <title>Hooks</title>
\r
1169 <simpara>The apply backend has not traditionally called the post-commit hook,
\r
1170 while the merge backend has. Both have called the post-checkout hook,
\r
1171 though the merge backend has squelched its output. Further, both
\r
1172 backends only call the post-checkout hook with the starting point
\r
1173 commit of the rebase, not the intermediate commits nor the final
\r
1174 commit. In each case, the calling of these hooks was by accident of
\r
1175 implementation rather than by design (both backends were originally
\r
1176 implemented as shell scripts and happened to invoke other commands
\r
1177 like <emphasis>git checkout</emphasis> or <emphasis>git commit</emphasis> that would call the hooks). Both
\r
1178 backends should have the same behavior, though it is not entirely
\r
1179 clear which, if any, is correct. We will likely make rebase stop
\r
1180 calling either of these hooks in the future.</simpara>
\r
1182 <section id="git-rebase(1)__interruptability">
\r
1183 <title>Interruptability</title>
\r
1184 <simpara>The apply backend has safety problems with an ill-timed interrupt; if
\r
1185 the user presses Ctrl-C at the wrong time to try to abort the rebase,
\r
1186 the rebase can enter a state where it cannot be aborted with a
\r
1187 subsequent <emphasis>git rebase --abort</emphasis>. The merge backend does not appear to
\r
1188 suffer from the same shortcoming. (See
\r
1189 <ulink url="https://lore.kernel.org/git/20200207132152.GC2868@szeder.dev/">https://lore.kernel.org/git/20200207132152.GC2868@szeder.dev/</ulink> for
\r
1190 details.)</simpara>
\r
1192 <section id="git-rebase(1)__commit_rewording">
\r
1193 <title>Commit Rewording</title>
\r
1194 <simpara>When a conflict occurs while rebasing, rebase stops and asks the user
\r
1195 to resolve. Since the user may need to make notable changes while
\r
1196 resolving conflicts, after conflicts are resolved and the user has run
\r
1197 <emphasis>git rebase --continue</emphasis>, the rebase should open an editor and ask the
\r
1198 user to update the commit message. The merge backend does this, while
\r
1199 the apply backend blindly applies the original commit message.</simpara>
\r
1201 <section id="git-rebase(1)__miscellaneous_differences">
\r
1202 <title>Miscellaneous differences</title>
\r
1203 <simpara>There are a few more behavioral differences that most folks would
\r
1204 probably consider inconsequential but which are mentioned for
\r
1205 completeness:</simpara>
\r
1209 Reflog: The two backends will use different wording when describing
\r
1210 the changes made in the reflog, though both will make use of the
\r
1216 Progress, informational, and error messages: The two backends
\r
1217 provide slightly different progress and informational messages.
\r
1218 Also, the apply backend writes error messages (such as "Your files
\r
1219 would be overwritten…") to stdout, while the merge backend writes
\r
1225 State directories: The two backends keep their state in different
\r
1226 directories under .git/
\r
1232 <simplesect id="git-rebase(1)__merge_strategies">
\r
1233 <title>MERGE STRATEGIES</title>
\r
1234 <simpara>The merge mechanism (<emphasis>git merge</emphasis> and <emphasis>git pull</emphasis> commands) allows the
\r
1235 backend <emphasis>merge strategies</emphasis> to be chosen with <emphasis>-s</emphasis> option. Some strategies
\r
1236 can also take their own options, which can be passed by giving <emphasis>-X<option></emphasis>
\r
1237 arguments to <emphasis>git merge</emphasis> and/or <emphasis>git pull</emphasis>.</simpara>
\r
1245 This can only resolve two heads (i.e. the current branch
\r
1246 and another branch you pulled from) using a 3-way merge
\r
1247 algorithm. It tries to carefully detect criss-cross
\r
1248 merge ambiguities and is considered generally safe and
\r
1259 This can only resolve two heads using a 3-way merge
\r
1260 algorithm. When there is more than one common
\r
1261 ancestor that can be used for 3-way merge, it creates a
\r
1262 merged tree of the common ancestors and uses that as
\r
1263 the reference tree for the 3-way merge. This has been
\r
1264 reported to result in fewer merge conflicts without
\r
1265 causing mismerges by tests done on actual merge commits
\r
1266 taken from Linux 2.6 kernel development history.
\r
1267 Additionally this can detect and handle merges involving
\r
1268 renames, but currently cannot make use of detected
\r
1269 copies. This is the default merge strategy when pulling
\r
1270 or merging one branch.
\r
1272 <simpara>The <emphasis>recursive</emphasis> strategy can take the following options:</simpara>
\r
1280 This option forces conflicting hunks to be auto-resolved cleanly by
\r
1281 favoring <emphasis>our</emphasis> version. Changes from the other tree that do not
\r
1282 conflict with our side are reflected in the merge result.
\r
1283 For a binary file, the entire contents are taken from our side.
\r
1285 <simpara>This should not be confused with the <emphasis>ours</emphasis> merge strategy, which does not
\r
1286 even look at what the other tree contains at all. It discards everything
\r
1287 the other tree did, declaring <emphasis>our</emphasis> history contains all that happened in it.</simpara>
\r
1296 This is the opposite of <emphasis>ours</emphasis>; note that, unlike <emphasis>ours</emphasis>, there is
\r
1297 no <emphasis>theirs</emphasis> merge strategy to confuse this merge option with.
\r
1307 With this option, <emphasis>merge-recursive</emphasis> spends a little extra time
\r
1308 to avoid mismerges that sometimes occur due to unimportant
\r
1309 matching lines (e.g., braces from distinct functions). Use
\r
1310 this when the branches to be merged have diverged wildly.
\r
1311 See also <xref linkend="git-diff(1)" /> <emphasis>--patience</emphasis>.
\r
1317 diff-algorithm=[patience|minimal|histogram|myers]
\r
1321 Tells <emphasis>merge-recursive</emphasis> to use a different diff algorithm, which
\r
1322 can help avoid mismerges that occur due to unimportant matching
\r
1323 lines (such as braces from distinct functions). See also
\r
1324 <xref linkend="git-diff(1)" /> <emphasis>--diff-algorithm</emphasis>.
\r
1330 ignore-space-change
\r
1336 ignore-space-at-eol
\r
1343 Treats lines with the indicated type of whitespace change as
\r
1344 unchanged for the sake of a three-way merge. Whitespace
\r
1345 changes mixed with other changes to a line are not ignored.
\r
1346 See also <xref linkend="git-diff(1)" /> <emphasis>-b</emphasis>, <emphasis>-w</emphasis>,
\r
1347 <emphasis>--ignore-space-at-eol</emphasis>, and <emphasis>--ignore-cr-at-eol</emphasis>.
\r
1352 If <emphasis>their</emphasis> version only introduces whitespace changes to a line,
\r
1353 <emphasis>our</emphasis> version is used;
\r
1358 If <emphasis>our</emphasis> version introduces whitespace changes but <emphasis>their</emphasis>
\r
1359 version includes a substantial change, <emphasis>their</emphasis> version is used;
\r
1364 Otherwise, the merge proceeds in the usual way.
\r
1376 This runs a virtual check-out and check-in of all three stages
\r
1377 of a file when resolving a three-way merge. This option is
\r
1378 meant to be used when merging branches with different clean
\r
1379 filters or end-of-line normalization rules. See "Merging
\r
1380 branches with differing checkin/checkout attributes" in
\r
1381 <xref linkend="gitattributes(5)" /> for details.
\r
1391 Disables the <emphasis>renormalize</emphasis> option. This overrides the
\r
1392 <emphasis>merge.renormalize</emphasis> configuration variable.
\r
1402 Turn off rename detection. This overrides the <emphasis>merge.renames</emphasis>
\r
1403 configuration variable.
\r
1404 See also <xref linkend="git-diff(1)" /> <emphasis>--no-renames</emphasis>.
\r
1410 find-renames[=<n>]
\r
1414 Turn on rename detection, optionally setting the similarity
\r
1415 threshold. This is the default. This overrides the
\r
1416 <emphasis>merge.renames</emphasis> configuration variable.
\r
1417 See also <xref linkend="git-diff(1)" /> <emphasis>--find-renames</emphasis>.
\r
1423 rename-threshold=<n>
\r
1427 Deprecated synonym for <emphasis>find-renames=<n></emphasis>.
\r
1433 subtree[=<path>]
\r
1437 This option is a more advanced form of <emphasis>subtree</emphasis> strategy, where
\r
1438 the strategy makes a guess on how two trees must be shifted to
\r
1439 match with each other when merging. Instead, the specified path
\r
1440 is prefixed (or stripped from the beginning) to make the shape of
\r
1441 two trees to match.
\r
1454 This resolves cases with more than two heads, but refuses to do
\r
1455 a complex merge that needs manual resolution. It is
\r
1456 primarily meant to be used for bundling topic branch
\r
1457 heads together. This is the default merge strategy when
\r
1458 pulling or merging more than one branch.
\r
1468 This resolves any number of heads, but the resulting tree of the
\r
1469 merge is always that of the current branch head, effectively
\r
1470 ignoring all changes from all other branches. It is meant to
\r
1471 be used to supersede old development history of side
\r
1472 branches. Note that this is different from the -Xours option to
\r
1473 the <emphasis>recursive</emphasis> merge strategy.
\r
1483 This is a modified recursive strategy. When merging trees A and
\r
1484 B, if B corresponds to a subtree of A, B is first adjusted to
\r
1485 match the tree structure of A, instead of reading the trees at
\r
1486 the same level. This adjustment is also done to the common
\r
1492 <simpara>With the strategies that use 3-way merge (including the default, <emphasis>recursive</emphasis>),
\r
1493 if a change is made on both branches, but later reverted on one of the
\r
1494 branches, that change will be present in the merged result; some people find
\r
1495 this behavior confusing. It occurs because only the heads and the merge base
\r
1496 are considered when performing a merge, not the individual commits. The merge
\r
1497 algorithm therefore considers the reverted change as no change at all, and
\r
1498 substitutes the changed version instead.</simpara>
\r
1500 <simplesect id="git-rebase(1)__notes">
\r
1501 <title>NOTES</title>
\r
1502 <simpara>You should understand the implications of using <emphasis>git rebase</emphasis> on a
\r
1503 repository that you share. See also RECOVERING FROM UPSTREAM REBASE
\r
1505 <simpara>When the git-rebase command is run, it will first execute a "pre-rebase"
\r
1506 hook if one exists. You can use this hook to do sanity checks and
\r
1507 reject the rebase if it isn't appropriate. Please see the template
\r
1508 pre-rebase hook script for an example.</simpara>
\r
1509 <simpara>Upon completion, <branch> will be the current branch.</simpara>
\r
1511 <simplesect id="git-rebase(1)__interactive_mode">
\r
1512 <title>INTERACTIVE MODE</title>
\r
1513 <simpara>Rebasing interactively means that you have a chance to edit the commits
\r
1514 which are rebased. You can reorder the commits, and you can
\r
1515 remove them (weeding out bad or otherwise unwanted patches).</simpara>
\r
1516 <simpara>The interactive mode is meant for this type of workflow:</simpara>
\r
1517 <orderedlist numeration="arabic">
\r
1520 have a wonderful idea
\r
1530 prepare a series for submission
\r
1539 <simpara>where point 2. consists of several instances of</simpara>
\r
1540 <simpara>a) regular use</simpara>
\r
1541 <orderedlist numeration="arabic">
\r
1544 finish something worthy of a commit
\r
1553 <simpara>b) independent fixup</simpara>
\r
1554 <orderedlist numeration="arabic">
\r
1557 realize that something does not work
\r
1571 <simpara>Sometimes the thing fixed in b.2. cannot be amended to the not-quite
\r
1572 perfect commit it fixes, because that commit is buried deeply in a
\r
1573 patch series. That is exactly what interactive rebase is for: use it
\r
1574 after plenty of "a"s and "b"s, by rearranging and editing
\r
1575 commits, and squashing multiple commits into one.</simpara>
\r
1576 <simpara>Start it with the last commit you want to retain as-is:</simpara>
\r
1577 <literallayout class="monospaced">git rebase -i <after-this-commit></literallayout>
\r
1578 <simpara>An editor will be fired up with all the commits in your current branch
\r
1579 (ignoring merge commits), which come after the given commit. You can
\r
1580 reorder the commits in this list to your heart's content, and you can
\r
1581 remove them. The list looks more or less like this:</simpara>
\r
1582 <screen>pick deadbee The oneline of this commit
\r
1583 pick fa1afe1 The oneline of the next commit
\r
1585 <simpara>The oneline descriptions are purely for your pleasure; <emphasis>git rebase</emphasis> will
\r
1586 not look at them but at the commit names ("deadbee" and "fa1afe1" in this
\r
1587 example), so do not delete or edit the names.</simpara>
\r
1588 <simpara>By replacing the command "pick" with the command "edit", you can tell
\r
1589 <emphasis>git rebase</emphasis> to stop after applying that commit, so that you can edit
\r
1590 the files and/or the commit message, amend the commit, and continue
\r
1591 rebasing.</simpara>
\r
1592 <simpara>To interrupt the rebase (just like an "edit" command would do, but without
\r
1593 cherry-picking any commit first), use the "break" command.</simpara>
\r
1594 <simpara>If you just want to edit the commit message for a commit, replace the
\r
1595 command "pick" with the command "reword".</simpara>
\r
1596 <simpara>To drop a commit, replace the command "pick" with "drop", or just
\r
1597 delete the matching line.</simpara>
\r
1598 <simpara>If you want to fold two or more commits into one, replace the command
\r
1599 "pick" for the second and subsequent commits with "squash" or "fixup".
\r
1600 If the commits had different authors, the folded commit will be
\r
1601 attributed to the author of the first commit. The suggested commit
\r
1602 message for the folded commit is the concatenation of the commit
\r
1603 messages of the first commit and of those with the "squash" command,
\r
1604 but omits the commit messages of commits with the "fixup" command.</simpara>
\r
1605 <simpara><emphasis>git rebase</emphasis> will stop when "pick" has been replaced with "edit" or
\r
1606 when a command fails due to merge errors. When you are done editing
\r
1607 and/or resolving conflicts you can continue with <emphasis>git rebase --continue</emphasis>.</simpara>
\r
1608 <simpara>For example, if you want to reorder the last 5 commits, such that what
\r
1609 was HEAD~4 becomes the new HEAD. To achieve that, you would call
\r
1610 <emphasis>git rebase</emphasis> like this:</simpara>
\r
1611 <screen>$ git rebase -i HEAD~5</screen>
\r
1612 <simpara>And move the first patch to the end of the list.</simpara>
\r
1613 <simpara>You might want to recreate merge commits, e.g. if you have a history
\r
1614 like this:</simpara>
\r
1619 ---o---O---P---Q</screen>
\r
1620 <simpara>Suppose you want to rebase the side branch starting at "A" to "Q". Make
\r
1621 sure that the current HEAD is "B", and call</simpara>
\r
1622 <screen>$ git rebase -i -r --onto Q O</screen>
\r
1623 <simpara>Reordering and editing commits usually creates untested intermediate
\r
1624 steps. You may want to check that your history editing did not break
\r
1625 anything by running a test, or at least recompiling at intermediate
\r
1626 points in history by using the "exec" command (shortcut "x"). You may
\r
1627 do so by creating a todo list like this one:</simpara>
\r
1628 <screen>pick deadbee Implement feature XXX
\r
1629 fixup f1a5c00 Fix to feature XXX
\r
1631 pick c0ffeee The oneline of the next commit
\r
1632 edit deadbab The oneline of the commit after
\r
1633 exec cd subdir; make test
\r
1635 <simpara>The interactive rebase will stop when a command fails (i.e. exits with
\r
1636 non-0 status) to give you an opportunity to fix the problem. You can
\r
1637 continue with <emphasis>git rebase --continue</emphasis>.</simpara>
\r
1638 <simpara>The "exec" command launches the command in a shell (the one specified
\r
1639 in <emphasis>$SHELL</emphasis>, or the default shell if <emphasis>$SHELL</emphasis> is not set), so you can
\r
1640 use shell features (like "cd", ">", ";" …). The command is run from
\r
1641 the root of the working tree.</simpara>
\r
1642 <screen>$ git rebase -i --exec "make test"</screen>
\r
1643 <simpara>This command lets you check that intermediate commits are compilable.
\r
1644 The todo list becomes like that:</simpara>
\r
1645 <screen>pick 5928aea one
\r
1649 pick ba46169 three
\r
1652 exec make test</screen>
\r
1654 <simplesect id="git-rebase(1)__splitting_commits">
\r
1655 <title>SPLITTING COMMITS</title>
\r
1656 <simpara>In interactive mode, you can mark commits with the action "edit". However,
\r
1657 this does not necessarily mean that <emphasis>git rebase</emphasis> expects the result of this
\r
1658 edit to be exactly one commit. Indeed, you can undo the commit, or you can
\r
1659 add other commits. This can be used to split a commit into two:</simpara>
\r
1663 Start an interactive rebase with <emphasis>git rebase -i <commit>^</emphasis>, where
\r
1664 <commit> is the commit you want to split. In fact, any commit range
\r
1665 will do, as long as it contains that commit.
\r
1670 Mark the commit you want to split with the action "edit".
\r
1675 When it comes to editing that commit, execute <emphasis>git reset HEAD^</emphasis>. The
\r
1676 effect is that the HEAD is rewound by one, and the index follows suit.
\r
1677 However, the working tree stays the same.
\r
1682 Now add the changes to the index that you want to have in the first
\r
1683 commit. You can use <emphasis>git add</emphasis> (possibly interactively) or
\r
1684 <emphasis>git gui</emphasis> (or both) to do that.
\r
1689 Commit the now-current index with whatever commit message is appropriate
\r
1695 Repeat the last two steps until your working tree is clean.
\r
1700 Continue the rebase with <emphasis>git rebase --continue</emphasis>.
\r
1704 <simpara>If you are not absolutely sure that the intermediate revisions are
\r
1705 consistent (they compile, pass the testsuite, etc.) you should use
\r
1706 <emphasis>git stash</emphasis> to stash away the not-yet-committed changes
\r
1707 after each commit, test, and amend the commit if fixes are necessary.</simpara>
\r
1709 <simplesect id="git-rebase(1)__recovering_from_upstream_rebase">
\r
1710 <title>RECOVERING FROM UPSTREAM REBASE</title>
\r
1711 <simpara>Rebasing (or any other form of rewriting) a branch that others have
\r
1712 based work on is a bad idea: anyone downstream of it is forced to
\r
1713 manually fix their history. This section explains how to do the fix
\r
1714 from the downstream's point of view. The real fix, however, would be
\r
1715 to avoid rebasing the upstream in the first place.</simpara>
\r
1716 <simpara>To illustrate, suppose you are in a situation where someone develops a
\r
1717 <emphasis>subsystem</emphasis> branch, and you are working on a <emphasis>topic</emphasis> that is dependent
\r
1718 on this <emphasis>subsystem</emphasis>. You might end up with a history like the
\r
1719 following:</simpara>
\r
1720 <screen> o---o---o---o---o---o---o---o master
\r
1722 o---o---o---o---o subsystem
\r
1724 *---*---* topic</screen>
\r
1725 <simpara>If <emphasis>subsystem</emphasis> is rebased against <emphasis>master</emphasis>, the following happens:</simpara>
\r
1726 <screen> o---o---o---o---o---o---o---o master
\r
1728 o---o---o---o---o o'--o'--o'--o'--o' subsystem
\r
1730 *---*---* topic</screen>
\r
1731 <simpara>If you now continue development as usual, and eventually merge <emphasis>topic</emphasis>
\r
1732 to <emphasis>subsystem</emphasis>, the commits from <emphasis>subsystem</emphasis> will remain duplicated forever:</simpara>
\r
1733 <screen> o---o---o---o---o---o---o---o master
\r
1735 o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
\r
1737 *---*---*-..........-*--* topic</screen>
\r
1738 <simpara>Such duplicates are generally frowned upon because they clutter up
\r
1739 history, making it harder to follow. To clean things up, you need to
\r
1740 transplant the commits on <emphasis>topic</emphasis> to the new <emphasis>subsystem</emphasis> tip, i.e.,
\r
1741 rebase <emphasis>topic</emphasis>. This becomes a ripple effect: anyone downstream from
\r
1742 <emphasis>topic</emphasis> is forced to rebase too, and so on!</simpara>
\r
1743 <simpara>There are two kinds of fixes, discussed in the following subsections:</simpara>
\r
1747 Easy case: The changes are literally the same.
\r
1751 This happens if the <emphasis>subsystem</emphasis> rebase was a simple rebase and
\r
1758 Hard case: The changes are not the same.
\r
1762 This happens if the <emphasis>subsystem</emphasis> rebase had conflicts, or used
\r
1763 <emphasis>--interactive</emphasis> to omit, edit, squash, or fixup commits; or
\r
1764 if the upstream used one of <emphasis>commit --amend</emphasis>, <emphasis>reset</emphasis>, or
\r
1765 a full history rewriting command like
\r
1766 <ulink url="https://github.com/newren/git-filter-repo"><emphasis>filter-repo</emphasis></ulink>.
\r
1771 <section id="git-rebase(1)__the_easy_case">
\r
1772 <title>The easy case</title>
\r
1773 <simpara>Only works if the changes (patch IDs based on the diff contents) on
\r
1774 <emphasis>subsystem</emphasis> are literally the same before and after the rebase
\r
1775 <emphasis>subsystem</emphasis> did.</simpara>
\r
1776 <simpara>In that case, the fix is easy because <emphasis>git rebase</emphasis> knows to skip
\r
1777 changes that are already present in the new upstream (unless
\r
1778 <emphasis>--reapply-cherry-picks</emphasis> is given). So if you say
\r
1779 (assuming you're on <emphasis>topic</emphasis>)</simpara>
\r
1780 <screen> $ git rebase subsystem</screen>
\r
1781 <simpara>you will end up with the fixed history</simpara>
\r
1782 <screen> o---o---o---o---o---o---o---o master
\r
1784 o'--o'--o'--o'--o' subsystem
\r
1786 *---*---* topic</screen>
\r
1788 <section id="git-rebase(1)__the_hard_case">
\r
1789 <title>The hard case</title>
\r
1790 <simpara>Things get more complicated if the <emphasis>subsystem</emphasis> changes do not exactly
\r
1791 correspond to the ones before the rebase.</simpara>
\r
1792 <note><simpara>While an "easy case recovery" sometimes appears to be successful
\r
1793 even in the hard case, it may have unintended consequences. For
\r
1794 example, a commit that was removed via <emphasis>git rebase
\r
1795 --interactive</emphasis> will be <emphasis role="strong">resurrected</emphasis>!</simpara></note>
\r
1796 <simpara>The idea is to manually tell <emphasis>git rebase</emphasis> "where the old <emphasis>subsystem</emphasis>
\r
1797 ended and your <emphasis>topic</emphasis> began", that is, what the old merge base
\r
1798 between them was. You will have to find a way to name the last commit
\r
1799 of the old <emphasis>subsystem</emphasis>, for example:</simpara>
\r
1803 With the <emphasis>subsystem</emphasis> reflog: after <emphasis>git fetch</emphasis>, the old tip of
\r
1804 <emphasis>subsystem</emphasis> is at <emphasis>subsystem@{1}</emphasis>. Subsequent fetches will
\r
1805 increase the number. (See <xref linkend="git-reflog(1)" />.)
\r
1810 Relative to the tip of <emphasis>topic</emphasis>: knowing that your <emphasis>topic</emphasis> has three
\r
1811 commits, the old tip of <emphasis>subsystem</emphasis> must be <emphasis>topic~3</emphasis>.
\r
1815 <simpara>You can then transplant the old <emphasis>subsystem..topic</emphasis> to the new tip by
\r
1816 saying (for the reflog case, and assuming you are on <emphasis>topic</emphasis> already):</simpara>
\r
1817 <screen> $ git rebase --onto subsystem subsystem@{1}</screen>
\r
1818 <simpara>The ripple effect of a "hard case" recovery is especially bad:
\r
1819 <emphasis>everyone</emphasis> downstream from <emphasis>topic</emphasis> will now have to perform a "hard
\r
1820 case" recovery too!</simpara>
\r
1823 <simplesect id="git-rebase(1)__rebasing_merges">
\r
1824 <title>REBASING MERGES</title>
\r
1825 <simpara>The interactive rebase command was originally designed to handle
\r
1826 individual patch series. As such, it makes sense to exclude merge
\r
1827 commits from the todo list, as the developer may have merged the
\r
1828 then-current <emphasis>master</emphasis> while working on the branch, only to rebase
\r
1829 all the commits onto <emphasis>master</emphasis> eventually (skipping the merge
\r
1830 commits).</simpara>
\r
1831 <simpara>However, there are legitimate reasons why a developer may want to
\r
1832 recreate merge commits: to keep the branch structure (or "commit
\r
1833 topology") when working on multiple, inter-related branches.</simpara>
\r
1834 <simpara>In the following example, the developer works on a topic branch that
\r
1835 refactors the way buttons are defined, and on another topic branch
\r
1836 that uses that refactoring to implement a "Report a bug" button. The
\r
1837 output of <emphasis>git log --graph --format=%s -5</emphasis> may look like this:</simpara>
\r
1838 <screen>* Merge branch 'report-a-bug'
\r
1840 | * Add the feedback button
\r
1841 * | Merge branch 'refactor-button'
\r
1844 | * Use the Button class for all buttons
\r
1845 | * Extract a generic Button class from the DownloadButton one</screen>
\r
1846 <simpara>The developer might want to rebase those commits to a newer <emphasis>master</emphasis>
\r
1847 while keeping the branch topology, for example when the first topic
\r
1848 branch is expected to be integrated into <emphasis>master</emphasis> much earlier than the
\r
1849 second one, say, to resolve merge conflicts with changes to the
\r
1850 DownloadButton class that made it into <emphasis>master</emphasis>.</simpara>
\r
1851 <simpara>This rebase can be performed using the <emphasis>--rebase-merges</emphasis> option.
\r
1852 It will generate a todo list looking like this:</simpara>
\r
1853 <screen>label onto
\r
1855 # Branch: refactor-button
\r
1857 pick 123456 Extract a generic Button class from the DownloadButton one
\r
1858 pick 654321 Use the Button class for all buttons
\r
1859 label refactor-button
\r
1861 # Branch: report-a-bug
\r
1862 reset refactor-button # Use the Button class for all buttons
\r
1863 pick abcdef Add the feedback button
\r
1864 label report-a-bug
\r
1867 merge -C a1b2c3 refactor-button # Merge 'refactor-button'
\r
1868 merge -C 6f5e4d report-a-bug # Merge 'report-a-bug'</screen>
\r
1869 <simpara>In contrast to a regular interactive rebase, there are <emphasis>label</emphasis>, <emphasis>reset</emphasis>
\r
1870 and <emphasis>merge</emphasis> commands in addition to <emphasis>pick</emphasis> ones.</simpara>
\r
1871 <simpara>The <emphasis>label</emphasis> command associates a label with the current HEAD when that
\r
1872 command is executed. These labels are created as worktree-local refs
\r
1873 (<emphasis>refs/rewritten/<label></emphasis>) that will be deleted when the rebase
\r
1874 finishes. That way, rebase operations in multiple worktrees linked to
\r
1875 the same repository do not interfere with one another. If the <emphasis>label</emphasis>
\r
1876 command fails, it is rescheduled immediately, with a helpful message how
\r
1877 to proceed.</simpara>
\r
1878 <simpara>The <emphasis>reset</emphasis> command resets the HEAD, index and worktree to the specified
\r
1879 revision. It is similar to an <emphasis>exec git reset --hard <label></emphasis>, but
\r
1880 refuses to overwrite untracked files. If the <emphasis>reset</emphasis> command fails, it is
\r
1881 rescheduled immediately, with a helpful message how to edit the todo list
\r
1882 (this typically happens when a <emphasis>reset</emphasis> command was inserted into the todo
\r
1883 list manually and contains a typo).</simpara>
\r
1884 <simpara>The <emphasis>merge</emphasis> command will merge the specified revision(s) into whatever
\r
1885 is HEAD at that time. With <emphasis>-C <original-commit></emphasis>, the commit message of
\r
1886 the specified merge commit will be used. When the <emphasis>-C</emphasis> is changed to
\r
1887 a lower-case <emphasis>-c</emphasis>, the message will be opened in an editor after a
\r
1888 successful merge so that the user can edit the message.</simpara>
\r
1889 <simpara>If a <emphasis>merge</emphasis> command fails for any reason other than merge conflicts (i.e.
\r
1890 when the merge operation did not even start), it is rescheduled immediately.</simpara>
\r
1891 <simpara>At this time, the <emphasis>merge</emphasis> command will <emphasis role="strong">always</emphasis> use the <emphasis>recursive</emphasis>
\r
1892 merge strategy for regular merges, and <emphasis>octopus</emphasis> for octopus merges,
\r
1893 with no way to choose a different one. To work around
\r
1894 this, an <emphasis>exec</emphasis> command can be used to call <emphasis>git merge</emphasis> explicitly,
\r
1895 using the fact that the labels are worktree-local refs (the ref
\r
1896 <emphasis>refs/rewritten/onto</emphasis> would correspond to the label <emphasis>onto</emphasis>, for example).</simpara>
\r
1897 <simpara>Note: the first command (<emphasis>label onto</emphasis>) labels the revision onto which
\r
1898 the commits are rebased; The name <emphasis>onto</emphasis> is just a convention, as a nod
\r
1899 to the <emphasis>--onto</emphasis> option.</simpara>
\r
1900 <simpara>It is also possible to introduce completely new merge commits from scratch
\r
1901 by adding a command of the form <emphasis>merge <merge-head></emphasis>. This form will
\r
1902 generate a tentative commit message and always open an editor to let the
\r
1903 user edit it. This can be useful e.g. when a topic branch turns out to
\r
1904 address more than a single concern and wants to be split into two or
\r
1905 even more topic branches. Consider this todo list:</simpara>
\r
1906 <screen>pick 192837 Switch from GNU Makefiles to CMake
\r
1907 pick 5a6c7e Document the switch to CMake
\r
1908 pick 918273 Fix detection of OpenSSL in CMake
\r
1909 pick afbecd http: add support for TLS v1.3
\r
1910 pick fdbaec Fix detection of cURL in CMake on Windows</screen>
\r
1911 <simpara>The one commit in this list that is not related to CMake may very well
\r
1912 have been motivated by working on fixing all those bugs introduced by
\r
1913 switching to CMake, but it addresses a different concern. To split this
\r
1914 branch into two topic branches, the todo list could be edited like this:</simpara>
\r
1915 <screen>label onto
\r
1917 pick afbecd http: add support for TLS v1.3
\r
1921 pick 192837 Switch from GNU Makefiles to CMake
\r
1922 pick 918273 Fix detection of OpenSSL in CMake
\r
1923 pick fdbaec Fix detection of cURL in CMake on Windows
\r
1924 pick 5a6c7e Document the switch to CMake
\r
1929 merge cmake</screen>
\r
1931 <simplesect id="git-rebase(1)__bugs">
\r
1932 <title>BUGS</title>
\r
1933 <simpara>The todo list presented by the deprecated <emphasis>--preserve-merges --interactive</emphasis>
\r
1934 does not represent the topology of the revision graph (use <emphasis>--rebase-merges</emphasis>
\r
1935 instead). Editing commits and rewording their commit messages should work
\r
1936 fine, but attempts to reorder commits tend to produce counterintuitive results.
\r
1937 Use <emphasis>--rebase-merges</emphasis> in such scenarios instead.</simpara>
\r
1938 <simpara>For example, an attempt to rearrange</simpara>
\r
1939 <screen>1 --- 2 --- 3 --- 4 --- 5</screen>
\r
1940 <simpara>to</simpara>
\r
1941 <screen>1 --- 2 --- 4 --- 3 --- 5</screen>
\r
1942 <simpara>by moving the "pick 4" line will result in the following history:</simpara>
\r
1945 1 --- 2 --- 4 --- 5</screen>
\r
1947 <simplesect id="git-rebase(1)__git">
\r
1948 <title>GIT</title>
\r
1949 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
\r