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