6 git-rebase - Forward-port local commits to the updated upstream head
11 'git rebase' [-i | --interactive] [options] [--onto <newbase>]
12 [<upstream>] [<branch>]
13 'git rebase' [-i | --interactive] [options] --onto <newbase>
16 'git rebase' --continue | --skip | --abort
20 If <branch> is specified, 'git rebase' will perform an automatic
21 `git checkout <branch>` before doing anything else. Otherwise
22 it remains on the current branch.
24 If <upstream> is not specified, the upstream configured in
25 branch.<name>.remote and branch.<name>.merge options will be used; see
26 linkgit:git-config[1] for details. If you are currently not on any
27 branch or if the current branch does not have a configured upstream,
28 the rebase will abort.
30 All changes made by commits in the current branch but that are not
31 in <upstream> are saved to a temporary area. This is the same set
32 of commits that would be shown by `git log <upstream>..HEAD` (or
33 `git log HEAD`, if --root is specified).
35 The current branch is reset to <upstream>, or <newbase> if the
36 --onto option was supplied. This has the exact same effect as
37 `git reset --hard <upstream>` (or <newbase>). ORIG_HEAD is set
38 to point at the tip of the branch before the reset.
40 The commits that were previously saved into the temporary area are
41 then reapplied to the current branch, one by one, in order. Note that
42 any commits in HEAD which introduce the same textual changes as a commit
43 in HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
44 with a different commit message or timestamp will be skipped).
46 It is possible that a merge failure will prevent this process from being
47 completely automatic. You will have to resolve any such merge failure
48 and run `git rebase --continue`. Another option is to bypass the commit
49 that caused the merge failure with `git rebase --skip`. To check out the
50 original <branch> and remove the .git/rebase-apply working files, use the
51 command `git rebase --abort` instead.
53 Assume the following history exists and the current branch is "topic":
61 From this point, the result of either of the following commands:
65 git rebase master topic
75 *NOTE:* The latter form is just a short-hand of `git checkout topic`
76 followed by `git rebase master`. When rebase exits `topic` will
77 remain the checked-out branch.
79 If the upstream branch already contains a change you have made (e.g.,
80 because you mailed a patch which was applied upstream), then that commit
81 will be skipped. For example, running `git rebase master` on the
82 following history (in which A' and A introduce the same set of changes,
83 but have different committer information):
99 Here is how you would transplant a topic branch based on one
100 branch to another, to pretend that you forked the topic branch
101 from the latter branch, using `rebase --onto`.
103 First let's assume your 'topic' is based on branch 'next'.
104 For example, a feature developed in 'topic' depends on some
105 functionality which is found in 'next'.
108 o---o---o---o---o master
110 o---o---o---o---o next
115 We want to make 'topic' forked from branch 'master'; for example,
116 because the functionality on which 'topic' depends was merged into the
117 more stable 'master' branch. We want our tree to look like this:
120 o---o---o---o---o master
124 o---o---o---o---o next
127 We can get this using the following command:
129 git rebase --onto master next topic
132 Another example of --onto option is to rebase part of a
133 branch. If we have the following situation:
145 git rebase --onto master topicA topicB
157 This is useful when topicB does not depend on topicA.
159 A range of commits could also be removed with rebase. If we have
160 the following situation:
163 E---F---G---H---I---J topicA
168 git rebase --onto topicA~5 topicA~3 topicA
170 would result in the removal of commits F and G:
173 E---H'---I'---J' topicA
176 This is useful if F and G were flawed in some way, or should not be
177 part of topicA. Note that the argument to --onto and the <upstream>
178 parameter can be any valid commit-ish.
180 In case of conflict, 'git rebase' will stop at the first problematic commit
181 and leave conflict markers in the tree. You can use 'git diff' to locate
182 the markers (<<<<<<) and make edits to resolve the conflict. For each
183 file you edit, you need to tell git that the conflict has been resolved,
184 typically this would be done with
190 After resolving the conflict manually and updating the index with the
191 desired resolution, you can continue the rebasing process with
194 git rebase --continue
197 Alternatively, you can undo the 'git rebase' with
206 Whether to show a diffstat of what changed upstream since the last
207 rebase. False by default.
210 If set to true enable '--autosquash' option by default.
215 Starting point at which to create the new commits. If the
216 --onto option is not specified, the starting point is
217 <upstream>. May be any valid commit, and not just an
218 existing branch name.
220 As a special case, you may use "A\...B" as a shortcut for the
221 merge base of A and B if there is exactly one merge base. You can
222 leave out at most one of A and B, in which case it defaults to HEAD.
225 Upstream branch to compare against. May be any valid commit,
226 not just an existing branch name. Defaults to the configured
227 upstream for the current branch.
230 Working branch; defaults to HEAD.
233 Restart the rebasing process after having resolved a merge conflict.
236 Abort the rebase operation and reset HEAD to the original
237 branch. If <branch> was provided when the rebase operation was
238 started, then HEAD will be reset to <branch>. Otherwise HEAD
239 will be reset to where it was when the rebase operation was
243 Restart the rebasing process by skipping the current patch.
247 Use merging strategies to rebase. When the recursive (default) merge
248 strategy is used, this allows rebase to be aware of renames on the
251 Note that a rebase merge works by replaying each commit from the working
252 branch on top of the <upstream> branch. Because of this, when a merge
253 conflict happens, the side reported as 'ours' is the so-far rebased
254 series, starting with <upstream>, and 'theirs' is the working branch. In
255 other words, the sides are swapped.
258 --strategy=<strategy>::
259 Use the given merge strategy.
260 If there is no `-s` option 'git merge-recursive' is used
261 instead. This implies --merge.
263 Because 'git rebase' replays each commit from the working branch
264 on top of the <upstream> branch using the given strategy, using
265 the 'ours' strategy simply discards all patches from the <branch>,
266 which makes little sense.
268 -X <strategy-option>::
269 --strategy-option=<strategy-option>::
270 Pass the <strategy-option> through to the merge strategy.
271 This implies `\--merge` and, if no strategy has been
272 specified, `-s recursive`. Note the reversal of 'ours' and
273 'theirs' as noted in above for the `-m` option.
277 Be quiet. Implies --no-stat.
281 Be verbose. Implies --stat.
284 Show a diffstat of what changed upstream since the last rebase. The
285 diffstat is also controlled by the configuration option rebase.stat.
289 Do not show a diffstat as part of the rebase process.
292 This option bypasses the pre-rebase hook. See also linkgit:githooks[5].
295 Allows the pre-rebase hook to run, which is the default. This option can
296 be used to override --no-verify. See also linkgit:githooks[5].
299 Ensure at least <n> lines of surrounding context match before
300 and after each change. When fewer lines of surrounding
301 context exist they all must match. By default no context is
306 Force the rebase even if the current branch is a descendant
307 of the commit you are rebasing onto. Normally non-interactive rebase will
308 exit with the message "Current branch is up to date" in such a
310 Incompatible with the --interactive option.
312 You may find this (or --no-ff with an interactive rebase) helpful after
313 reverting a topic branch merge, as this option recreates the topic branch with
314 fresh commits so it can be remerged successfully without needing to "revert
315 the reversion" (see the
316 link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
318 --ignore-whitespace::
319 --whitespace=<option>::
320 These flag are passed to the 'git apply' program
321 (see linkgit:git-apply[1]) that applies the patch.
322 Incompatible with the --interactive option.
324 --committer-date-is-author-date::
326 These flags are passed to 'git am' to easily change the dates
327 of the rebased commits (see linkgit:git-am[1]).
328 Incompatible with the --interactive option.
332 Make a list of the commits which are about to be rebased. Let the
333 user edit that list before rebasing. This mode can also be used to
334 split commits (see SPLITTING COMMITS below).
338 Instead of ignoring merges, try to recreate them.
340 This uses the `--interactive` machinery internally, but combining it
341 with the `--interactive` option explicitly is generally not a good
342 idea unless you know what you are doing (see BUGS below).
346 Rebase all commits reachable from <branch>, instead of
347 limiting them with an <upstream>. This allows you to rebase
348 the root commit(s) on a branch. Must be used with --onto, and
349 will skip changes already contained in <newbase> (instead of
350 <upstream>). When used together with --preserve-merges, 'all'
351 root commits will be rewritten to have <newbase> as parent
356 When the commit log message begins with "squash! ..." (or
357 "fixup! ..."), and there is a commit whose title begins with
358 the same ..., automatically modify the todo list of rebase -i
359 so that the commit marked for squashing comes right after the
360 commit to be modified, and change the action of the moved
361 commit from `pick` to `squash` (or `fixup`).
363 This option is only valid when the '--interactive' option is used.
365 If the '--autosquash' option is enabled by default using the
366 configuration variable `rebase.autosquash`, this option can be
367 used to override and disable this setting.
370 With --interactive, cherry-pick all rebased commits instead of
371 fast-forwarding over the unchanged ones. This ensures that the
372 entire history of the rebased branch is composed of new commits.
374 Without --interactive, this is a synonym for --force-rebase.
376 You may find this helpful after reverting a topic branch merge, as this option
377 recreates the topic branch with fresh commits so it can be remerged
378 successfully without needing to "revert the reversion" (see the
379 link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
381 include::merge-strategies.txt[]
386 You should understand the implications of using 'git rebase' on a
387 repository that you share. See also RECOVERING FROM UPSTREAM REBASE
390 When the git-rebase command is run, it will first execute a "pre-rebase"
391 hook if one exists. You can use this hook to do sanity checks and
392 reject the rebase if it isn't appropriate. Please see the template
393 pre-rebase hook script for an example.
395 Upon completion, <branch> will be the current branch.
400 Rebasing interactively means that you have a chance to edit the commits
401 which are rebased. You can reorder the commits, and you can
402 remove them (weeding out bad or otherwise unwanted patches).
404 The interactive mode is meant for this type of workflow:
406 1. have a wonderful idea
408 3. prepare a series for submission
411 where point 2. consists of several instances of
414 1. finish something worthy of a commit
417 1. realize that something does not work
421 Sometimes the thing fixed in b.2. cannot be amended to the not-quite
422 perfect commit it fixes, because that commit is buried deeply in a
423 patch series. That is exactly what interactive rebase is for: use it
424 after plenty of "a"s and "b"s, by rearranging and editing
425 commits, and squashing multiple commits into one.
427 Start it with the last commit you want to retain as-is:
429 git rebase -i <after-this-commit>
431 An editor will be fired up with all the commits in your current branch
432 (ignoring merge commits), which come after the given commit. You can
433 reorder the commits in this list to your heart's content, and you can
434 remove them. The list looks more or less like this:
436 -------------------------------------------
437 pick deadbee The oneline of this commit
438 pick fa1afe1 The oneline of the next commit
440 -------------------------------------------
442 The oneline descriptions are purely for your pleasure; 'git rebase' will
443 not look at them but at the commit names ("deadbee" and "fa1afe1" in this
444 example), so do not delete or edit the names.
446 By replacing the command "pick" with the command "edit", you can tell
447 'git rebase' to stop after applying that commit, so that you can edit
448 the files and/or the commit message, amend the commit, and continue
451 If you just want to edit the commit message for a commit, replace the
452 command "pick" with the command "reword".
454 If you want to fold two or more commits into one, replace the command
455 "pick" for the second and subsequent commits with "squash" or "fixup".
456 If the commits had different authors, the folded commit will be
457 attributed to the author of the first commit. The suggested commit
458 message for the folded commit is the concatenation of the commit
459 messages of the first commit and of those with the "squash" command,
460 but omits the commit messages of commits with the "fixup" command.
462 'git rebase' will stop when "pick" has been replaced with "edit" or
463 when a command fails due to merge errors. When you are done editing
464 and/or resolving conflicts you can continue with `git rebase --continue`.
466 For example, if you want to reorder the last 5 commits, such that what
467 was HEAD~4 becomes the new HEAD. To achieve that, you would call
468 'git rebase' like this:
470 ----------------------
471 $ git rebase -i HEAD~5
472 ----------------------
474 And move the first patch to the end of the list.
476 You might want to preserve merges, if you have a history like this:
486 Suppose you want to rebase the side branch starting at "A" to "Q". Make
487 sure that the current HEAD is "B", and call
489 -----------------------------
490 $ git rebase -i -p --onto Q O
491 -----------------------------
493 Reordering and editing commits usually creates untested intermediate
494 steps. You may want to check that your history editing did not break
495 anything by running a test, or at least recompiling at intermediate
496 points in history by using the "exec" command (shortcut "x"). You may
497 do so by creating a todo list like this one:
499 -------------------------------------------
500 pick deadbee Implement feature XXX
501 fixup f1a5c00 Fix to feature XXX
503 pick c0ffeee The oneline of the next commit
504 edit deadbab The oneline of the commit after
505 exec cd subdir; make test
507 -------------------------------------------
509 The interactive rebase will stop when a command fails (i.e. exits with
510 non-0 status) to give you an opportunity to fix the problem. You can
511 continue with `git rebase --continue`.
513 The "exec" command launches the command in a shell (the one specified
514 in `$SHELL`, or the default shell if `$SHELL` is not set), so you can
515 use shell features (like "cd", ">", ";" ...). The command is run from
516 the root of the working tree.
521 In interactive mode, you can mark commits with the action "edit". However,
522 this does not necessarily mean that 'git rebase' expects the result of this
523 edit to be exactly one commit. Indeed, you can undo the commit, or you can
524 add other commits. This can be used to split a commit into two:
526 - Start an interactive rebase with `git rebase -i <commit>^`, where
527 <commit> is the commit you want to split. In fact, any commit range
528 will do, as long as it contains that commit.
530 - Mark the commit you want to split with the action "edit".
532 - When it comes to editing that commit, execute `git reset HEAD^`. The
533 effect is that the HEAD is rewound by one, and the index follows suit.
534 However, the working tree stays the same.
536 - Now add the changes to the index that you want to have in the first
537 commit. You can use `git add` (possibly interactively) or
538 'git gui' (or both) to do that.
540 - Commit the now-current index with whatever commit message is appropriate
543 - Repeat the last two steps until your working tree is clean.
545 - Continue the rebase with `git rebase --continue`.
547 If you are not absolutely sure that the intermediate revisions are
548 consistent (they compile, pass the testsuite, etc.) you should use
549 'git stash' to stash away the not-yet-committed changes
550 after each commit, test, and amend the commit if fixes are necessary.
553 RECOVERING FROM UPSTREAM REBASE
554 -------------------------------
556 Rebasing (or any other form of rewriting) a branch that others have
557 based work on is a bad idea: anyone downstream of it is forced to
558 manually fix their history. This section explains how to do the fix
559 from the downstream's point of view. The real fix, however, would be
560 to avoid rebasing the upstream in the first place.
562 To illustrate, suppose you are in a situation where someone develops a
563 'subsystem' branch, and you are working on a 'topic' that is dependent
564 on this 'subsystem'. You might end up with a history like the
568 o---o---o---o---o---o---o---o---o master
570 o---o---o---o---o subsystem
575 If 'subsystem' is rebased against 'master', the following happens:
578 o---o---o---o---o---o---o---o master
580 o---o---o---o---o o'--o'--o'--o'--o' subsystem
585 If you now continue development as usual, and eventually merge 'topic'
586 to 'subsystem', the commits from 'subsystem' will remain duplicated forever:
589 o---o---o---o---o---o---o---o master
591 o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
593 *---*---*-..........-*--* topic
596 Such duplicates are generally frowned upon because they clutter up
597 history, making it harder to follow. To clean things up, you need to
598 transplant the commits on 'topic' to the new 'subsystem' tip, i.e.,
599 rebase 'topic'. This becomes a ripple effect: anyone downstream from
600 'topic' is forced to rebase too, and so on!
602 There are two kinds of fixes, discussed in the following subsections:
604 Easy case: The changes are literally the same.::
606 This happens if the 'subsystem' rebase was a simple rebase and
609 Hard case: The changes are not the same.::
611 This happens if the 'subsystem' rebase had conflicts, or used
612 `\--interactive` to omit, edit, squash, or fixup commits; or
613 if the upstream used one of `commit \--amend`, `reset`, or
620 Only works if the changes (patch IDs based on the diff contents) on
621 'subsystem' are literally the same before and after the rebase
624 In that case, the fix is easy because 'git rebase' knows to skip
625 changes that are already present in the new upstream. So if you say
626 (assuming you're on 'topic')
628 $ git rebase subsystem
630 you will end up with the fixed history
632 o---o---o---o---o---o---o---o master
634 o'--o'--o'--o'--o' subsystem
643 Things get more complicated if the 'subsystem' changes do not exactly
644 correspond to the ones before the rebase.
646 NOTE: While an "easy case recovery" sometimes appears to be successful
647 even in the hard case, it may have unintended consequences. For
648 example, a commit that was removed via `git rebase
649 \--interactive` will be **resurrected**!
651 The idea is to manually tell 'git rebase' "where the old 'subsystem'
652 ended and your 'topic' began", that is, what the old merge-base
653 between them was. You will have to find a way to name the last commit
654 of the old 'subsystem', for example:
656 * With the 'subsystem' reflog: after 'git fetch', the old tip of
657 'subsystem' is at `subsystem@\{1}`. Subsequent fetches will
658 increase the number. (See linkgit:git-reflog[1].)
660 * Relative to the tip of 'topic': knowing that your 'topic' has three
661 commits, the old tip of 'subsystem' must be `topic~3`.
663 You can then transplant the old `subsystem..topic` to the new tip by
664 saying (for the reflog case, and assuming you are on 'topic' already):
666 $ git rebase --onto subsystem subsystem@{1}
669 The ripple effect of a "hard case" recovery is especially bad:
670 'everyone' downstream from 'topic' will now have to perform a "hard
675 The todo list presented by `--preserve-merges --interactive` does not
676 represent the topology of the revision graph. Editing commits and
677 rewording their commit messages should work fine, but attempts to
678 reorder commits tend to produce counterintuitive results.
680 For example, an attempt to rearrange
682 1 --- 2 --- 3 --- 4 --- 5
686 1 --- 2 --- 4 --- 3 --- 5
688 by moving the "pick 4" line will result in the following history:
697 Part of the linkgit:git[1] suite