Code

Documentation: new upstream rebase recovery section in git-rebase
[git.git] / Documentation / git-rebase.txt
1 git-rebase(1)
2 =============
4 NAME
5 ----
6 git-rebase - Forward-port local commits to the updated upstream head
8 SYNOPSIS
9 --------
10 [verse]
11 'git rebase' [-i | --interactive] [-v | --verbose] [-m | --merge]
12         [-s <strategy> | --strategy=<strategy>]
13         [-C<n>] [ --whitespace=<option>] [-p | --preserve-merges]
14         [--onto <newbase>] <upstream> [<branch>]
15 'git rebase' --continue | --skip | --abort
17 DESCRIPTION
18 -----------
19 If <branch> is specified, 'git-rebase' will perform an automatic
20 `git checkout <branch>` before doing anything else.  Otherwise
21 it remains on the current branch.
23 All changes made by commits in the current branch but that are not
24 in <upstream> are saved to a temporary area.  This is the same set
25 of commits that would be shown by `git log <upstream>..HEAD`.
27 The current branch is reset to <upstream>, or <newbase> if the
28 --onto option was supplied.  This has the exact same effect as
29 `git reset --hard <upstream>` (or <newbase>).  ORIG_HEAD is set
30 to point at the tip of the branch before the reset.
32 The commits that were previously saved into the temporary area are
33 then reapplied to the current branch, one by one, in order. Note that
34 any commits in HEAD which introduce the same textual changes as a commit
35 in HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
36 with a different commit message or timestamp will be skipped).
38 It is possible that a merge failure will prevent this process from being
39 completely automatic.  You will have to resolve any such merge failure
40 and run `git rebase --continue`.  Another option is to bypass the commit
41 that caused the merge failure with `git rebase --skip`.  To restore the
42 original <branch> and remove the .git/rebase-apply working files, use the
43 command `git rebase --abort` instead.
45 Assume the following history exists and the current branch is "topic":
47 ------------
48           A---B---C topic
49          /
50     D---E---F---G master
51 ------------
53 From this point, the result of either of the following commands:
56     git rebase master
57     git rebase master topic
59 would be:
61 ------------
62                   A'--B'--C' topic
63                  /
64     D---E---F---G master
65 ------------
67 The latter form is just a short-hand of `git checkout topic`
68 followed by `git rebase master`.
70 If the upstream branch already contains a change you have made (e.g.,
71 because you mailed a patch which was applied upstream), then that commit
72 will be skipped. For example, running `git rebase master` on the
73 following history (in which A' and A introduce the same set of changes,
74 but have different committer information):
76 ------------
77           A---B---C topic
78          /
79     D---E---A'---F master
80 ------------
82 will result in:
84 ------------
85                    B'---C' topic
86                   /
87     D---E---A'---F master
88 ------------
90 Here is how you would transplant a topic branch based on one
91 branch to another, to pretend that you forked the topic branch
92 from the latter branch, using `rebase --onto`.
94 First let's assume your 'topic' is based on branch 'next'.
95 For example feature developed in 'topic' depends on some
96 functionality which is found in 'next'.
98 ------------
99     o---o---o---o---o  master
100          \
101           o---o---o---o---o  next
102                            \
103                             o---o---o  topic
104 ------------
106 We would want to make 'topic' forked from branch 'master',
107 for example because the functionality 'topic' branch depend on
108 got merged into more stable 'master' branch, like this:
110 ------------
111     o---o---o---o---o  master
112         |            \
113         |             o'--o'--o'  topic
114          \
115           o---o---o---o---o  next
116 ------------
118 We can get this using the following command:
120     git rebase --onto master next topic
123 Another example of --onto option is to rebase part of a
124 branch.  If we have the following situation:
126 ------------
127                             H---I---J topicB
128                            /
129                   E---F---G  topicA
130                  /
131     A---B---C---D  master
132 ------------
134 then the command
136     git rebase --onto master topicA topicB
138 would result in:
140 ------------
141                  H'--I'--J'  topicB
142                 /
143                 | E---F---G  topicA
144                 |/
145     A---B---C---D  master
146 ------------
148 This is useful when topicB does not depend on topicA.
150 A range of commits could also be removed with rebase.  If we have
151 the following situation:
153 ------------
154     E---F---G---H---I---J  topicA
155 ------------
157 then the command
159     git rebase --onto topicA~5 topicA~3 topicA
161 would result in the removal of commits F and G:
163 ------------
164     E---H'---I'---J'  topicA
165 ------------
167 This is useful if F and G were flawed in some way, or should not be
168 part of topicA.  Note that the argument to --onto and the <upstream>
169 parameter can be any valid commit-ish.
171 In case of conflict, 'git-rebase' will stop at the first problematic commit
172 and leave conflict markers in the tree.  You can use 'git-diff' to locate
173 the markers (<<<<<<) and make edits to resolve the conflict.  For each
174 file you edit, you need to tell git that the conflict has been resolved,
175 typically this would be done with
178     git add <filename>
181 After resolving the conflict manually and updating the index with the
182 desired resolution, you can continue the rebasing process with
185     git rebase --continue
188 Alternatively, you can undo the 'git-rebase' with
191     git rebase --abort
193 OPTIONS
194 -------
195 <newbase>::
196         Starting point at which to create the new commits. If the
197         --onto option is not specified, the starting point is
198         <upstream>.  May be any valid commit, and not just an
199         existing branch name.
201 <upstream>::
202         Upstream branch to compare against.  May be any valid commit,
203         not just an existing branch name.
205 <branch>::
206         Working branch; defaults to HEAD.
208 --continue::
209         Restart the rebasing process after having resolved a merge conflict.
211 --abort::
212         Restore the original branch and abort the rebase operation.
214 --skip::
215         Restart the rebasing process by skipping the current patch.
217 -m::
218 --merge::
219         Use merging strategies to rebase.  When the recursive (default) merge
220         strategy is used, this allows rebase to be aware of renames on the
221         upstream side.
223 -s <strategy>::
224 --strategy=<strategy>::
225         Use the given merge strategy; can be supplied more than
226         once to specify them in the order they should be tried.
227         If there is no `-s` option, a built-in list of strategies
228         is used instead ('git-merge-recursive' when merging a single
229         head, 'git-merge-octopus' otherwise).  This implies --merge.
231 -v::
232 --verbose::
233         Display a diffstat of what changed upstream since the last rebase.
235 -C<n>::
236         Ensure at least <n> lines of surrounding context match before
237         and after each change.  When fewer lines of surrounding
238         context exist they all must match.  By default no context is
239         ever ignored.
241 --whitespace=<nowarn|warn|error|error-all|strip>::
242         This flag is passed to the 'git-apply' program
243         (see linkgit:git-apply[1]) that applies the patch.
245 -i::
246 --interactive::
247         Make a list of the commits which are about to be rebased.  Let the
248         user edit that list before rebasing.  This mode can also be used to
249         split commits (see SPLITTING COMMITS below).
251 -p::
252 --preserve-merges::
253         Instead of ignoring merges, try to recreate them.  This option
254         only works in interactive mode.
256 include::merge-strategies.txt[]
258 NOTES
259 -----
261 You should understand the implications of using 'git-rebase' on a
262 repository that you share.  See also RECOVERING FROM UPSTREAM REBASE
263 below.
265 When the git-rebase command is run, it will first execute a "pre-rebase"
266 hook if one exists.  You can use this hook to do sanity checks and
267 reject the rebase if it isn't appropriate.  Please see the template
268 pre-rebase hook script for an example.
270 Upon completion, <branch> will be the current branch.
272 INTERACTIVE MODE
273 ----------------
275 Rebasing interactively means that you have a chance to edit the commits
276 which are rebased.  You can reorder the commits, and you can
277 remove them (weeding out bad or otherwise unwanted patches).
279 The interactive mode is meant for this type of workflow:
281 1. have a wonderful idea
282 2. hack on the code
283 3. prepare a series for submission
284 4. submit
286 where point 2. consists of several instances of
288 a. regular use
289  1. finish something worthy of a commit
290  2. commit
291 b. independent fixup
292  1. realize that something does not work
293  2. fix that
294  3. commit it
296 Sometimes the thing fixed in b.2. cannot be amended to the not-quite
297 perfect commit it fixes, because that commit is buried deeply in a
298 patch series.  That is exactly what interactive rebase is for: use it
299 after plenty of "a"s and "b"s, by rearranging and editing
300 commits, and squashing multiple commits into one.
302 Start it with the last commit you want to retain as-is:
304         git rebase -i <after-this-commit>
306 An editor will be fired up with all the commits in your current branch
307 (ignoring merge commits), which come after the given commit.  You can
308 reorder the commits in this list to your heart's content, and you can
309 remove them.  The list looks more or less like this:
311 -------------------------------------------
312 pick deadbee The oneline of this commit
313 pick fa1afe1 The oneline of the next commit
314 ...
315 -------------------------------------------
317 The oneline descriptions are purely for your pleasure; 'git-rebase' will
318 not look at them but at the commit names ("deadbee" and "fa1afe1" in this
319 example), so do not delete or edit the names.
321 By replacing the command "pick" with the command "edit", you can tell
322 'git-rebase' to stop after applying that commit, so that you can edit
323 the files and/or the commit message, amend the commit, and continue
324 rebasing.
326 If you want to fold two or more commits into one, replace the command
327 "pick" with "squash" for the second and subsequent commit.  If the
328 commits had different authors, it will attribute the squashed commit to
329 the author of the first commit.
331 In both cases, or when a "pick" does not succeed (because of merge
332 errors), the loop will stop to let you fix things, and you can continue
333 the loop with `git rebase --continue`.
335 For example, if you want to reorder the last 5 commits, such that what
336 was HEAD~4 becomes the new HEAD. To achieve that, you would call
337 'git-rebase' like this:
339 ----------------------
340 $ git rebase -i HEAD~5
341 ----------------------
343 And move the first patch to the end of the list.
345 You might want to preserve merges, if you have a history like this:
347 ------------------
348            X
349             \
350          A---M---B
351         /
352 ---o---O---P---Q
353 ------------------
355 Suppose you want to rebase the side branch starting at "A" to "Q". Make
356 sure that the current HEAD is "B", and call
358 -----------------------------
359 $ git rebase -i -p --onto Q O
360 -----------------------------
363 SPLITTING COMMITS
364 -----------------
366 In interactive mode, you can mark commits with the action "edit".  However,
367 this does not necessarily mean that 'git-rebase' expects the result of this
368 edit to be exactly one commit.  Indeed, you can undo the commit, or you can
369 add other commits.  This can be used to split a commit into two:
371 - Start an interactive rebase with `git rebase -i <commit>^`, where
372   <commit> is the commit you want to split.  In fact, any commit range
373   will do, as long as it contains that commit.
375 - Mark the commit you want to split with the action "edit".
377 - When it comes to editing that commit, execute `git reset HEAD^`.  The
378   effect is that the HEAD is rewound by one, and the index follows suit.
379   However, the working tree stays the same.
381 - Now add the changes to the index that you want to have in the first
382   commit.  You can use `git add` (possibly interactively) or
383   'git-gui' (or both) to do that.
385 - Commit the now-current index with whatever commit message is appropriate
386   now.
388 - Repeat the last two steps until your working tree is clean.
390 - Continue the rebase with `git rebase --continue`.
392 If you are not absolutely sure that the intermediate revisions are
393 consistent (they compile, pass the testsuite, etc.) you should use
394 'git-stash' to stash away the not-yet-committed changes
395 after each commit, test, and amend the commit if fixes are necessary.
398 RECOVERING FROM UPSTREAM REBASE
399 -------------------------------
401 Rebasing (or any other form of rewriting) a branch that others have
402 based work on is a bad idea: anyone downstream of it is forced to
403 manually fix their history.  This section explains how to do the fix
404 from the downstream's point of view.  The real fix, however, would be
405 to avoid rebasing the upstream in the first place.
407 To illustrate, suppose you are in a situation where someone develops a
408 'subsystem' branch, and you are working on a 'topic' that is dependent
409 on this 'subsystem'.  You might end up with a history like the
410 following:
412 ------------
413     o---o---o---o---o---o---o---o---o  master
414          \
415           o---o---o---o---o  subsystem
416                            \
417                             *---*---*  topic
418 ------------
420 If 'subsystem' is rebased against 'master', the following happens:
422 ------------
423     o---o---o---o---o---o---o---o  master
424          \                       \
425           o---o---o---o---o       o'--o'--o'--o'--o'  subsystem
426                            \
427                             *---*---*  topic
428 ------------
430 If you now continue development as usual, and eventually merge 'topic'
431 to 'subsystem', the commits from 'subsystem' will remain duplicated forever:
433 ------------
434     o---o---o---o---o---o---o---o  master
435          \                       \
436           o---o---o---o---o       o'--o'--o'--o'--o'--M  subsystem
437                            \                         /
438                             *---*---*-..........-*--*  topic
439 ------------
441 Such duplicates are generally frowned upon because they clutter up
442 history, making it harder to follow.  To clean things up, you need to
443 transplant the commits on 'topic' to the new 'subsystem' tip, i.e.,
444 rebase 'topic'.  This becomes a ripple effect: anyone downstream from
445 'topic' is forced to rebase too, and so on!
447 There are two kinds of fixes, discussed in the following subsections:
449 Easy case: The changes are literally the same.::
451         This happens if the 'subsystem' rebase was a simple rebase and
452         had no conflicts.
454 Hard case: The changes are not the same.::
456         This happens if the 'subsystem' rebase had conflicts, or used
457         `\--interactive` to omit, edit, or squash commits; or if the
458         upstream used one of `commit \--amend`, `reset`, or
459         `filter-branch`.
462 The easy case
463 ~~~~~~~~~~~~~
465 Only works if the changes (patch IDs based on the diff contents) on
466 'subsystem' are literally the same before and after the rebase
467 'subsystem' did.
469 In that case, the fix is easy because 'git-rebase' knows to skip
470 changes that are already present in the new upstream.  So if you say
471 (assuming you're on 'topic')
472 ------------
473     $ git rebase subsystem
474 ------------
475 you will end up with the fixed history
476 ------------
477     o---o---o---o---o---o---o---o  master
478                                  \
479                                   o'--o'--o'--o'--o'  subsystem
480                                                    \
481                                                     *---*---*  topic
482 ------------
485 The hard case
486 ~~~~~~~~~~~~~
488 Things get more complicated if the 'subsystem' changes do not exactly
489 correspond to the ones before the rebase.
491 NOTE: While an "easy case recovery" sometimes appears to be successful
492       even in the hard case, it may have unintended consequences.  For
493       example, a commit that was removed via `git rebase
494       \--interactive` will be **resurrected**!
496 The idea is to manually tell 'git-rebase' "where the old 'subsystem'
497 ended and your 'topic' began", that is, what the old merge-base
498 between them was.  You will have to find a way to name the last commit
499 of the old 'subsystem', for example:
501 * With the 'subsystem' reflog: after 'git-fetch', the old tip of
502   'subsystem' is at `subsystem@\{1}`.  Subsequent fetches will
503   increase the number.  (See linkgit:git-reflog[1].)
505 * Relative to the tip of 'topic': knowing that your 'topic' has three
506   commits, the old tip of 'subsystem' must be `topic~3`.
508 You can then transplant the old `subsystem..topic` to the new tip by
509 saying (for the reflog case, and assuming you are on 'topic' already):
510 ------------
511     $ git rebase --onto subsystem subsystem@{1}
512 ------------
514 The ripple effect of a "hard case" recovery is especially bad:
515 'everyone' downstream from 'topic' will now have to perform a "hard
516 case" recovery too!
519 Authors
520 ------
521 Written by Junio C Hamano <gitster@pobox.com> and
522 Johannes E. Schindelin <johannes.schindelin@gmx.de>
524 Documentation
525 --------------
526 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
528 GIT
529 ---
530 Part of the linkgit:git[1] suite