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