1GIT-REBASE(1) Git Manual GIT-REBASE(1)
2
3
4
6 git-rebase - Reapply commits on top of another base tip
7
9 git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
10 [<upstream> [<branch>]]
11 git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
12 --root [<branch>]
13 git rebase --continue | --skip | --abort | --quit | --edit-todo | --show-current-patch
14
15
17 If <branch> is specified, git rebase will perform an automatic git
18 checkout <branch> before doing anything else. Otherwise it remains on
19 the current branch.
20
21 If <upstream> is not specified, the upstream configured in
22 branch.<name>.remote and branch.<name>.merge options will be used (see
23 git-config(1) for details) and the --fork-point option is assumed. If
24 you are currently not on any branch or if the current branch does not
25 have a configured upstream, the rebase will abort.
26
27 All changes made by commits in the current branch but that are not in
28 <upstream> are saved to a temporary area. This is the same set of
29 commits that would be shown by git log <upstream>..HEAD; or by git log
30 'fork_point'..HEAD, if --fork-point is active (see the description on
31 --fork-point below); or by git log HEAD, if the --root option is
32 specified.
33
34 The current branch is reset to <upstream>, or <newbase> if the --onto
35 option was supplied. This has the exact same effect as git reset --hard
36 <upstream> (or <newbase>). ORIG_HEAD is set to point at the tip of the
37 branch before the reset.
38
39 The commits that were previously saved into the temporary area are then
40 reapplied to the current branch, one by one, in order. Note that any
41 commits in HEAD which introduce the same textual changes as a commit in
42 HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
43 with a different commit message or timestamp will be skipped).
44
45 It is possible that a merge failure will prevent this process from
46 being completely automatic. You will have to resolve any such merge
47 failure and run git rebase --continue. Another option is to bypass the
48 commit that caused the merge failure with git rebase --skip. To check
49 out the original <branch> and remove the .git/rebase-apply working
50 files, use the command git rebase --abort instead.
51
52 Assume the following history exists and the current branch is "topic":
53
54 A---B---C topic
55 /
56 D---E---F---G master
57
58
59 From this point, the result of either of the following commands:
60
61 git rebase master
62 git rebase master topic
63
64 would be:
65
66 A'--B'--C' topic
67 /
68 D---E---F---G master
69
70
71 NOTE: The latter form is just a short-hand of git checkout topic
72 followed by git rebase master. When rebase exits topic will remain the
73 checked-out branch.
74
75 If the upstream branch already contains a change you have made (e.g.,
76 because you mailed a patch which was applied upstream), then that
77 commit will be skipped. For example, running git rebase master on the
78 following history (in which A' and A introduce the same set of changes,
79 but have different committer information):
80
81 A---B---C topic
82 /
83 D---E---A'---F master
84
85
86 will result in:
87
88 B'---C' topic
89 /
90 D---E---A'---F master
91
92
93 Here is how you would transplant a topic branch based on one branch to
94 another, to pretend that you forked the topic branch from the latter
95 branch, using rebase --onto.
96
97 First let’s assume your topic is based on branch next. For example, a
98 feature developed in topic depends on some functionality which is found
99 in next.
100
101 o---o---o---o---o master
102 \
103 o---o---o---o---o next
104 \
105 o---o---o topic
106
107
108 We want to make topic forked from branch master; for example, because
109 the functionality on which topic depends was merged into the more
110 stable master branch. We want our tree to look like this:
111
112 o---o---o---o---o master
113 | \
114 | o'--o'--o' topic
115 \
116 o---o---o---o---o next
117
118
119 We can get this using the following command:
120
121 git rebase --onto master next topic
122
123 Another example of --onto option is to rebase part of a branch. If we
124 have the following situation:
125
126 H---I---J topicB
127 /
128 E---F---G topicA
129 /
130 A---B---C---D master
131
132
133 then the command
134
135 git rebase --onto master topicA topicB
136
137 would result in:
138
139 H'--I'--J' topicB
140 /
141 | E---F---G topicA
142 |/
143 A---B---C---D master
144
145
146 This is useful when topicB does not depend on topicA.
147
148 A range of commits could also be removed with rebase. If we have the
149 following situation:
150
151 E---F---G---H---I---J topicA
152
153
154 then the command
155
156 git rebase --onto topicA~5 topicA~3 topicA
157
158 would result in the removal of commits F and G:
159
160 E---H'---I'---J' topicA
161
162
163 This is useful if F and G were flawed in some way, or should not be
164 part of topicA. Note that the argument to --onto and the <upstream>
165 parameter can be any valid commit-ish.
166
167 In case of conflict, git rebase will stop at the first problematic
168 commit and leave conflict markers in the tree. You can use git diff to
169 locate the markers (<<<<<<) and make edits to resolve the conflict. For
170 each file you edit, you need to tell Git that the conflict has been
171 resolved, typically this would be done with
172
173 git add <filename>
174
175 After resolving the conflict manually and updating the index with the
176 desired resolution, you can continue the rebasing process with
177
178 git rebase --continue
179
180 Alternatively, you can undo the git rebase with
181
182 git rebase --abort
183
185 rebase.stat
186 Whether to show a diffstat of what changed upstream since the last
187 rebase. False by default.
188
189 rebase.autoSquash
190 If set to true enable --autosquash option by default.
191
192 rebase.autoStash
193 When set to true, automatically create a temporary stash entry
194 before the operation begins, and apply it after the operation ends.
195 This means that you can run rebase on a dirty worktree. However,
196 use with care: the final stash application after a successful
197 rebase might result in non-trivial conflicts. This option can be
198 overridden by the --no-autostash and --autostash options of git-
199 rebase(1). Defaults to false.
200
201 rebase.missingCommitsCheck
202 If set to "warn", git rebase -i will print a warning if some
203 commits are removed (e.g. a line was deleted), however the rebase
204 will still proceed. If set to "error", it will print the previous
205 warning and stop the rebase, git rebase --edit-todo can then be
206 used to correct the error. If set to "ignore", no checking is done.
207 To drop a commit without warning or error, use the drop command in
208 the todo list. Defaults to "ignore".
209
210 rebase.instructionFormat
211 A format string, as specified in git-log(1), to be used for the
212 todo list during an interactive rebase. The format will
213 automatically have the long commit hash prepended to the format.
214
215 rebase.abbreviateCommands
216 If set to true, git rebase will use abbreviated command names in
217 the todo list resulting in something like this:
218
219 p deadbee The oneline of the commit
220 p fa1afe1 The oneline of the next commit
221 ...
222
223 instead of:
224
225 pick deadbee The oneline of the commit
226 pick fa1afe1 The oneline of the next commit
227 ...
228
229 Defaults to false.
230
232 --onto <newbase>
233 Starting point at which to create the new commits. If the --onto
234 option is not specified, the starting point is <upstream>. May be
235 any valid commit, and not just an existing branch name.
236
237 As a special case, you may use "A...B" as a shortcut for the merge
238 base of A and B if there is exactly one merge base. You can leave
239 out at most one of A and B, in which case it defaults to HEAD.
240
241 <upstream>
242 Upstream branch to compare against. May be any valid commit, not
243 just an existing branch name. Defaults to the configured upstream
244 for the current branch.
245
246 <branch>
247 Working branch; defaults to HEAD.
248
249 --continue
250 Restart the rebasing process after having resolved a merge
251 conflict.
252
253 --abort
254 Abort the rebase operation and reset HEAD to the original branch.
255 If <branch> was provided when the rebase operation was started,
256 then HEAD will be reset to <branch>. Otherwise HEAD will be reset
257 to where it was when the rebase operation was started.
258
259 --quit
260 Abort the rebase operation but HEAD is not reset back to the
261 original branch. The index and working tree are also left unchanged
262 as a result.
263
264 --keep-empty
265 Keep the commits that do not change anything from its parents in
266 the result.
267
268 --allow-empty-message
269 By default, rebasing commits with an empty message will fail. This
270 option overrides that behavior, allowing commits with empty
271 messages to be rebased.
272
273 --skip
274 Restart the rebasing process by skipping the current patch.
275
276 --edit-todo
277 Edit the todo list during an interactive rebase.
278
279 --show-current-patch
280 Show the current patch in an interactive rebase or when rebase is
281 stopped because of conflicts. This is the equivalent of git show
282 REBASE_HEAD.
283
284 -m, --merge
285 Use merging strategies to rebase. When the recursive (default)
286 merge strategy is used, this allows rebase to be aware of renames
287 on the upstream side.
288
289 Note that a rebase merge works by replaying each commit from the
290 working branch on top of the <upstream> branch. Because of this,
291 when a merge conflict happens, the side reported as ours is the
292 so-far rebased series, starting with <upstream>, and theirs is the
293 working branch. In other words, the sides are swapped.
294
295 -s <strategy>, --strategy=<strategy>
296 Use the given merge strategy. If there is no -s option git
297 merge-recursive is used instead. This implies --merge.
298
299 Because git rebase replays each commit from the working branch on
300 top of the <upstream> branch using the given strategy, using the
301 ours strategy simply discards all patches from the <branch>, which
302 makes little sense.
303
304 -X <strategy-option>, --strategy-option=<strategy-option>
305 Pass the <strategy-option> through to the merge strategy. This
306 implies --merge and, if no strategy has been specified, -s
307 recursive. Note the reversal of ours and theirs as noted above for
308 the -m option.
309
310 -S[<keyid>], --gpg-sign[=<keyid>]
311 GPG-sign commits. The keyid argument is optional and defaults to
312 the committer identity; if specified, it must be stuck to the
313 option without a space.
314
315 -q, --quiet
316 Be quiet. Implies --no-stat.
317
318 -v, --verbose
319 Be verbose. Implies --stat.
320
321 --stat
322 Show a diffstat of what changed upstream since the last rebase. The
323 diffstat is also controlled by the configuration option
324 rebase.stat.
325
326 -n, --no-stat
327 Do not show a diffstat as part of the rebase process.
328
329 --no-verify
330 This option bypasses the pre-rebase hook. See also githooks(5).
331
332 --verify
333 Allows the pre-rebase hook to run, which is the default. This
334 option can be used to override --no-verify. See also githooks(5).
335
336 -C<n>
337 Ensure at least <n> lines of surrounding context match before and
338 after each change. When fewer lines of surrounding context exist
339 they all must match. By default no context is ever ignored.
340
341 -f, --force-rebase
342 Force a rebase even if the current branch is up to date and the
343 command without --force would return without doing anything.
344
345 You may find this (or --no-ff with an interactive rebase) helpful
346 after reverting a topic branch merge, as this option recreates the
347 topic branch with fresh commits so it can be remerged successfully
348 without needing to "revert the reversion" (see the
349 revert-a-faulty-merge How-To[1] for details).
350
351 --fork-point, --no-fork-point
352 Use reflog to find a better common ancestor between <upstream> and
353 <branch> when calculating which commits have been introduced by
354 <branch>.
355
356 When --fork-point is active, fork_point will be used instead of
357 <upstream> to calculate the set of commits to rebase, where
358 fork_point is the result of git merge-base --fork-point <upstream>
359 <branch> command (see git-merge-base(1)). If fork_point ends up
360 being empty, the <upstream> will be used as a fallback.
361
362 If either <upstream> or --root is given on the command line, then
363 the default is --no-fork-point, otherwise the default is
364 --fork-point.
365
366 --ignore-whitespace, --whitespace=<option>
367 These flag are passed to the git apply program (see git-apply(1))
368 that applies the patch. Incompatible with the --interactive option.
369
370 --committer-date-is-author-date, --ignore-date
371 These flags are passed to git am to easily change the dates of the
372 rebased commits (see git-am(1)). Incompatible with the
373 --interactive option.
374
375 --signoff
376 Add a Signed-off-by: trailer to all the rebased commits. Note that
377 if --interactive is given then only commits marked to be picked,
378 edited or reworded will have the trailer added. Incompatible with
379 the --preserve-merges option.
380
381 -i, --interactive
382 Make a list of the commits which are about to be rebased. Let the
383 user edit that list before rebasing. This mode can also be used to
384 split commits (see SPLITTING COMMITS below).
385
386 The commit list format can be changed by setting the configuration
387 option rebase.instructionFormat. A customized instruction format
388 will automatically have the long commit hash prepended to the
389 format.
390
391 -r, --rebase-merges[=(rebase-cousins|no-rebase-cousins)]
392 By default, a rebase will simply drop merge commits from the todo
393 list, and put the rebased commits into a single, linear branch.
394 With --rebase-merges, the rebase will instead try to preserve the
395 branching structure within the commits that are to be rebased, by
396 recreating the merge commits. Any resolved merge conflicts or
397 manual amendments in these merge commits will have to be
398 resolved/re-applied manually.
399
400 By default, or when no-rebase-cousins was specified, commits which
401 do not have <upstream> as direct ancestor will keep their original
402 branch point, i.e. commits that would be excluded by
403 gitlink:git-log[1]'s --ancestry-path option will keep their
404 original ancestry by default. If the rebase-cousins mode is turned
405 on, such commits are instead rebased onto <upstream> (or <onto>, if
406 specified).
407
408 The --rebase-merges mode is similar in spirit to --preserve-merges,
409 but in contrast to that option works well in interactive rebases:
410 commits can be reordered, inserted and dropped at will.
411
412 It is currently only possible to recreate the merge commits using
413 the recursive merge strategy; Different merge strategies can be
414 used only via explicit exec git merge -s <strategy> [...]
415 commands.
416
417 See also REBASING MERGES below.
418
419 -p, --preserve-merges
420 Recreate merge commits instead of flattening the history by
421 replaying commits a merge commit introduces. Merge conflict
422 resolutions or manual amendments to merge commits are not
423 preserved.
424
425 This uses the --interactive machinery internally, but combining it
426 with the --interactive option explicitly is generally not a good
427 idea unless you know what you are doing (see BUGS below).
428
429 -x <cmd>, --exec <cmd>
430 Append "exec <cmd>" after each line creating a commit in the final
431 history. <cmd> will be interpreted as one or more shell commands.
432
433 You may execute several commands by either using one instance of
434 --exec with several commands:
435
436 git rebase -i --exec "cmd1 && cmd2 && ..."
437
438 or by giving more than one --exec:
439
440 git rebase -i --exec "cmd1" --exec "cmd2" --exec ...
441
442 If --autosquash is used, "exec" lines will not be appended for the
443 intermediate commits, and will only appear at the end of each
444 squash/fixup series.
445
446 This uses the --interactive machinery internally, but it can be run
447 without an explicit --interactive.
448
449 --root
450 Rebase all commits reachable from <branch>, instead of limiting
451 them with an <upstream>. This allows you to rebase the root
452 commit(s) on a branch. When used with --onto, it will skip changes
453 already contained in <newbase> (instead of <upstream>) whereas
454 without --onto it will operate on every change. When used together
455 with both --onto and --preserve-merges, all root commits will be
456 rewritten to have <newbase> as parent instead.
457
458 --autosquash, --no-autosquash
459 When the commit log message begins with "squash! ..." (or "fixup!
460 ..."), and there is already a commit in the todo list that matches
461 the same ..., automatically modify the todo list of rebase -i so
462 that the commit marked for squashing comes right after the commit
463 to be modified, and change the action of the moved commit from pick
464 to squash (or fixup). A commit matches the ... if the commit
465 subject matches, or if the ... refers to the commit’s hash. As a
466 fall-back, partial matches of the commit subject work, too. The
467 recommended way to create fixup/squash commits is by using the
468 --fixup/--squash options of git-commit(1).
469
470 This option is only valid when the --interactive option is used.
471
472 If the --autosquash option is enabled by default using the
473 configuration variable rebase.autoSquash, this option can be used
474 to override and disable this setting.
475
476 --autostash, --no-autostash
477 Automatically create a temporary stash entry before the operation
478 begins, and apply it after the operation ends. This means that you
479 can run rebase on a dirty worktree. However, use with care: the
480 final stash application after a successful rebase might result in
481 non-trivial conflicts.
482
483 --no-ff
484 With --interactive, cherry-pick all rebased commits instead of
485 fast-forwarding over the unchanged ones. This ensures that the
486 entire history of the rebased branch is composed of new commits.
487
488 Without --interactive, this is a synonym for --force-rebase.
489
490 You may find this helpful after reverting a topic branch merge, as
491 this option recreates the topic branch with fresh commits so it can
492 be remerged successfully without needing to "revert the reversion"
493 (see the revert-a-faulty-merge How-To[1] for details).
494
496 The merge mechanism (git merge and git pull commands) allows the
497 backend merge strategies to be chosen with -s option. Some strategies
498 can also take their own options, which can be passed by giving
499 -X<option> arguments to git merge and/or git pull.
500
501 resolve
502 This can only resolve two heads (i.e. the current branch and
503 another branch you pulled from) using a 3-way merge algorithm. It
504 tries to carefully detect criss-cross merge ambiguities and is
505 considered generally safe and fast.
506
507 recursive
508 This can only resolve two heads using a 3-way merge algorithm. When
509 there is more than one common ancestor that can be used for 3-way
510 merge, it creates a merged tree of the common ancestors and uses
511 that as the reference tree for the 3-way merge. This has been
512 reported to result in fewer merge conflicts without causing
513 mismerges by tests done on actual merge commits taken from Linux
514 2.6 kernel development history. Additionally this can detect and
515 handle merges involving renames, but currently cannot make use of
516 detected copies. This is the default merge strategy when pulling or
517 merging one branch.
518
519 The recursive strategy can take the following options:
520
521 ours
522 This option forces conflicting hunks to be auto-resolved
523 cleanly by favoring our version. Changes from the other tree
524 that do not conflict with our side are reflected to the merge
525 result. For a binary file, the entire contents are taken from
526 our side.
527
528 This should not be confused with the ours merge strategy, which
529 does not even look at what the other tree contains at all. It
530 discards everything the other tree did, declaring our history
531 contains all that happened in it.
532
533 theirs
534 This is the opposite of ours; note that, unlike ours, there is
535 no theirs merge strategy to confuse this merge option with.
536
537 patience
538 With this option, merge-recursive spends a little extra time to
539 avoid mismerges that sometimes occur due to unimportant
540 matching lines (e.g., braces from distinct functions). Use this
541 when the branches to be merged have diverged wildly. See also
542 git-diff(1) --patience.
543
544 diff-algorithm=[patience|minimal|histogram|myers]
545 Tells merge-recursive to use a different diff algorithm, which
546 can help avoid mismerges that occur due to unimportant matching
547 lines (such as braces from distinct functions). See also git-
548 diff(1) --diff-algorithm.
549
550 ignore-space-change, ignore-all-space, ignore-space-at-eol,
551 ignore-cr-at-eol
552 Treats lines with the indicated type of whitespace change as
553 unchanged for the sake of a three-way merge. Whitespace changes
554 mixed with other changes to a line are not ignored. See also
555 git-diff(1) -b, -w, --ignore-space-at-eol, and
556 --ignore-cr-at-eol.
557
558 · If their version only introduces whitespace changes to a
559 line, our version is used;
560
561 · If our version introduces whitespace changes but their
562 version includes a substantial change, their version is
563 used;
564
565 · Otherwise, the merge proceeds in the usual way.
566
567 renormalize
568 This runs a virtual check-out and check-in of all three stages
569 of a file when resolving a three-way merge. This option is
570 meant to be used when merging branches with different clean
571 filters or end-of-line normalization rules. See "Merging
572 branches with differing checkin/checkout attributes" in
573 gitattributes(5) for details.
574
575 no-renormalize
576 Disables the renormalize option. This overrides the
577 merge.renormalize configuration variable.
578
579 no-renames
580 Turn off rename detection. This overrides the merge.renames
581 configuration variable. See also git-diff(1) --no-renames.
582
583 find-renames[=<n>]
584 Turn on rename detection, optionally setting the similarity
585 threshold. This is the default. This overrides the
586 merge.renames configuration variable. See also git-diff(1)
587 --find-renames.
588
589 rename-threshold=<n>
590 Deprecated synonym for find-renames=<n>.
591
592 subtree[=<path>]
593 This option is a more advanced form of subtree strategy, where
594 the strategy makes a guess on how two trees must be shifted to
595 match with each other when merging. Instead, the specified path
596 is prefixed (or stripped from the beginning) to make the shape
597 of two trees to match.
598
599 octopus
600 This resolves cases with more than two heads, but refuses to do a
601 complex merge that needs manual resolution. It is primarily meant
602 to be used for bundling topic branch heads together. This is the
603 default merge strategy when pulling or merging more than one
604 branch.
605
606 ours
607 This resolves any number of heads, but the resulting tree of the
608 merge is always that of the current branch head, effectively
609 ignoring all changes from all other branches. It is meant to be
610 used to supersede old development history of side branches. Note
611 that this is different from the -Xours option to the recursive
612 merge strategy.
613
614 subtree
615 This is a modified recursive strategy. When merging trees A and B,
616 if B corresponds to a subtree of A, B is first adjusted to match
617 the tree structure of A, instead of reading the trees at the same
618 level. This adjustment is also done to the common ancestor tree.
619
620 With the strategies that use 3-way merge (including the default,
621 recursive), if a change is made on both branches, but later reverted on
622 one of the branches, that change will be present in the merged result;
623 some people find this behavior confusing. It occurs because only the
624 heads and the merge base are considered when performing a merge, not
625 the individual commits. The merge algorithm therefore considers the
626 reverted change as no change at all, and substitutes the changed
627 version instead.
628
630 You should understand the implications of using git rebase on a
631 repository that you share. See also RECOVERING FROM UPSTREAM REBASE
632 below.
633
634 When the git-rebase command is run, it will first execute a
635 "pre-rebase" hook if one exists. You can use this hook to do sanity
636 checks and reject the rebase if it isn’t appropriate. Please see the
637 template pre-rebase hook script for an example.
638
639 Upon completion, <branch> will be the current branch.
640
642 Rebasing interactively means that you have a chance to edit the commits
643 which are rebased. You can reorder the commits, and you can remove them
644 (weeding out bad or otherwise unwanted patches).
645
646 The interactive mode is meant for this type of workflow:
647
648 1. have a wonderful idea
649
650 2. hack on the code
651
652 3. prepare a series for submission
653
654 4. submit
655
656 where point 2. consists of several instances of
657
658 a) regular use
659
660 1. finish something worthy of a commit
661
662 2. commit
663
664 b) independent fixup
665
666 1. realize that something does not work
667
668 2. fix that
669
670 3. commit it
671
672 Sometimes the thing fixed in b.2. cannot be amended to the not-quite
673 perfect commit it fixes, because that commit is buried deeply in a
674 patch series. That is exactly what interactive rebase is for: use it
675 after plenty of "a"s and "b"s, by rearranging and editing commits, and
676 squashing multiple commits into one.
677
678 Start it with the last commit you want to retain as-is:
679
680 git rebase -i <after-this-commit>
681
682 An editor will be fired up with all the commits in your current branch
683 (ignoring merge commits), which come after the given commit. You can
684 reorder the commits in this list to your heart’s content, and you can
685 remove them. The list looks more or less like this:
686
687 pick deadbee The oneline of this commit
688 pick fa1afe1 The oneline of the next commit
689 ...
690
691
692 The oneline descriptions are purely for your pleasure; git rebase will
693 not look at them but at the commit names ("deadbee" and "fa1afe1" in
694 this example), so do not delete or edit the names.
695
696 By replacing the command "pick" with the command "edit", you can tell
697 git rebase to stop after applying that commit, so that you can edit the
698 files and/or the commit message, amend the commit, and continue
699 rebasing.
700
701 If you just want to edit the commit message for a commit, replace the
702 command "pick" with the command "reword".
703
704 To drop a commit, replace the command "pick" with "drop", or just
705 delete the matching line.
706
707 If you want to fold two or more commits into one, replace the command
708 "pick" for the second and subsequent commits with "squash" or "fixup".
709 If the commits had different authors, the folded commit will be
710 attributed to the author of the first commit. The suggested commit
711 message for the folded commit is the concatenation of the commit
712 messages of the first commit and of those with the "squash" command,
713 but omits the commit messages of commits with the "fixup" command.
714
715 git rebase will stop when "pick" has been replaced with "edit" or when
716 a command fails due to merge errors. When you are done editing and/or
717 resolving conflicts you can continue with git rebase --continue.
718
719 For example, if you want to reorder the last 5 commits, such that what
720 was HEAD~4 becomes the new HEAD. To achieve that, you would call git
721 rebase like this:
722
723 $ git rebase -i HEAD~5
724
725
726 And move the first patch to the end of the list.
727
728 You might want to preserve merges, if you have a history like this:
729
730 X
731 \
732 A---M---B
733 /
734 ---o---O---P---Q
735
736
737 Suppose you want to rebase the side branch starting at "A" to "Q". Make
738 sure that the current HEAD is "B", and call
739
740 $ git rebase -i -p --onto Q O
741
742
743 Reordering and editing commits usually creates untested intermediate
744 steps. You may want to check that your history editing did not break
745 anything by running a test, or at least recompiling at intermediate
746 points in history by using the "exec" command (shortcut "x"). You may
747 do so by creating a todo list like this one:
748
749 pick deadbee Implement feature XXX
750 fixup f1a5c00 Fix to feature XXX
751 exec make
752 pick c0ffeee The oneline of the next commit
753 edit deadbab The oneline of the commit after
754 exec cd subdir; make test
755 ...
756
757
758 The interactive rebase will stop when a command fails (i.e. exits with
759 non-0 status) to give you an opportunity to fix the problem. You can
760 continue with git rebase --continue.
761
762 The "exec" command launches the command in a shell (the one specified
763 in $SHELL, or the default shell if $SHELL is not set), so you can use
764 shell features (like "cd", ">", ";" ...). The command is run from the
765 root of the working tree.
766
767 $ git rebase -i --exec "make test"
768
769
770 This command lets you check that intermediate commits are compilable.
771 The todo list becomes like that:
772
773 pick 5928aea one
774 exec make test
775 pick 04d0fda two
776 exec make test
777 pick ba46169 three
778 exec make test
779 pick f4593f9 four
780 exec make test
781
782
784 In interactive mode, you can mark commits with the action "edit".
785 However, this does not necessarily mean that git rebase expects the
786 result of this edit to be exactly one commit. Indeed, you can undo the
787 commit, or you can add other commits. This can be used to split a
788 commit into two:
789
790 · Start an interactive rebase with git rebase -i <commit>^, where
791 <commit> is the commit you want to split. In fact, any commit range
792 will do, as long as it contains that commit.
793
794 · Mark the commit you want to split with the action "edit".
795
796 · When it comes to editing that commit, execute git reset HEAD^. The
797 effect is that the HEAD is rewound by one, and the index follows
798 suit. However, the working tree stays the same.
799
800 · Now add the changes to the index that you want to have in the first
801 commit. You can use git add (possibly interactively) or git gui (or
802 both) to do that.
803
804 · Commit the now-current index with whatever commit message is
805 appropriate now.
806
807 · Repeat the last two steps until your working tree is clean.
808
809 · Continue the rebase with git rebase --continue.
810
811 If you are not absolutely sure that the intermediate revisions are
812 consistent (they compile, pass the testsuite, etc.) you should use git
813 stash to stash away the not-yet-committed changes after each commit,
814 test, and amend the commit if fixes are necessary.
815
817 Rebasing (or any other form of rewriting) a branch that others have
818 based work on is a bad idea: anyone downstream of it is forced to
819 manually fix their history. This section explains how to do the fix
820 from the downstream’s point of view. The real fix, however, would be to
821 avoid rebasing the upstream in the first place.
822
823 To illustrate, suppose you are in a situation where someone develops a
824 subsystem branch, and you are working on a topic that is dependent on
825 this subsystem. You might end up with a history like the following:
826
827 o---o---o---o---o---o---o---o master
828 \
829 o---o---o---o---o subsystem
830 \
831 *---*---* topic
832
833
834 If subsystem is rebased against master, the following happens:
835
836 o---o---o---o---o---o---o---o master
837 \ \
838 o---o---o---o---o o'--o'--o'--o'--o' subsystem
839 \
840 *---*---* topic
841
842
843 If you now continue development as usual, and eventually merge topic to
844 subsystem, the commits from subsystem will remain duplicated forever:
845
846 o---o---o---o---o---o---o---o master
847 \ \
848 o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
849 \ /
850 *---*---*-..........-*--* topic
851
852
853 Such duplicates are generally frowned upon because they clutter up
854 history, making it harder to follow. To clean things up, you need to
855 transplant the commits on topic to the new subsystem tip, i.e., rebase
856 topic. This becomes a ripple effect: anyone downstream from topic is
857 forced to rebase too, and so on!
858
859 There are two kinds of fixes, discussed in the following subsections:
860
861 Easy case: The changes are literally the same.
862 This happens if the subsystem rebase was a simple rebase and had no
863 conflicts.
864
865 Hard case: The changes are not the same.
866 This happens if the subsystem rebase had conflicts, or used
867 --interactive to omit, edit, squash, or fixup commits; or if the
868 upstream used one of commit --amend, reset, or filter-branch.
869
870 The easy case
871 Only works if the changes (patch IDs based on the diff contents) on
872 subsystem are literally the same before and after the rebase subsystem
873 did.
874
875 In that case, the fix is easy because git rebase knows to skip changes
876 that are already present in the new upstream. So if you say (assuming
877 you’re on topic)
878
879 $ git rebase subsystem
880
881
882 you will end up with the fixed history
883
884 o---o---o---o---o---o---o---o master
885 \
886 o'--o'--o'--o'--o' subsystem
887 \
888 *---*---* topic
889
890
891 The hard case
892 Things get more complicated if the subsystem changes do not exactly
893 correspond to the ones before the rebase.
894
895 Note
896 While an "easy case recovery" sometimes appears to be successful
897 even in the hard case, it may have unintended consequences. For
898 example, a commit that was removed via git rebase --interactive
899 will be resurrected!
900
901 The idea is to manually tell git rebase "where the old subsystem ended
902 and your topic began", that is, what the old merge-base between them
903 was. You will have to find a way to name the last commit of the old
904 subsystem, for example:
905
906 · With the subsystem reflog: after git fetch, the old tip of
907 subsystem is at subsystem@{1}. Subsequent fetches will increase the
908 number. (See git-reflog(1).)
909
910 · Relative to the tip of topic: knowing that your topic has three
911 commits, the old tip of subsystem must be topic~3.
912
913 You can then transplant the old subsystem..topic to the new tip by
914 saying (for the reflog case, and assuming you are on topic already):
915
916 $ git rebase --onto subsystem subsystem@{1}
917
918
919 The ripple effect of a "hard case" recovery is especially bad: everyone
920 downstream from topic will now have to perform a "hard case" recovery
921 too!
922
924 The interactive rebase command was originally designed to handle
925 individual patch series. As such, it makes sense to exclude merge
926 commits from the todo list, as the developer may have merged the
927 then-current master while working on the branch, only to rebase all the
928 commits onto master eventually (skipping the merge commits).
929
930 However, there are legitimate reasons why a developer may want to
931 recreate merge commits: to keep the branch structure (or "commit
932 topology") when working on multiple, inter-related branches.
933
934 In the following example, the developer works on a topic branch that
935 refactors the way buttons are defined, and on another topic branch that
936 uses that refactoring to implement a "Report a bug" button. The output
937 of git log --graph --format=%s -5 may look like this:
938
939 * Merge branch 'report-a-bug'
940 |\
941 | * Add the feedback button
942 * | Merge branch 'refactor-button'
943 |\ \
944 | |/
945 | * Use the Button class for all buttons
946 | * Extract a generic Button class from the DownloadButton one
947
948
949 The developer might want to rebase those commits to a newer master
950 while keeping the branch topology, for example when the first topic
951 branch is expected to be integrated into master much earlier than the
952 second one, say, to resolve merge conflicts with changes to the
953 DownloadButton class that made it into master.
954
955 This rebase can be performed using the --rebase-merges option. It will
956 generate a todo list looking like this:
957
958 label onto
959
960 # Branch: refactor-button
961 reset onto
962 pick 123456 Extract a generic Button class from the DownloadButton one
963 pick 654321 Use the Button class for all buttons
964 label refactor-button
965
966 # Branch: report-a-bug
967 reset refactor-button # Use the Button class for all buttons
968 pick abcdef Add the feedback button
969 label report-a-bug
970
971 reset onto
972 merge -C a1b2c3 refactor-button # Merge 'refactor-button'
973 merge -C 6f5e4d report-a-bug # Merge 'report-a-bug'
974
975
976 In contrast to a regular interactive rebase, there are label, reset and
977 merge commands in addition to pick ones.
978
979 The label command associates a label with the current HEAD when that
980 command is executed. These labels are created as worktree-local refs
981 (refs/rewritten/<label>) that will be deleted when the rebase finishes.
982 That way, rebase operations in multiple worktrees linked to the same
983 repository do not interfere with one another. If the label command
984 fails, it is rescheduled immediately, with a helpful message how to
985 proceed.
986
987 The reset command resets the HEAD, index and worktree to the specified
988 revision. It is isimilar to an exec git reset --hard <label>, but
989 refuses to overwrite untracked files. If the reset command fails, it is
990 rescheduled immediately, with a helpful message how to edit the todo
991 list (this typically happens when a reset command was inserted into the
992 todo list manually and contains a typo).
993
994 The merge command will merge the specified revision into whatever is
995 HEAD at that time. With -C <original-commit>, the commit message of the
996 specified merge commit will be used. When the -C is changed to a
997 lower-case -c, the message will be opened in an editor after a
998 successful merge so that the user can edit the message.
999
1000 If a merge command fails for any reason other than merge conflicts
1001 (i.e. when the merge operation did not even start), it is rescheduled
1002 immediately.
1003
1004 At this time, the merge command will always use the recursive merge
1005 strategy, with no way to choose a different one. To work around this,
1006 an exec command can be used to call git merge explicitly, using the
1007 fact that the labels are worktree-local refs (the ref
1008 refs/rewritten/onto would correspond to the label onto, for example).
1009
1010 Note: the first command (label onto) labels the revision onto which the
1011 commits are rebased; The name onto is just a convention, as a nod to
1012 the --onto option.
1013
1014 It is also possible to introduce completely new merge commits from
1015 scratch by adding a command of the form merge <merge-head>. This form
1016 will generate a tentative commit message and always open an editor to
1017 let the user edit it. This can be useful e.g. when a topic branch turns
1018 out to address more than a single concern and wants to be split into
1019 two or even more topic branches. Consider this todo list:
1020
1021 pick 192837 Switch from GNU Makefiles to CMake
1022 pick 5a6c7e Document the switch to CMake
1023 pick 918273 Fix detection of OpenSSL in CMake
1024 pick afbecd http: add support for TLS v1.3
1025 pick fdbaec Fix detection of cURL in CMake on Windows
1026
1027
1028 The one commit in this list that is not related to CMake may very well
1029 have been motivated by working on fixing all those bugs introduced by
1030 switching to CMake, but it addresses a different concern. To split this
1031 branch into two topic branches, the todo list could be edited like
1032 this:
1033
1034 label onto
1035
1036 pick afbecd http: add support for TLS v1.3
1037 label tlsv1.3
1038
1039 reset onto
1040 pick 192837 Switch from GNU Makefiles to CMake
1041 pick 918273 Fix detection of OpenSSL in CMake
1042 pick fdbaec Fix detection of cURL in CMake on Windows
1043 pick 5a6c7e Document the switch to CMake
1044 label cmake
1045
1046 reset onto
1047 merge tlsv1.3
1048 merge cmake
1049
1050
1052 The todo list presented by --preserve-merges --interactive does not
1053 represent the topology of the revision graph. Editing commits and
1054 rewording their commit messages should work fine, but attempts to
1055 reorder commits tend to produce counterintuitive results. Use
1056 --rebase-merges in such scenarios instead.
1057
1058 For example, an attempt to rearrange
1059
1060 1 --- 2 --- 3 --- 4 --- 5
1061
1062
1063 to
1064
1065 1 --- 2 --- 4 --- 3 --- 5
1066
1067
1068 by moving the "pick 4" line will result in the following history:
1069
1070 3
1071 /
1072 1 --- 2 --- 4 --- 5
1073
1074
1076 Part of the git(1) suite
1077
1079 1. revert-a-faulty-merge How-To
1080 file:///usr/share/doc/git/howto/revert-a-faulty-merge.html
1081
1082
1083
1084Git 2.18.1 05/14/2019 GIT-REBASE(1)