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