1GIT-REBASE(1) Git Manual GIT-REBASE(1)
2
3
4
6 git-rebase - Forward-port local commits to the updated upstream head
7
9 git rebase [-i | --interactive] [options] [--onto <newbase>]
10 <upstream> [<branch>]
11 git rebase [-i | --interactive] [options] --onto <newbase>
12 --root [<branch>]
13
14
15 git rebase --continue | --skip | --abort
16
18 If <branch> is specified, git rebase will perform an automatic git
19 checkout <branch> before doing anything else. Otherwise it remains on
20 the current branch.
21
22 All changes made by commits in the current branch but that are not in
23 <upstream> are saved to a temporary area. This is the same set of
24 commits that would be shown by git log <upstream>..HEAD (or git log
25 HEAD, if --root is specified).
26
27 The current branch is reset to <upstream>, or <newbase> if the --onto
28 option was supplied. This has the exact same effect as git reset --hard
29 <upstream> (or <newbase>). ORIG_HEAD is set to point at the tip of the
30 branch before the reset.
31
32 The commits that were previously saved into the temporary area are then
33 reapplied to the current branch, one by one, in order. Note that any
34 commits in HEAD which introduce the same textual changes as a commit in
35 HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
36 with a different commit message or timestamp will be skipped).
37
38 It is possible that a merge failure will prevent this process from
39 being completely automatic. You will have to resolve any such merge
40 failure and run git rebase --continue. Another option is to bypass the
41 commit that caused the merge failure with git rebase --skip. To restore
42 the original <branch> and remove the .git/rebase-apply working files,
43 use the command git rebase --abort instead.
44
45 Assume the following history exists and the current branch is "topic":
46
47 A---B---C topic
48 /
49 D---E---F---G master
50
51
52 From this point, the result of either of the following commands:
53
54 git rebase master
55 git rebase master topic
56
57 would be:
58
59 A'--B'--C' topic
60 /
61 D---E---F---G master
62
63
64 The latter form is just a short-hand of git checkout topic followed by
65 git rebase master.
66
67 If the upstream branch already contains a change you have made (e.g.,
68 because you mailed a patch which was applied upstream), then that
69 commit will be skipped. For example, running ‘git rebase master` on the
70 following history (in which A’ and A introduce the same set of changes,
71 but have different committer information):
72
73 A---B---C topic
74 /
75 D---E---A'---F master
76
77
78 will result in:
79
80 B'---C' topic
81 /
82 D---E---A'---F master
83
84
85 Here is how you would transplant a topic branch based on one branch to
86 another, to pretend that you forked the topic branch from the latter
87 branch, using rebase --onto.
88
89 First let’s assume your topic is based on branch next. For example, a
90 feature developed in topic depends on some functionality which is found
91 in next.
92
93 o---o---o---o---o master
94 \
95 o---o---o---o---o next
96 \
97 o---o---o topic
98
99
100 We want to make topic forked from branch master; for example, because
101 the functionality on which topic depends was merged into the more
102 stable master branch. We want our tree to look like this:
103
104 o---o---o---o---o master
105 | \
106 | o'--o'--o' topic
107 \
108 o---o---o---o---o next
109
110
111 We can get this using the following command:
112
113 git rebase --onto master next topic
114
115 Another example of --onto option is to rebase part of a branch. If we
116 have the following situation:
117
118 H---I---J topicB
119 /
120 E---F---G topicA
121 /
122 A---B---C---D master
123
124
125 then the command
126
127 git rebase --onto master topicA topicB
128
129 would result in:
130
131 H'--I'--J' topicB
132 /
133 | E---F---G topicA
134 |/
135 A---B---C---D master
136
137
138 This is useful when topicB does not depend on topicA.
139
140 A range of commits could also be removed with rebase. If we have the
141 following situation:
142
143 E---F---G---H---I---J topicA
144
145
146 then the command
147
148 git rebase --onto topicA~5 topicA~3 topicA
149
150 would result in the removal of commits F and G:
151
152 E---H'---I'---J' topicA
153
154
155 This is useful if F and G were flawed in some way, or should not be
156 part of topicA. Note that the argument to --onto and the <upstream>
157 parameter can be any valid commit-ish.
158
159 In case of conflict, git rebase will stop at the first problematic
160 commit and leave conflict markers in the tree. You can use git diff to
161 locate the markers (<<<<<<) and make edits to resolve the conflict. For
162 each file you edit, you need to tell git that the conflict has been
163 resolved, typically this would be done with
164
165 git add <filename>
166
167 After resolving the conflict manually and updating the index with the
168 desired resolution, you can continue the rebasing process with
169
170 git rebase --continue
171
172 Alternatively, you can undo the git rebase with
173
174 git rebase --abort
175
177 rebase.stat
178 Whether to show a diffstat of what changed upstream since the last
179 rebase. False by default.
180
181 rebase.autosquash
182 If set to true enable --autosquash option by default.
183
185 <newbase>
186 Starting point at which to create the new commits. If the --onto
187 option is not specified, the starting point is <upstream>. May be
188 any valid commit, and not just an existing branch name.
189
190 As a special case, you may use "A...B" as a shortcut for the merge
191 base of A and B if there is exactly one merge base. You can leave
192 out at most one of A and B, in which case it defaults to HEAD.
193
194 <upstream>
195 Upstream branch to compare against. May be any valid commit, not
196 just an existing branch name.
197
198 <branch>
199 Working branch; defaults to HEAD.
200
201 --continue
202 Restart the rebasing process after having resolved a merge
203 conflict.
204
205 --abort
206 Restore the original branch and abort the rebase operation.
207
208 --skip
209 Restart the rebasing process by skipping the current patch.
210
211 -m, --merge
212 Use merging strategies to rebase. When the recursive (default)
213 merge strategy is used, this allows rebase to be aware of renames
214 on the upstream side.
215
216 Note that a rebase merge works by replaying each commit from the
217 working branch on top of the <upstream> branch. Because of this,
218 when a merge conflict happens, the side reported as ours is the
219 so-far rebased series, starting with <upstream>, and theirs is the
220 working branch. In other words, the sides are swapped.
221
222 -s <strategy>, --strategy=<strategy>
223 Use the given merge strategy. If there is no -s option git
224 merge-recursive is used instead. This implies --merge.
225
226 Because git rebase replays each commit from the working branch on
227 top of the <upstream> branch using the given strategy, using the
228 ours strategy simply discards all patches from the <branch>, which
229 makes little sense.
230
231 -X <strategy-option>, --strategy-option=<strategy-option>
232 Pass the <strategy-option> through to the merge strategy. This
233 implies --merge and, if no strategy has been specified, -s
234 recursive. Note the reversal of ours and theirs as noted in above
235 for the -m option.
236
237 -q, --quiet
238 Be quiet. Implies --no-stat.
239
240 -v, --verbose
241 Be verbose. Implies --stat.
242
243 --stat
244 Show a diffstat of what changed upstream since the last rebase. The
245 diffstat is also controlled by the configuration option
246 rebase.stat.
247
248 -n, --no-stat
249 Do not show a diffstat as part of the rebase process.
250
251 --no-verify
252 This option bypasses the pre-rebase hook. See also githooks(5).
253
254 --verify
255 Allows the pre-rebase hook to run, which is the default. This
256 option can be used to override --no-verify. See also githooks(5).
257
258 -C<n>
259 Ensure at least <n> lines of surrounding context match before and
260 after each change. When fewer lines of surrounding context exist
261 they all must match. By default no context is ever ignored.
262
263 -f, --force-rebase
264 Force the rebase even if the current branch is a descendant of the
265 commit you are rebasing onto. Normally non-interactive rebase will
266 exit with the message "Current branch is up to date" in such a
267 situation. Incompatible with the --interactive option.
268
269 You may find this (or --no-ff with an interactive rebase) helpful
270 after reverting a topic branch merge, as this option recreates the
271 topic branch with fresh commits so it can be remerged successfully
272 without needing to "revert the reversion" (see the
273 revert-a-faulty-merge How-To[1] for details).
274
275 --ignore-whitespace, --whitespace=<option>
276 These flag are passed to the git apply program (see git-apply(1))
277 that applies the patch. Incompatible with the --interactive option.
278
279 --committer-date-is-author-date, --ignore-date
280 These flags are passed to git am to easily change the dates of the
281 rebased commits (see git-am(1)). Incompatible with the
282 --interactive option.
283
284 -i, --interactive
285 Make a list of the commits which are about to be rebased. Let the
286 user edit that list before rebasing. This mode can also be used to
287 split commits (see SPLITTING COMMITS below).
288
289 -p, --preserve-merges
290 Instead of ignoring merges, try to recreate them.
291
292 This uses the --interactive machinery internally, but combining it
293 with the --interactive option explicitly is generally not a good
294 idea unless you know what you are doing (see BUGS below).
295
296 --root
297 Rebase all commits reachable from <branch>, instead of limiting
298 them with an <upstream>. This allows you to rebase the root
299 commit(s) on a branch. Must be used with --onto, and will skip
300 changes already contained in <newbase> (instead of <upstream>).
301 When used together with --preserve-merges, all root commits will be
302 rewritten to have <newbase> as parent instead.
303
304 --autosquash, --no-autosquash
305 When the commit log message begins with "squash! ..." (or "fixup!
306 ..."), and there is a commit whose title begins with the same ...,
307 automatically modify the todo list of rebase -i so that the commit
308 marked for squashing comes right after the commit to be modified,
309 and change the action of the moved commit from pick to squash (or
310 fixup).
311
312 This option is only valid when the --interactive option is used.
313
314 If the --autosquash option is enabled by default using the
315 configuration variable rebase.autosquash, this option can be used
316 to override and disable this setting.
317
318 --no-ff
319 With --interactive, cherry-pick all rebased commits instead of
320 fast-forwarding over the unchanged ones. This ensures that the
321 entire history of the rebased branch is composed of new commits.
322
323 Without --interactive, this is a synonym for --force-rebase.
324
325 You may find this helpful after reverting a topic branch merge, as
326 this option recreates the topic branch with fresh commits so it can
327 be remerged successfully without needing to "revert the reversion"
328 (see the revert-a-faulty-merge How-To[1] for details).
329
331 The merge mechanism (git-merge and git-pull commands) allows the
332 backend merge strategies to be chosen with -s option. Some strategies
333 can also take their own options, which can be passed by giving
334 -X<option> arguments to git-merge and/or git-pull.
335
336 resolve
337 This can only resolve two heads (i.e. the current branch and
338 another branch you pulled from) using a 3-way merge algorithm. It
339 tries to carefully detect criss-cross merge ambiguities and is
340 considered generally safe and fast.
341
342 recursive
343 This can only resolve two heads using a 3-way merge algorithm. When
344 there is more than one common ancestor that can be used for 3-way
345 merge, it creates a merged tree of the common ancestors and uses
346 that as the reference tree for the 3-way merge. This has been
347 reported to result in fewer merge conflicts without causing
348 mis-merges by tests done on actual merge commits taken from Linux
349 2.6 kernel development history. Additionally this can detect and
350 handle merges involving renames. This is the default merge strategy
351 when pulling or merging one branch.
352
353 The recursive strategy can take the following options:
354
355 ours
356 This option forces conflicting hunks to be auto-resolved
357 cleanly by favoring our version. Changes from the other tree
358 that do not conflict with our side are reflected to the merge
359 result.
360
361 This should not be confused with the ours merge strategy, which
362 does not even look at what the other tree contains at all. It
363 discards everything the other tree did, declaring our history
364 contains all that happened in it.
365
366 theirs
367 This is opposite of ours.
368
369 patience
370 With this option, merge-recursive spends a little extra time to
371 avoid mismerges that sometimes occur due to unimportant
372 matching lines (e.g., braces from distinct functions). Use this
373 when the branches to be merged have diverged wildly. See also
374 git-diff(1) --patience.
375
376 ignore-space-change, ignore-all-space, ignore-space-at-eol
377 Treats lines with the indicated type of whitespace change as
378 unchanged for the sake of a three-way merge. Whitespace changes
379 mixed with other changes to a line are not ignored. See also
380 git-diff(1) -b, -w, and --ignore-space-at-eol.
381
382 · If their version only introduces whitespace changes to a
383 line, our version is used;
384
385 · If our version introduces whitespace changes but their
386 version includes a substantial change, their version is
387 used;
388
389 · Otherwise, the merge proceeds in the usual way.
390
391 renormalize
392 This runs a virtual check-out and check-in of all three stages
393 of a file when resolving a three-way merge. This option is
394 meant to be used when merging branches with different clean
395 filters or end-of-line normalization rules. See "Merging
396 branches with differing checkin/checkout attributes" in
397 gitattributes(5) for details.
398
399 no-renormalize
400 Disables the renormalize option. This overrides the
401 merge.renormalize configuration variable.
402
403 rename-threshold=<n>
404 Controls the similarity threshold used for rename detection.
405 See also git-diff(1) -M.
406
407 subtree[=<path>]
408 This option is a more advanced form of subtree strategy, where
409 the strategy makes a guess on how two trees must be shifted to
410 match with each other when merging. Instead, the specified path
411 is prefixed (or stripped from the beginning) to make the shape
412 of two trees to match.
413
414 octopus
415 This resolves cases with more than two heads, but refuses to do a
416 complex merge that needs manual resolution. It is primarily meant
417 to be used for bundling topic branch heads together. This is the
418 default merge strategy when pulling or merging more than one
419 branch.
420
421 ours
422 This resolves any number of heads, but the resulting tree of the
423 merge is always that of the current branch head, effectively
424 ignoring all changes from all other branches. It is meant to be
425 used to supersede old development history of side branches. Note
426 that this is different from the -Xours option to the recursive
427 merge strategy.
428
429 subtree
430 This is a modified recursive strategy. When merging trees A and B,
431 if B corresponds to a subtree of A, B is first adjusted to match
432 the tree structure of A, instead of reading the trees at the same
433 level. This adjustment is also done to the common ancestor tree.
434
436 You should understand the implications of using git rebase on a
437 repository that you share. See also RECOVERING FROM UPSTREAM REBASE
438 below.
439
440 When the git-rebase command is run, it will first execute a
441 "pre-rebase" hook if one exists. You can use this hook to do sanity
442 checks and reject the rebase if it isn’t appropriate. Please see the
443 template pre-rebase hook script for an example.
444
445 Upon completion, <branch> will be the current branch.
446
448 Rebasing interactively means that you have a chance to edit the commits
449 which are rebased. You can reorder the commits, and you can remove them
450 (weeding out bad or otherwise unwanted patches).
451
452 The interactive mode is meant for this type of workflow:
453
454 1. have a wonderful idea
455
456 2. hack on the code
457
458 3. prepare a series for submission
459
460 4. submit
461
462 where point 2. consists of several instances of
463
464 1. regular use
465
466 1. finish something worthy of a commit
467
468 2. commit
469
470 2. independent fixup
471
472 1. realize that something does not work
473
474 2. fix that
475
476 3. commit it
477
478 Sometimes the thing fixed in b.2. cannot be amended to the not-quite
479 perfect commit it fixes, because that commit is buried deeply in a
480 patch series. That is exactly what interactive rebase is for: use it
481 after plenty of "a"s and "b"s, by rearranging and editing commits, and
482 squashing multiple commits into one.
483
484 Start it with the last commit you want to retain as-is:
485
486 git rebase -i <after-this-commit>
487
488 An editor will be fired up with all the commits in your current branch
489 (ignoring merge commits), which come after the given commit. You can
490 reorder the commits in this list to your heart’s content, and you can
491 remove them. The list looks more or less like this:
492
493 pick deadbee The oneline of this commit
494 pick fa1afe1 The oneline of the next commit
495 ...
496
497
498 The oneline descriptions are purely for your pleasure; git rebase will
499 not look at them but at the commit names ("deadbee" and "fa1afe1" in
500 this example), so do not delete or edit the names.
501
502 By replacing the command "pick" with the command "edit", you can tell
503 git rebase to stop after applying that commit, so that you can edit the
504 files and/or the commit message, amend the commit, and continue
505 rebasing.
506
507 If you just want to edit the commit message for a commit, replace the
508 command "pick" with the command "reword".
509
510 If you want to fold two or more commits into one, replace the command
511 "pick" for the second and subsequent commits with "squash" or "fixup".
512 If the commits had different authors, the folded commit will be
513 attributed to the author of the first commit. The suggested commit
514 message for the folded commit is the concatenation of the commit
515 messages of the first commit and of those with the "squash" command,
516 but omits the commit messages of commits with the "fixup" command.
517
518 git rebase will stop when "pick" has been replaced with "edit" or when
519 a command fails due to merge errors. When you are done editing and/or
520 resolving conflicts you can continue with git rebase --continue.
521
522 For example, if you want to reorder the last 5 commits, such that what
523 was HEAD~4 becomes the new HEAD. To achieve that, you would call git
524 rebase like this:
525
526 $ git rebase -i HEAD~5
527
528
529 And move the first patch to the end of the list.
530
531 You might want to preserve merges, if you have a history like this:
532
533 X
534 \
535 A---M---B
536 /
537 ---o---O---P---Q
538
539
540 Suppose you want to rebase the side branch starting at "A" to "Q". Make
541 sure that the current HEAD is "B", and call
542
543 $ git rebase -i -p --onto Q O
544
545
546 Reordering and editing commits usually creates untested intermediate
547 steps. You may want to check that your history editing did not break
548 anything by running a test, or at least recompiling at intermediate
549 points in history by using the "exec" command (shortcut "x"). You may
550 do so by creating a todo list like this one:
551
552 pick deadbee Implement feature XXX
553 fixup f1a5c00 Fix to feature XXX
554 exec make
555 pick c0ffeee The oneline of the next commit
556 edit deadbab The oneline of the commit after
557 exec cd subdir; make test
558 ...
559
560
561 The interactive rebase will stop when a command fails (i.e. exits with
562 non-0 status) to give you an opportunity to fix the problem. You can
563 continue with git rebase --continue.
564
565 The "exec" command launches the command in a shell (the one specified
566 in $SHELL, or the default shell if $SHELL is not set), so you can use
567 shell features (like "cd", ">", ";" ...). The command is run from the
568 root of the working tree.
569
571 In interactive mode, you can mark commits with the action "edit".
572 However, this does not necessarily mean that git rebase expects the
573 result of this edit to be exactly one commit. Indeed, you can undo the
574 commit, or you can add other commits. This can be used to split a
575 commit into two:
576
577 · Start an interactive rebase with git rebase -i <commit>^, where
578 <commit> is the commit you want to split. In fact, any commit range
579 will do, as long as it contains that commit.
580
581 · Mark the commit you want to split with the action "edit".
582
583 · When it comes to editing that commit, execute git reset HEAD^. The
584 effect is that the HEAD is rewound by one, and the index follows
585 suit. However, the working tree stays the same.
586
587 · Now add the changes to the index that you want to have in the first
588 commit. You can use git add (possibly interactively) or git gui (or
589 both) to do that.
590
591 · Commit the now-current index with whatever commit message is
592 appropriate now.
593
594 · Repeat the last two steps until your working tree is clean.
595
596 · Continue the rebase with git rebase --continue.
597
598 If you are not absolutely sure that the intermediate revisions are
599 consistent (they compile, pass the testsuite, etc.) you should use git
600 stash to stash away the not-yet-committed changes after each commit,
601 test, and amend the commit if fixes are necessary.
602
604 Rebasing (or any other form of rewriting) a branch that others have
605 based work on is a bad idea: anyone downstream of it is forced to
606 manually fix their history. This section explains how to do the fix
607 from the downstream’s point of view. The real fix, however, would be to
608 avoid rebasing the upstream in the first place.
609
610 To illustrate, suppose you are in a situation where someone develops a
611 subsystem branch, and you are working on a topic that is dependent on
612 this subsystem. You might end up with a history like the following:
613
614 o---o---o---o---o---o---o---o---o master
615 \
616 o---o---o---o---o subsystem
617 \
618 *---*---* topic
619
620
621 If subsystem is rebased against master, the following happens:
622
623 o---o---o---o---o---o---o---o master
624 \ \
625 o---o---o---o---o o'--o'--o'--o'--o' subsystem
626 \
627 *---*---* topic
628
629
630 If you now continue development as usual, and eventually merge topic to
631 subsystem, the commits from subsystem will remain duplicated forever:
632
633 o---o---o---o---o---o---o---o master
634 \ \
635 o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
636 \ /
637 *---*---*-..........-*--* topic
638
639
640 Such duplicates are generally frowned upon because they clutter up
641 history, making it harder to follow. To clean things up, you need to
642 transplant the commits on topic to the new subsystem tip, i.e., rebase
643 topic. This becomes a ripple effect: anyone downstream from topic is
644 forced to rebase too, and so on!
645
646 There are two kinds of fixes, discussed in the following subsections:
647
648 Easy case: The changes are literally the same.
649 This happens if the subsystem rebase was a simple rebase and had no
650 conflicts.
651
652 Hard case: The changes are not the same.
653 This happens if the subsystem rebase had conflicts, or used
654 --interactive to omit, edit, squash, or fixup commits; or if the
655 upstream used one of commit --amend, reset, or filter-branch.
656
657 The easy case
658 Only works if the changes (patch IDs based on the diff contents) on
659 subsystem are literally the same before and after the rebase subsystem
660 did.
661
662 In that case, the fix is easy because git rebase knows to skip changes
663 that are already present in the new upstream. So if you say (assuming
664 you’re on topic)
665
666 $ git rebase subsystem
667
668
669 you will end up with the fixed history
670
671 o---o---o---o---o---o---o---o master
672 \
673 o'--o'--o'--o'--o' subsystem
674 \
675 *---*---* topic
676
677
678 The hard case
679 Things get more complicated if the subsystem changes do not exactly
680 correspond to the ones before the rebase.
681
682 Note
683 While an "easy case recovery" sometimes appears to be successful
684 even in the hard case, it may have unintended consequences. For
685 example, a commit that was removed via git rebase --interactive
686 will be resurrected!
687
688 The idea is to manually tell git rebase "where the old subsystem ended
689 and your topic began", that is, what the old merge-base between them
690 was. You will have to find a way to name the last commit of the old
691 subsystem, for example:
692
693 · With the subsystem reflog: after git fetch, the old tip of
694 subsystem is at subsystem@{1}. Subsequent fetches will increase the
695 number. (See git-reflog(1).)
696
697 · Relative to the tip of topic: knowing that your topic has three
698 commits, the old tip of subsystem must be topic~3.
699
700 You can then transplant the old subsystem..topic to the new tip by
701 saying (for the reflog case, and assuming you are on topic already):
702
703 $ git rebase --onto subsystem subsystem@{1}
704
705
706 The ripple effect of a "hard case" recovery is especially bad: everyone
707 downstream from topic will now have to perform a "hard case" recovery
708 too!
709
711 The todo list presented by --preserve-merges --interactive does not
712 represent the topology of the revision graph. Editing commits and
713 rewording their commit messages should work fine, but attempts to
714 reorder commits tend to produce counterintuitive results.
715
716 For example, an attempt to rearrange
717
718 1 --- 2 --- 3 --- 4 --- 5
719
720
721 to
722
723 1 --- 2 --- 4 --- 3 --- 5
724
725
726 by moving the "pick 4" line will result in the following history:
727
728 3
729 /
730 1 --- 2 --- 4 --- 5
731
732
734 Written by Junio C Hamano <gitster@pobox.com[2]> and Johannes E.
735 Schindelin <johannes.schindelin@gmx.de[3]>
736
738 Documentation by Junio C Hamano and the git-list
739 <git@vger.kernel.org[4]>.
740
742 Part of the git(1) suite
743
745 1. revert-a-faulty-merge How-To
746 file:///usr/share/doc/git-1.7.4.4/howto/revert-a-faulty-merge.txt
747
748 2. gitster@pobox.com
749 mailto:gitster@pobox.com
750
751 3. johannes.schindelin@gmx.de
752 mailto:johannes.schindelin@gmx.de
753
754 4. git@vger.kernel.org
755 mailto:git@vger.kernel.org
756
757
758
759Git 1.7.4.4 04/11/2011 GIT-REBASE(1)