OSDN Git Service

Add Git official document to help
[tortoisegit/TortoiseGitJp.git] / doc / source / en / TortoiseGit / git_doc / git-rebase.html.xml
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-rebase(1)">\r
5 <articleinfo>\r
6     <title>git-rebase(1)</title>\r
7         <indexterm>\r
8                 <primary>git-rebase(1)</primary>\r
9         </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-rebase - Forward-port local commits to the updated upstream head</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <blockquote>\r
18 <literallayout><emphasis>git rebase</emphasis> [-i | --interactive] [options] [--onto &lt;newbase&gt;]\r
19         &lt;upstream&gt; [&lt;branch&gt;]\r
20 <emphasis>git rebase</emphasis> [-i | --interactive] [options] --onto &lt;newbase&gt;\r
21         --root [&lt;branch&gt;]</literallayout>\r
22 </blockquote>\r
23 <simpara><emphasis>git rebase</emphasis> --continue | --skip | --abort</simpara>\r
24 </simplesect>\r
25 <simplesect id="_description">\r
26 <title>DESCRIPTION</title>\r
27 <simpara>If &lt;branch&gt; is specified, <emphasis>git-rebase</emphasis> will perform an automatic\r
28 <literal>git checkout &lt;branch&gt;</literal> before doing anything else.  Otherwise\r
29 it remains on the current branch.</simpara>\r
30 <simpara>All changes made by commits in the current branch but that are not\r
31 in &lt;upstream&gt; are saved to a temporary area.  This is the same set\r
32 of commits that would be shown by <literal>git log &lt;upstream&gt;..HEAD</literal> (or\r
33 <literal>git log HEAD</literal>, if --root is specified).</simpara>\r
34 <simpara>The current branch is reset to &lt;upstream&gt;, or &lt;newbase&gt; if the\r
35 --onto option was supplied.  This has the exact same effect as\r
36 <literal>git reset --hard &lt;upstream&gt;</literal> (or &lt;newbase&gt;).  ORIG_HEAD is set\r
37 to point at the tip of the branch before the reset.</simpara>\r
38 <simpara>The commits that were previously saved into the temporary area are\r
39 then reapplied to the current branch, one by one, in order. Note that\r
40 any commits in HEAD which introduce the same textual changes as a commit\r
41 in HEAD..&lt;upstream&gt; are omitted (i.e., a patch already accepted upstream\r
42 with a different commit message or timestamp will be skipped).</simpara>\r
43 <simpara>It is possible that a merge failure will prevent this process from being\r
44 completely automatic.  You will have to resolve any such merge failure\r
45 and run <literal>git rebase --continue</literal>.  Another option is to bypass the commit\r
46 that caused the merge failure with <literal>git rebase --skip</literal>.  To restore the\r
47 original &lt;branch&gt; and remove the .git/rebase-apply working files, use the\r
48 command <literal>git rebase --abort</literal> instead.</simpara>\r
49 <simpara>Assume the following history exists and the current branch is "topic":</simpara>\r
50 <literallayout>          A---B---C topic\r
51          /\r
52     D---E---F---G master</literallayout>\r
53 <simpara>From this point, the result of either of the following commands:</simpara>\r
54 <literallayout class="monospaced">git rebase master\r
55 git rebase master topic</literallayout>\r
56 <simpara>would be:</simpara>\r
57 <literallayout>                  A'--B'--C' topic\r
58                  /\r
59     D---E---F---G master</literallayout>\r
60 <simpara>The latter form is just a short-hand of <literal>git checkout topic</literal>\r
61 followed by <literal>git rebase master</literal>.</simpara>\r
62 <simpara>If the upstream branch already contains a change you have made (e.g.,\r
63 because you mailed a patch which was applied upstream), then that commit\r
64 will be skipped. For example, running &#8216;git rebase master` on the\r
65 following history (in which A&#8217; and A introduce the same set of changes,\r
66 but have different committer information):</simpara>\r
67 <literallayout>          A---B---C topic\r
68          /\r
69     D---E---A'---F master</literallayout>\r
70 <simpara>will result in:</simpara>\r
71 <literallayout>                   B'---C' topic\r
72                   /\r
73     D---E---A'---F master</literallayout>\r
74 <simpara>Here is how you would transplant a topic branch based on one\r
75 branch to another, to pretend that you forked the topic branch\r
76 from the latter branch, using <literal>rebase --onto</literal>.</simpara>\r
77 <simpara>First let&#8217;s assume your <emphasis>topic</emphasis> is based on branch <emphasis>next</emphasis>.\r
78 For example, a feature developed in <emphasis>topic</emphasis> depends on some\r
79 functionality which is found in <emphasis>next</emphasis>.</simpara>\r
80 <literallayout>    o---o---o---o---o  master\r
81          \\r
82           o---o---o---o---o  next\r
83                            \\r
84                             o---o---o  topic</literallayout>\r
85 <simpara>We want to make <emphasis>topic</emphasis> forked from branch <emphasis>master</emphasis>; for example,\r
86 because the functionality on which <emphasis>topic</emphasis> depends was merged into the\r
87 more stable <emphasis>master</emphasis> branch. We want our tree to look like this:</simpara>\r
88 <literallayout>    o---o---o---o---o  master\r
89         |            \\r
90         |             o'--o'--o'  topic\r
91          \\r
92           o---o---o---o---o  next</literallayout>\r
93 <simpara>We can get this using the following command:</simpara>\r
94 <literallayout class="monospaced">git rebase --onto master next topic</literallayout>\r
95 <simpara>Another example of --onto option is to rebase part of a\r
96 branch.  If we have the following situation:</simpara>\r
97 <literallayout>                            H---I---J topicB\r
98                            /\r
99                   E---F---G  topicA\r
100                  /\r
101     A---B---C---D  master</literallayout>\r
102 <simpara>then the command</simpara>\r
103 <literallayout class="monospaced">git rebase --onto master topicA topicB</literallayout>\r
104 <simpara>would result in:</simpara>\r
105 <literallayout>                 H'--I'--J'  topicB\r
106                 /\r
107                 | E---F---G  topicA\r
108                 |/\r
109     A---B---C---D  master</literallayout>\r
110 <simpara>This is useful when topicB does not depend on topicA.</simpara>\r
111 <simpara>A range of commits could also be removed with rebase.  If we have\r
112 the following situation:</simpara>\r
113 <literallayout>    E---F---G---H---I---J  topicA</literallayout>\r
114 <simpara>then the command</simpara>\r
115 <literallayout class="monospaced">git rebase --onto topicA~5 topicA~3 topicA</literallayout>\r
116 <simpara>would result in the removal of commits F and G:</simpara>\r
117 <literallayout>    E---H'---I'---J'  topicA</literallayout>\r
118 <simpara>This is useful if F and G were flawed in some way, or should not be\r
119 part of topicA.  Note that the argument to --onto and the &lt;upstream&gt;\r
120 parameter can be any valid commit-ish.</simpara>\r
121 <simpara>In case of conflict, <emphasis>git-rebase</emphasis> will stop at the first problematic commit\r
122 and leave conflict markers in the tree.  You can use <emphasis>git-diff</emphasis> to locate\r
123 the markers (&lt;&lt;&lt;&lt;&lt;&lt;) and make edits to resolve the conflict.  For each\r
124 file you edit, you need to tell git that the conflict has been resolved,\r
125 typically this would be done with</simpara>\r
126 <literallayout class="monospaced">git add &lt;filename&gt;</literallayout>\r
127 <simpara>After resolving the conflict manually and updating the index with the\r
128 desired resolution, you can continue the rebasing process with</simpara>\r
129 <literallayout class="monospaced">git rebase --continue</literallayout>\r
130 <simpara>Alternatively, you can undo the <emphasis>git-rebase</emphasis> with</simpara>\r
131 <literallayout class="monospaced">git rebase --abort</literallayout>\r
132 </simplesect>\r
133 <simplesect id="_options">\r
134 <title>OPTIONS</title>\r
135 <variablelist>\r
136 <varlistentry>\r
137 <term>\r
138 &lt;newbase&gt;\r
139 </term>\r
140 <listitem>\r
141 <simpara>\r
142         Starting point at which to create the new commits. If the\r
143         --onto option is not specified, the starting point is\r
144         &lt;upstream&gt;.  May be any valid commit, and not just an\r
145         existing branch name.\r
146 </simpara>\r
147 </listitem>\r
148 </varlistentry>\r
149 <varlistentry>\r
150 <term>\r
151 &lt;upstream&gt;\r
152 </term>\r
153 <listitem>\r
154 <simpara>\r
155         Upstream branch to compare against.  May be any valid commit,\r
156         not just an existing branch name.\r
157 </simpara>\r
158 </listitem>\r
159 </varlistentry>\r
160 <varlistentry>\r
161 <term>\r
162 &lt;branch&gt;\r
163 </term>\r
164 <listitem>\r
165 <simpara>\r
166         Working branch; defaults to HEAD.\r
167 </simpara>\r
168 </listitem>\r
169 </varlistentry>\r
170 <varlistentry>\r
171 <term>\r
172 --continue\r
173 </term>\r
174 <listitem>\r
175 <simpara>\r
176         Restart the rebasing process after having resolved a merge conflict.\r
177 </simpara>\r
178 </listitem>\r
179 </varlistentry>\r
180 <varlistentry>\r
181 <term>\r
182 --abort\r
183 </term>\r
184 <listitem>\r
185 <simpara>\r
186         Restore the original branch and abort the rebase operation.\r
187 </simpara>\r
188 </listitem>\r
189 </varlistentry>\r
190 <varlistentry>\r
191 <term>\r
192 --skip\r
193 </term>\r
194 <listitem>\r
195 <simpara>\r
196         Restart the rebasing process by skipping the current patch.\r
197 </simpara>\r
198 </listitem>\r
199 </varlistentry>\r
200 <varlistentry>\r
201 <term>\r
202 -m\r
203 </term>\r
204 <term>\r
205 --merge\r
206 </term>\r
207 <listitem>\r
208 <simpara>\r
209         Use merging strategies to rebase.  When the recursive (default) merge\r
210         strategy is used, this allows rebase to be aware of renames on the\r
211         upstream side.\r
212 </simpara>\r
213 </listitem>\r
214 </varlistentry>\r
215 <varlistentry>\r
216 <term>\r
217 -s &lt;strategy&gt;\r
218 </term>\r
219 <term>\r
220 --strategy=&lt;strategy&gt;\r
221 </term>\r
222 <listitem>\r
223 <simpara>\r
224         Use the given merge strategy; can be supplied more than\r
225         once to specify them in the order they should be tried.\r
226         If there is no <literal>-s</literal> option, a built-in list of strategies\r
227         is used instead (<emphasis>git-merge-recursive</emphasis> when merging a single\r
228         head, <emphasis>git-merge-octopus</emphasis> otherwise).  This implies --merge.\r
229 </simpara>\r
230 </listitem>\r
231 </varlistentry>\r
232 <varlistentry>\r
233 <term>\r
234 -v\r
235 </term>\r
236 <term>\r
237 --verbose\r
238 </term>\r
239 <listitem>\r
240 <simpara>\r
241         Display a diffstat of what changed upstream since the last rebase.\r
242 </simpara>\r
243 </listitem>\r
244 </varlistentry>\r
245 <varlistentry>\r
246 <term>\r
247 --no-verify\r
248 </term>\r
249 <listitem>\r
250 <simpara>\r
251         This option bypasses the pre-rebase hook.  See also <xref linkend="githooks(5)"/>.\r
252 </simpara>\r
253 </listitem>\r
254 </varlistentry>\r
255 <varlistentry>\r
256 <term>\r
257 -C&lt;n&gt;\r
258 </term>\r
259 <listitem>\r
260 <simpara>\r
261         Ensure at least &lt;n&gt; lines of surrounding context match before\r
262         and after each change.  When fewer lines of surrounding\r
263         context exist they all must match.  By default no context is\r
264         ever ignored.\r
265 </simpara>\r
266 </listitem>\r
267 </varlistentry>\r
268 <varlistentry>\r
269 <term>\r
270 --whitespace=&lt;nowarn|warn|error|error-all|strip&gt;\r
271 </term>\r
272 <listitem>\r
273 <simpara>\r
274         This flag is passed to the <emphasis>git-apply</emphasis> program\r
275         (see <xref linkend="git-apply(1)"/>) that applies the patch.\r
276 </simpara>\r
277 </listitem>\r
278 </varlistentry>\r
279 <varlistentry>\r
280 <term>\r
281 -i\r
282 </term>\r
283 <term>\r
284 --interactive\r
285 </term>\r
286 <listitem>\r
287 <simpara>\r
288         Make a list of the commits which are about to be rebased.  Let the\r
289         user edit that list before rebasing.  This mode can also be used to\r
290         split commits (see SPLITTING COMMITS below).\r
291 </simpara>\r
292 </listitem>\r
293 </varlistentry>\r
294 <varlistentry>\r
295 <term>\r
296 -p\r
297 </term>\r
298 <term>\r
299 --preserve-merges\r
300 </term>\r
301 <listitem>\r
302 <simpara>\r
303         Instead of ignoring merges, try to recreate them.\r
304 </simpara>\r
305 </listitem>\r
306 </varlistentry>\r
307 <varlistentry>\r
308 <term>\r
309 --root\r
310 </term>\r
311 <listitem>\r
312 <simpara>\r
313         Rebase all commits reachable from &lt;branch&gt;, instead of\r
314         limiting them with an &lt;upstream&gt;.  This allows you to rebase\r
315         the root commit(s) on a branch.  Must be used with --onto, and\r
316         will skip changes already contained in &lt;newbase&gt; (instead of\r
317         &lt;upstream&gt;).  When used together with --preserve-merges, <emphasis>all</emphasis>\r
318         root commits will be rewritten to have &lt;newbase&gt; as parent\r
319         instead.\r
320 </simpara>\r
321 </listitem>\r
322 </varlistentry>\r
323 </variablelist>\r
324 </simplesect>\r
325 <simplesect id="_merge_strategies">\r
326 <title>MERGE STRATEGIES</title>\r
327 <variablelist>\r
328 <varlistentry>\r
329 <term>\r
330 resolve\r
331 </term>\r
332 <listitem>\r
333 <simpara>\r
334         This can only resolve two heads (i.e. the current branch\r
335         and another branch you pulled from) using 3-way merge\r
336         algorithm.  It tries to carefully detect criss-cross\r
337         merge ambiguities and is considered generally safe and\r
338         fast.\r
339 </simpara>\r
340 </listitem>\r
341 </varlistentry>\r
342 <varlistentry>\r
343 <term>\r
344 recursive\r
345 </term>\r
346 <listitem>\r
347 <simpara>\r
348         This can only resolve two heads using 3-way merge\r
349         algorithm.  When there are more than one common\r
350         ancestors that can be used for 3-way merge, it creates a\r
351         merged tree of the common ancestors and uses that as\r
352         the reference tree for the 3-way merge.  This has been\r
353         reported to result in fewer merge conflicts without\r
354         causing mis-merges by tests done on actual merge commits\r
355         taken from Linux 2.6 kernel development history.\r
356         Additionally this can detect and handle merges involving\r
357         renames.  This is the default merge strategy when\r
358         pulling or merging one branch.\r
359 </simpara>\r
360 </listitem>\r
361 </varlistentry>\r
362 <varlistentry>\r
363 <term>\r
364 octopus\r
365 </term>\r
366 <listitem>\r
367 <simpara>\r
368         This resolves more than two-head case, but refuses to do\r
369         complex merge that needs manual resolution.  It is\r
370         primarily meant to be used for bundling topic branch\r
371         heads together.  This is the default merge strategy when\r
372         pulling or merging more than one branches.\r
373 </simpara>\r
374 </listitem>\r
375 </varlistentry>\r
376 <varlistentry>\r
377 <term>\r
378 ours\r
379 </term>\r
380 <listitem>\r
381 <simpara>\r
382         This resolves any number of heads, but the result of the\r
383         merge is always the current branch head.  It is meant to\r
384         be used to supersede old development history of side\r
385         branches.\r
386 </simpara>\r
387 </listitem>\r
388 </varlistentry>\r
389 <varlistentry>\r
390 <term>\r
391 subtree\r
392 </term>\r
393 <listitem>\r
394 <simpara>\r
395         This is a modified recursive strategy. When merging trees A and\r
396         B, if B corresponds to a subtree of A, B is first adjusted to\r
397         match the tree structure of A, instead of reading the trees at\r
398         the same level. This adjustment is also done to the common\r
399         ancestor tree.\r
400 </simpara>\r
401 </listitem>\r
402 </varlistentry>\r
403 </variablelist>\r
404 </simplesect>\r
405 <simplesect id="_notes">\r
406 <title>NOTES</title>\r
407 <simpara>You should understand the implications of using <emphasis>git-rebase</emphasis> on a\r
408 repository that you share.  See also RECOVERING FROM UPSTREAM REBASE\r
409 below.</simpara>\r
410 <simpara>When the git-rebase command is run, it will first execute a "pre-rebase"\r
411 hook if one exists.  You can use this hook to do sanity checks and\r
412 reject the rebase if it isn&#8217;t appropriate.  Please see the template\r
413 pre-rebase hook script for an example.</simpara>\r
414 <simpara>Upon completion, &lt;branch&gt; will be the current branch.</simpara>\r
415 </simplesect>\r
416 <simplesect id="_interactive_mode">\r
417 <title>INTERACTIVE MODE</title>\r
418 <simpara>Rebasing interactively means that you have a chance to edit the commits\r
419 which are rebased.  You can reorder the commits, and you can\r
420 remove them (weeding out bad or otherwise unwanted patches).</simpara>\r
421 <simpara>The interactive mode is meant for this type of workflow:</simpara>\r
422 <orderedlist numeration="arabic">\r
423 <listitem>\r
424 <simpara>\r
425 have a wonderful idea\r
426 </simpara>\r
427 </listitem>\r
428 <listitem>\r
429 <simpara>\r
430 hack on the code\r
431 </simpara>\r
432 </listitem>\r
433 <listitem>\r
434 <simpara>\r
435 prepare a series for submission\r
436 </simpara>\r
437 </listitem>\r
438 <listitem>\r
439 <simpara>\r
440 submit\r
441 </simpara>\r
442 </listitem>\r
443 </orderedlist>\r
444 <simpara>where point 2. consists of several instances of</simpara>\r
445 <orderedlist numeration="loweralpha">\r
446 <listitem>\r
447 <simpara>\r
448 regular use\r
449 </simpara>\r
450 <orderedlist numeration="arabic">\r
451 <listitem>\r
452 <simpara>\r
453 finish something worthy of a commit\r
454 </simpara>\r
455 </listitem>\r
456 <listitem>\r
457 <simpara>\r
458 commit\r
459 </simpara>\r
460 </listitem>\r
461 </orderedlist>\r
462 </listitem>\r
463 <listitem>\r
464 <simpara>\r
465 independent fixup\r
466 </simpara>\r
467 <orderedlist numeration="arabic">\r
468 <listitem>\r
469 <simpara>\r
470 realize that something does not work\r
471 </simpara>\r
472 </listitem>\r
473 <listitem>\r
474 <simpara>\r
475 fix that\r
476 </simpara>\r
477 </listitem>\r
478 <listitem>\r
479 <simpara>\r
480 commit it\r
481 </simpara>\r
482 </listitem>\r
483 </orderedlist>\r
484 </listitem>\r
485 </orderedlist>\r
486 <simpara>Sometimes the thing fixed in b.2. cannot be amended to the not-quite\r
487 perfect commit it fixes, because that commit is buried deeply in a\r
488 patch series.  That is exactly what interactive rebase is for: use it\r
489 after plenty of "a"s and "b"s, by rearranging and editing\r
490 commits, and squashing multiple commits into one.</simpara>\r
491 <simpara>Start it with the last commit you want to retain as-is:</simpara>\r
492 <literallayout class="monospaced">git rebase -i &lt;after-this-commit&gt;</literallayout>\r
493 <simpara>An editor will be fired up with all the commits in your current branch\r
494 (ignoring merge commits), which come after the given commit.  You can\r
495 reorder the commits in this list to your heart&#8217;s content, and you can\r
496 remove them.  The list looks more or less like this:</simpara>\r
497 <literallayout>pick deadbee The oneline of this commit\r
498 pick fa1afe1 The oneline of the next commit\r
499 ...</literallayout>\r
500 <simpara>The oneline descriptions are purely for your pleasure; <emphasis>git-rebase</emphasis> will\r
501 not look at them but at the commit names ("deadbee" and "fa1afe1" in this\r
502 example), so do not delete or edit the names.</simpara>\r
503 <simpara>By replacing the command "pick" with the command "edit", you can tell\r
504 <emphasis>git-rebase</emphasis> to stop after applying that commit, so that you can edit\r
505 the files and/or the commit message, amend the commit, and continue\r
506 rebasing.</simpara>\r
507 <simpara>If you want to fold two or more commits into one, replace the command\r
508 "pick" with "squash" for the second and subsequent commit.  If the\r
509 commits had different authors, it will attribute the squashed commit to\r
510 the author of the first commit.</simpara>\r
511 <simpara>In both cases, or when a "pick" does not succeed (because of merge\r
512 errors), the loop will stop to let you fix things, and you can continue\r
513 the loop with <literal>git rebase --continue</literal>.</simpara>\r
514 <simpara>For example, if you want to reorder the last 5 commits, such that what\r
515 was HEAD~4 becomes the new HEAD. To achieve that, you would call\r
516 <emphasis>git-rebase</emphasis> like this:</simpara>\r
517 <literallayout>$ git rebase -i HEAD~5</literallayout>\r
518 <simpara>And move the first patch to the end of the list.</simpara>\r
519 <simpara>You might want to preserve merges, if you have a history like this:</simpara>\r
520 <literallayout>           X\r
521             \\r
522          A---M---B\r
523         /\r
524 ---o---O---P---Q</literallayout>\r
525 <simpara>Suppose you want to rebase the side branch starting at "A" to "Q". Make\r
526 sure that the current HEAD is "B", and call</simpara>\r
527 <literallayout>$ git rebase -i -p --onto Q O</literallayout>\r
528 </simplesect>\r
529 <simplesect id="_splitting_commits">\r
530 <title>SPLITTING COMMITS</title>\r
531 <simpara>In interactive mode, you can mark commits with the action "edit".  However,\r
532 this does not necessarily mean that <emphasis>git-rebase</emphasis> expects the result of this\r
533 edit to be exactly one commit.  Indeed, you can undo the commit, or you can\r
534 add other commits.  This can be used to split a commit into two:</simpara>\r
535 <itemizedlist>\r
536 <listitem>\r
537 <simpara>\r
538 Start an interactive rebase with <literal>git rebase -i &lt;commit&gt;^</literal>, where\r
539   &lt;commit&gt; is the commit you want to split.  In fact, any commit range\r
540   will do, as long as it contains that commit.\r
541 </simpara>\r
542 </listitem>\r
543 <listitem>\r
544 <simpara>\r
545 Mark the commit you want to split with the action "edit".\r
546 </simpara>\r
547 </listitem>\r
548 <listitem>\r
549 <simpara>\r
550 When it comes to editing that commit, execute <literal>git reset HEAD^</literal>.  The\r
551   effect is that the HEAD is rewound by one, and the index follows suit.\r
552   However, the working tree stays the same.\r
553 </simpara>\r
554 </listitem>\r
555 <listitem>\r
556 <simpara>\r
557 Now add the changes to the index that you want to have in the first\r
558   commit.  You can use <literal>git add</literal> (possibly interactively) or\r
559   <emphasis>git-gui</emphasis> (or both) to do that.\r
560 </simpara>\r
561 </listitem>\r
562 <listitem>\r
563 <simpara>\r
564 Commit the now-current index with whatever commit message is appropriate\r
565   now.\r
566 </simpara>\r
567 </listitem>\r
568 <listitem>\r
569 <simpara>\r
570 Repeat the last two steps until your working tree is clean.\r
571 </simpara>\r
572 </listitem>\r
573 <listitem>\r
574 <simpara>\r
575 Continue the rebase with <literal>git rebase --continue</literal>.\r
576 </simpara>\r
577 </listitem>\r
578 </itemizedlist>\r
579 <simpara>If you are not absolutely sure that the intermediate revisions are\r
580 consistent (they compile, pass the testsuite, etc.) you should use\r
581 <emphasis>git-stash</emphasis> to stash away the not-yet-committed changes\r
582 after each commit, test, and amend the commit if fixes are necessary.</simpara>\r
583 </simplesect>\r
584 <simplesect id="_recovering_from_upstream_rebase">\r
585 <title>RECOVERING FROM UPSTREAM REBASE</title>\r
586 <simpara>Rebasing (or any other form of rewriting) a branch that others have\r
587 based work on is a bad idea: anyone downstream of it is forced to\r
588 manually fix their history.  This section explains how to do the fix\r
589 from the downstream&#8217;s point of view.  The real fix, however, would be\r
590 to avoid rebasing the upstream in the first place.</simpara>\r
591 <simpara>To illustrate, suppose you are in a situation where someone develops a\r
592 <emphasis>subsystem</emphasis> branch, and you are working on a <emphasis>topic</emphasis> that is dependent\r
593 on this <emphasis>subsystem</emphasis>.  You might end up with a history like the\r
594 following:</simpara>\r
595 <literallayout>    o---o---o---o---o---o---o---o---o  master\r
596          \\r
597           o---o---o---o---o  subsystem\r
598                            \\r
599                             *---*---*  topic</literallayout>\r
600 <simpara>If <emphasis>subsystem</emphasis> is rebased against <emphasis>master</emphasis>, the following happens:</simpara>\r
601 <literallayout>    o---o---o---o---o---o---o---o  master\r
602          \                       \\r
603           o---o---o---o---o       o'--o'--o'--o'--o'  subsystem\r
604                            \\r
605                             *---*---*  topic</literallayout>\r
606 <simpara>If you now continue development as usual, and eventually merge <emphasis>topic</emphasis>\r
607 to <emphasis>subsystem</emphasis>, the commits from <emphasis>subsystem</emphasis> will remain duplicated forever:</simpara>\r
608 <literallayout>    o---o---o---o---o---o---o---o  master\r
609          \                       \\r
610           o---o---o---o---o       o'--o'--o'--o'--o'--M  subsystem\r
611                            \                         /\r
612                             *---*---*-..........-*--*  topic</literallayout>\r
613 <simpara>Such duplicates are generally frowned upon because they clutter up\r
614 history, making it harder to follow.  To clean things up, you need to\r
615 transplant the commits on <emphasis>topic</emphasis> to the new <emphasis>subsystem</emphasis> tip, i.e.,\r
616 rebase <emphasis>topic</emphasis>.  This becomes a ripple effect: anyone downstream from\r
617 <emphasis>topic</emphasis> is forced to rebase too, and so on!</simpara>\r
618 <simpara>There are two kinds of fixes, discussed in the following subsections:</simpara>\r
619 <variablelist>\r
620 <varlistentry>\r
621 <term>\r
622 Easy case: The changes are literally the same.\r
623 </term>\r
624 <listitem>\r
625 <simpara>\r
626         This happens if the <emphasis>subsystem</emphasis> rebase was a simple rebase and\r
627         had no conflicts.\r
628 </simpara>\r
629 </listitem>\r
630 </varlistentry>\r
631 <varlistentry>\r
632 <term>\r
633 Hard case: The changes are not the same.\r
634 </term>\r
635 <listitem>\r
636 <simpara>\r
637         This happens if the <emphasis>subsystem</emphasis> rebase had conflicts, or used\r
638         <literal>--interactive</literal> to omit, edit, or squash commits; or if the\r
639         upstream used one of <literal>commit --amend</literal>, <literal>reset</literal>, or\r
640         <literal>filter-branch</literal>.\r
641 </simpara>\r
642 </listitem>\r
643 </varlistentry>\r
644 </variablelist>\r
645 <simplesect id="_the_easy_case">\r
646 <title>The easy case</title>\r
647 <simpara>Only works if the changes (patch IDs based on the diff contents) on\r
648 <emphasis>subsystem</emphasis> are literally the same before and after the rebase\r
649 <emphasis>subsystem</emphasis> did.</simpara>\r
650 <simpara>In that case, the fix is easy because <emphasis>git-rebase</emphasis> knows to skip\r
651 changes that are already present in the new upstream.  So if you say\r
652 (assuming you&#8217;re on <emphasis>topic</emphasis>)</simpara>\r
653 <literallayout>    $ git rebase subsystem</literallayout>\r
654 <simpara>you will end up with the fixed history</simpara>\r
655 <literallayout>    o---o---o---o---o---o---o---o  master\r
656                                  \\r
657                                   o'--o'--o'--o'--o'  subsystem\r
658                                                    \\r
659                                                     *---*---*  topic</literallayout>\r
660 </simplesect>\r
661 <simplesect id="_the_hard_case">\r
662 <title>The hard case</title>\r
663 <simpara>Things get more complicated if the <emphasis>subsystem</emphasis> changes do not exactly\r
664 correspond to the ones before the rebase.</simpara>\r
665 <note><simpara>While an "easy case recovery" sometimes appears to be successful\r
666       even in the hard case, it may have unintended consequences.  For\r
667       example, a commit that was removed via <literal>git rebase\r
668       --interactive</literal> will be <emphasis role="strong">resurrected</emphasis>!</simpara></note>\r
669 <simpara>The idea is to manually tell <emphasis>git-rebase</emphasis> "where the old <emphasis>subsystem</emphasis>\r
670 ended and your <emphasis>topic</emphasis> began", that is, what the old merge-base\r
671 between them was.  You will have to find a way to name the last commit\r
672 of the old <emphasis>subsystem</emphasis>, for example:</simpara>\r
673 <itemizedlist>\r
674 <listitem>\r
675 <simpara>\r
676 With the <emphasis>subsystem</emphasis> reflog: after <emphasis>git-fetch</emphasis>, the old tip of\r
677   <emphasis>subsystem</emphasis> is at <literal>subsystem@{1}</literal>.  Subsequent fetches will\r
678   increase the number.  (See <xref linkend="git-reflog(1)"/>.)\r
679 </simpara>\r
680 </listitem>\r
681 <listitem>\r
682 <simpara>\r
683 Relative to the tip of <emphasis>topic</emphasis>: knowing that your <emphasis>topic</emphasis> has three\r
684   commits, the old tip of <emphasis>subsystem</emphasis> must be <literal>topic~3</literal>.\r
685 </simpara>\r
686 </listitem>\r
687 </itemizedlist>\r
688 <simpara>You can then transplant the old <literal>subsystem..topic</literal> to the new tip by\r
689 saying (for the reflog case, and assuming you are on <emphasis>topic</emphasis> already):</simpara>\r
690 <literallayout>    $ git rebase --onto subsystem subsystem@{1}</literallayout>\r
691 <simpara>The ripple effect of a "hard case" recovery is especially bad:\r
692 <emphasis>everyone</emphasis> downstream from <emphasis>topic</emphasis> will now have to perform a "hard\r
693 case" recovery too!</simpara>\r
694 </simplesect>\r
695 </simplesect>\r
696 <simplesect id="_authors">\r
697 <title>Authors</title>\r
698 <simpara>Written by Junio C Hamano &lt;<ulink url="mailto:gitster@pobox.com">gitster@pobox.com</ulink>&gt; and\r
699 Johannes E. Schindelin &lt;<ulink url="mailto:johannes.schindelin@gmx.de">johannes.schindelin@gmx.de</ulink>&gt;</simpara>\r
700 </simplesect>\r
701 <simplesect id="_documentation">\r
702 <title>Documentation</title>\r
703 <simpara>Documentation by Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>\r
704 </simplesect>\r
705 <simplesect id="_git">\r
706 <title>GIT</title>\r
707 <simpara>Part of the <xref linkend="git(1)"/> suite</simpara>\r
708 </simplesect>\r
709 </article>\r