blob: fd535b06abf85ae166413ad0d24226240f8dd066 [file] [log] [blame]
Junio C Hamano1a4e8412005-12-27 08:17:231git-rebase(1)
2=============
3
4NAME
5----
Junio C Hamano7c73c662007-01-19 00:37:506git-rebase - Forward-port local commits to the updated upstream head
Junio C Hamano1a4e8412005-12-27 08:17:237
8SYNOPSIS
9--------
Junio C Hamanoa9b8d242007-05-19 04:51:5510[verse]
Junio C Hamano644936c2012-06-28 23:05:1411'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
Junio C Hamanob02377c2011-04-28 22:26:0212[<upstream>] [<branch>]
Junio C Hamano02482692012-07-16 05:28:3913'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
Junio C Hamanobd53dbf2009-01-18 18:26:3714--root [<branch>]
Junio C Hamanofce7c7e2008-07-02 03:06:3815'git rebase' --continue | --skip | --abort
Junio C Hamano6112cad2006-05-02 07:28:0616
Junio C Hamano1a4e8412005-12-27 08:17:2317DESCRIPTION
18-----------
Junio C Hamano1aa40d22010-01-21 17:46:4319If <branch> is specified, 'git rebase' will perform an automatic
Junio C Hamano89d4e0f2007-02-18 00:34:5920`git checkout <branch>` before doing anything else. Otherwise
21it remains on the current branch.
22
Junio C Hamanob02377c2011-04-28 22:26:0223If <upstream> is not specified, the upstream configured in
24branch.<name>.remote and branch.<name>.merge options will be used; see
25linkgit:git-config[1] for details. If you are currently not on any
26branch or if the current branch does not have a configured upstream,
27the rebase will abort.
28
Junio C Hamano89d4e0f2007-02-18 00:34:5929All changes made by commits in the current branch but that are not
30in <upstream> are saved to a temporary area. This is the same set
Junio C Hamanobd53dbf2009-01-18 18:26:3731of commits that would be shown by `git log <upstream>..HEAD` (or
32`git log HEAD`, if --root is specified).
Junio C Hamano89d4e0f2007-02-18 00:34:5933
34The current branch is reset to <upstream>, or <newbase> if the
35--onto option was supplied. This has the exact same effect as
Junio C Hamano38ddcce2008-07-15 15:49:0336`git reset --hard <upstream>` (or <newbase>). ORIG_HEAD is set
37to point at the tip of the branch before the reset.
Junio C Hamano89d4e0f2007-02-18 00:34:5938
39The commits that were previously saved into the temporary area are
Junio C Hamano764a6672007-10-23 01:23:3140then reapplied to the current branch, one by one, in order. Note that
41any commits in HEAD which introduce the same textual changes as a commit
42in HEAD..<upstream> are omitted (i.e., a patch already accepted upstream
43with a different commit message or timestamp will be skipped).
Junio C Hamano7e9f6b72006-02-22 10:44:5544
Junio C Hamano6112cad2006-05-02 07:28:0645It is possible that a merge failure will prevent this process from being
46completely automatic. You will have to resolve any such merge failure
Junio C Hamano6959c6c2006-05-17 10:34:1147and run `git rebase --continue`. Another option is to bypass the commit
Junio C Hamano15567bc2011-07-23 00:51:5948that caused the merge failure with `git rebase --skip`. To check out the
Junio C Hamano0868a302008-07-22 09:20:4449original <branch> and remove the .git/rebase-apply working files, use the
50command `git rebase --abort` instead.
Junio C Hamano6112cad2006-05-02 07:28:0651
Junio C Hamano7e9f6b72006-02-22 10:44:5552Assume the following history exists and the current branch is "topic":
53
Junio C Hamano6112cad2006-05-02 07:28:0654------------
Junio C Hamano7e9f6b72006-02-22 10:44:5555 A---B---C topic
56 /
57 D---E---F---G master
Junio C Hamano6112cad2006-05-02 07:28:0658------------
Junio C Hamano7e9f6b72006-02-22 10:44:5559
Junio C Hamano2b135272006-03-18 07:45:4260From this point, the result of either of the following commands:
Junio C Hamano7e9f6b72006-02-22 10:44:5561
Junio C Hamano6112cad2006-05-02 07:28:0662
Junio C Hamanofce7c7e2008-07-02 03:06:3863 git rebase master
64 git rebase master topic
Junio C Hamano7e9f6b72006-02-22 10:44:5565
66would be:
67
Junio C Hamano6112cad2006-05-02 07:28:0668------------
Junio C Hamano7e9f6b72006-02-22 10:44:5569 A'--B'--C' topic
70 /
71 D---E---F---G master
Junio C Hamano6112cad2006-05-02 07:28:0672------------
Junio C Hamano7e9f6b72006-02-22 10:44:5573
Junio C Hamano92faa802011-03-15 01:00:1874*NOTE:* The latter form is just a short-hand of `git checkout topic`
75followed by `git rebase master`. When rebase exits `topic` will
76remain the checked-out branch.
Junio C Hamano7e9f6b72006-02-22 10:44:5577
Junio C Hamano764a6672007-10-23 01:23:3178If the upstream branch already contains a change you have made (e.g.,
79because you mailed a patch which was applied upstream), then that commit
Junio C Hamanofce7c7e2008-07-02 03:06:3880will be skipped. For example, running `git rebase master` on the
Junio C Hamano764a6672007-10-23 01:23:3181following history (in which A' and A introduce the same set of changes,
82but have different committer information):
83
84------------
85 A---B---C topic
86 /
87 D---E---A'---F master
88------------
89
90will result in:
91
92------------
93 B'---C' topic
94 /
95 D---E---A'---F master
96------------
97
Junio C Hamanod8c9d432006-11-07 07:19:1398Here is how you would transplant a topic branch based on one
99branch to another, to pretend that you forked the topic branch
100from the latter branch, using `rebase --onto`.
Junio C Hamano7e9f6b72006-02-22 10:44:55101
Junio C Hamanod8c9d432006-11-07 07:19:13102First let's assume your 'topic' is based on branch 'next'.
Junio C Hamanoa476efa2008-10-10 15:31:42103For example, a feature developed in 'topic' depends on some
Junio C Hamanod8c9d432006-11-07 07:19:13104functionality which is found in 'next'.
Junio C Hamano7e9f6b72006-02-22 10:44:55105
Junio C Hamano6112cad2006-05-02 07:28:06106------------
Junio C Hamanod8c9d432006-11-07 07:19:13107 o---o---o---o---o master
108 \
109 o---o---o---o---o next
110 \
111 o---o---o topic
Junio C Hamano6112cad2006-05-02 07:28:06112------------
Junio C Hamano1a4e8412005-12-27 08:17:23113
Junio C Hamanoa476efa2008-10-10 15:31:42114We want to make 'topic' forked from branch 'master'; for example,
115because the functionality on which 'topic' depends was merged into the
116more stable 'master' branch. We want our tree to look like this:
Junio C Hamanod8c9d432006-11-07 07:19:13117
118------------
119 o---o---o---o---o master
120 | \
121 | o'--o'--o' topic
122 \
123 o---o---o---o---o next
124------------
125
126We can get this using the following command:
127
Junio C Hamanofce7c7e2008-07-02 03:06:38128 git rebase --onto master next topic
Junio C Hamanod8c9d432006-11-07 07:19:13129
130
131Another example of --onto option is to rebase part of a
132branch. If we have the following situation:
133
134------------
135 H---I---J topicB
136 /
137 E---F---G topicA
138 /
139 A---B---C---D master
140------------
141
142then the command
143
Junio C Hamanofce7c7e2008-07-02 03:06:38144 git rebase --onto master topicA topicB
Junio C Hamanod8c9d432006-11-07 07:19:13145
146would result in:
147
148------------
149 H'--I'--J' topicB
150 /
151 | E---F---G topicA
152 |/
153 A---B---C---D master
154------------
155
156This is useful when topicB does not depend on topicA.
157
Junio C Hamano42f855f2007-02-06 00:09:38158A range of commits could also be removed with rebase. If we have
159the following situation:
160
161------------
162 E---F---G---H---I---J topicA
163------------
164
165then the command
166
Junio C Hamanofce7c7e2008-07-02 03:06:38167 git rebase --onto topicA~5 topicA~3 topicA
Junio C Hamano42f855f2007-02-06 00:09:38168
169would result in the removal of commits F and G:
170
171------------
172 E---H'---I'---J' topicA
173------------
174
175This is useful if F and G were flawed in some way, or should not be
176part of topicA. Note that the argument to --onto and the <upstream>
177parameter can be any valid commit-ish.
178
Junio C Hamano1aa40d22010-01-21 17:46:43179In case of conflict, 'git rebase' will stop at the first problematic commit
180and leave conflict markers in the tree. You can use 'git diff' to locate
Junio C Hamano6112cad2006-05-02 07:28:06181the markers (<<<<<<) and make edits to resolve the conflict. For each
182file you edit, you need to tell git that the conflict has been resolved,
183typically this would be done with
Junio C Hamanof02e09f2006-03-27 07:51:03184
Junio C Hamano6112cad2006-05-02 07:28:06185
Junio C Hamano89d4e0f2007-02-18 00:34:59186 git add <filename>
Junio C Hamano6112cad2006-05-02 07:28:06187
188
189After resolving the conflict manually and updating the index with the
190desired resolution, you can continue the rebasing process with
191
192
193 git rebase --continue
194
Junio C Hamanof02e09f2006-03-27 07:51:03195
Junio C Hamano1aa40d22010-01-21 17:46:43196Alternatively, you can undo the 'git rebase' with
Junio C Hamanof02e09f2006-03-27 07:51:03197
Junio C Hamano6112cad2006-05-02 07:28:06198
199 git rebase --abort
Junio C Hamanof02e09f2006-03-27 07:51:03200
Junio C Hamanoea6a7642009-03-11 23:56:19201CONFIGURATION
202-------------
203
204rebase.stat::
205Whether to show a diffstat of what changed upstream since the last
206rebase. False by default.
207
Junio C Hamano075ae872010-09-01 18:43:07208rebase.autosquash::
209If set to true enable '--autosquash' option by default.
210
Junio C Hamano1a4e8412005-12-27 08:17:23211OPTIONS
212-------
Junio C Hamano644936c2012-06-28 23:05:14213--onto <newbase>::
Junio C Hamano7e9f6b72006-02-22 10:44:55214Starting point at which to create the new commits. If the
215--onto option is not specified, the starting point is
Junio C Hamano42f855f2007-02-06 00:09:38216<upstream>. May be any valid commit, and not just an
217existing branch name.
Junio C Hamanoe32ec8b2010-06-02 23:32:31218+
Junio C Hamano2db3e752010-09-03 21:33:06219As a special case, you may use "A\...B" as a shortcut for the
Junio C Hamanoe32ec8b2010-06-02 23:32:31220merge base of A and B if there is exactly one merge base. You can
221leave out at most one of A and B, in which case it defaults to HEAD.
Junio C Hamano7e9f6b72006-02-22 10:44:55222
Junio C Hamano1a4e8412005-12-27 08:17:23223<upstream>::
Junio C Hamano42f855f2007-02-06 00:09:38224Upstream branch to compare against. May be any valid commit,
Junio C Hamanob02377c2011-04-28 22:26:02225not just an existing branch name. Defaults to the configured
226upstream for the current branch.
Junio C Hamano1a4e8412005-12-27 08:17:23227
Junio C Hamano2b135272006-03-18 07:45:42228<branch>::
Junio C Hamano1a4e8412005-12-27 08:17:23229Working branch; defaults to HEAD.
230
Junio C Hamano6112cad2006-05-02 07:28:06231--continue::
232Restart the rebasing process after having resolved a merge conflict.
233
234--abort::
Junio C Hamano15567bc2011-07-23 00:51:59235Abort the rebase operation and reset HEAD to the original
236branch. If <branch> was provided when the rebase operation was
237started, then HEAD will be reset to <branch>. Otherwise HEAD
238will be reset to where it was when the rebase operation was
239started.
Junio C Hamano6112cad2006-05-02 07:28:06240
Junio C Hamano37e389e2012-04-30 22:36:09241--keep-empty::
242Keep the commits that do not change anything from its
243parents in the result.
244
Junio C Hamano97f518c2006-06-22 19:49:35245--skip::
246Restart the rebasing process by skipping the current patch.
Junio C Hamano97f518c2006-06-22 19:49:35247
Junio C Hamanoeb415992008-06-08 22:49:47248-m::
249--merge::
Junio C Hamano97f518c2006-06-22 19:49:35250Use merging strategies to rebase. When the recursive (default) merge
251strategy is used, this allows rebase to be aware of renames on the
252upstream side.
Junio C Hamanobf984de2009-11-23 06:11:19253+
254Note that a rebase merge works by replaying each commit from the working
255branch on top of the <upstream> branch. Because of this, when a merge
256conflict happens, the side reported as 'ours' is the so-far rebased
257series, starting with <upstream>, and 'theirs' is the working branch. In
258other words, the sides are swapped.
Junio C Hamano97f518c2006-06-22 19:49:35259
Junio C Hamanoeb415992008-06-08 22:49:47260-s <strategy>::
261--strategy=<strategy>::
Junio C Hamano52d5def2009-05-21 16:27:43262Use the given merge strategy.
Junio C Hamano1aa40d22010-01-21 17:46:43263If there is no `-s` option 'git merge-recursive' is used
Junio C Hamanobf984de2009-11-23 06:11:19264instead. This implies --merge.
265+
Junio C Hamano1aa40d22010-01-21 17:46:43266Because 'git rebase' replays each commit from the working branch
Junio C Hamanobf984de2009-11-23 06:11:19267on top of the <upstream> branch using the given strategy, using
268the 'ours' strategy simply discards all patches from the <branch>,
269which makes little sense.
Junio C Hamano97f518c2006-06-22 19:49:35270
Junio C Hamano619596a2010-08-18 22:15:35271-X <strategy-option>::
272--strategy-option=<strategy-option>::
273Pass the <strategy-option> through to the merge strategy.
Junio C Hamanob76a6862012-05-02 22:02:46274This implies `--merge` and, if no strategy has been
Junio C Hamano619596a2010-08-18 22:15:35275specified, `-s recursive`. Note the reversal of 'ours' and
Junio C Hamano44dcd492012-07-24 04:35:38276'theirs' as noted above for the `-m` option.
Junio C Hamano619596a2010-08-18 22:15:35277
Junio C Hamano2c14c8d2009-07-02 03:17:00278-q::
279--quiet::
280Be quiet. Implies --no-stat.
281
Junio C Hamanoeb415992008-06-08 22:49:47282-v::
283--verbose::
Junio C Hamanoea6a7642009-03-11 23:56:19284Be verbose. Implies --stat.
285
286--stat::
287Show a diffstat of what changed upstream since the last rebase. The
288diffstat is also controlled by the configuration option rebase.stat.
289
290-n::
291--no-stat::
292Do not show a diffstat as part of the rebase process.
Junio C Hamanofbe00522006-10-19 05:58:48293
Junio C Hamano7d06a8a2008-10-20 05:42:33294--no-verify::
295This option bypasses the pre-rebase hook. See also linkgit:githooks[5].
296
Junio C Hamanoeef01fe2010-12-13 08:31:58297--verify::
298Allows the pre-rebase hook to run, which is the default. This option can
299be used to override --no-verify. See also linkgit:githooks[5].
300
Junio C Hamanod3339982007-02-09 08:38:48301-C<n>::
302Ensure at least <n> lines of surrounding context match before
303and after each change. When fewer lines of surrounding
304context exist they all must match. By default no context is
305ever ignored.
306
Junio C Hamanoa973f1c2009-03-19 17:47:52307-f::
308--force-rebase::
309Force the rebase even if the current branch is a descendant
Junio C Hamanoef8fbf92010-04-04 19:12:02310of the commit you are rebasing onto. Normally non-interactive rebase will
Junio C Hamanoa973f1c2009-03-19 17:47:52311exit with the message "Current branch is up to date" in such a
312situation.
Junio C Hamanoef8fbf92010-04-04 19:12:02313Incompatible with the --interactive option.
314+
315You may find this (or --no-ff with an interactive rebase) helpful after
316reverting a topic branch merge, as this option recreates the topic branch with
317fresh commits so it can be remerged successfully without needing to "revert
318the reversion" (see the
319link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
Junio C Hamanoa973f1c2009-03-19 17:47:52320
Junio C Hamanofe24db02009-08-22 05:10:47321--ignore-whitespace::
Junio C Hamanof8a79222009-03-01 08:02:50322--whitespace=<option>::
Junio C Hamano1aa40d22010-01-21 17:46:43323These flag are passed to the 'git apply' program
Junio C Hamano35738e82008-01-07 07:55:46324(see linkgit:git-apply[1]) that applies the patch.
Junio C Hamanoec3b9a72009-02-13 08:45:52325Incompatible with the --interactive option.
Junio C Hamano250f03e2007-09-10 01:33:28326
Junio C Hamanoa973f1c2009-03-19 17:47:52327--committer-date-is-author-date::
328--ignore-date::
Junio C Hamano1aa40d22010-01-21 17:46:43329These flags are passed to 'git am' to easily change the dates
Junio C Hamanoa973f1c2009-03-19 17:47:52330of the rebased commits (see linkgit:git-am[1]).
Junio C Hamanoe97d5772010-05-19 08:13:58331Incompatible with the --interactive option.
Junio C Hamanoa973f1c2009-03-19 17:47:52332
Junio C Hamanoeb415992008-06-08 22:49:47333-i::
334--interactive::
Junio C Hamano1d90cb02007-07-03 07:05:31335Make a list of the commits which are about to be rebased. Let the
Junio C Hamanodbb64592007-09-01 11:17:39336user edit that list before rebasing. This mode can also be used to
337split commits (see SPLITTING COMMITS below).
Junio C Hamano1d90cb02007-07-03 07:05:31338
Junio C Hamanoeb415992008-06-08 22:49:47339-p::
340--preserve-merges::
Junio C Hamano7d06a8a2008-10-20 05:42:33341Instead of ignoring merges, try to recreate them.
Junio C Hamanobb88cf42010-06-21 15:23:55342+
343This uses the `--interactive` machinery internally, but combining it
344with the `--interactive` option explicitly is generally not a good
345idea unless you know what you are doing (see BUGS below).
346
Junio C Hamano644936c2012-06-28 23:05:14347-x <cmd>::
348--exec <cmd>::
349Append "exec <cmd>" after each line creating a commit in the
350final history. <cmd> will be interpreted as one or more shell
351commands.
352+
353This option can only be used with the `--interactive` option
354(see INTERACTIVE MODE below).
355+
356You may execute several commands by either using one instance of `--exec`
357with several commands:
358+
359git rebase -i --exec "cmd1 && cmd2 && ..."
360+
361or by giving more than one `--exec`:
362+
363git rebase -i --exec "cmd1" --exec "cmd2" --exec ...
364+
365If `--autosquash` is used, "exec" lines will not be appended for
366the intermediate commits, and will only appear at the end of each
367squash/fixup series.
Junio C Hamano1d90cb02007-07-03 07:05:31368
Junio C Hamanobd53dbf2009-01-18 18:26:37369--root::
370Rebase all commits reachable from <branch>, instead of
371limiting them with an <upstream>. This allows you to rebase
Junio C Hamano02482692012-07-16 05:28:39372the root commit(s) on a branch. When used with --onto, it
Junio C Hamanobd53dbf2009-01-18 18:26:37373will skip changes already contained in <newbase> (instead of
Junio C Hamano02482692012-07-16 05:28:39374<upstream>) whereas without --onto it will operate on every change.
375When used together with both --onto and --preserve-merges,
376'all' root commits will be rewritten to have <newbase> as parent
Junio C Hamanobd53dbf2009-01-18 18:26:37377instead.
378
Junio C Hamanoa9701f02010-01-21 00:42:16379--autosquash::
Junio C Hamano075ae872010-09-01 18:43:07380--no-autosquash::
Junio C Hamanoa9701f02010-01-21 00:42:16381When the commit log message begins with "squash! ..." (or
382"fixup! ..."), and there is a commit whose title begins with
383the same ..., automatically modify the todo list of rebase -i
Junio C Hamano364b6a42010-01-22 07:57:59384so that the commit marked for squashing comes right after the
Junio C Hamanoa9701f02010-01-21 00:42:16385commit to be modified, and change the action of the moved
386commit from `pick` to `squash` (or `fixup`).
387+
Junio C Hamanoef8fbf92010-04-04 19:12:02388This option is only valid when the '--interactive' option is used.
Junio C Hamano075ae872010-09-01 18:43:07389+
390If the '--autosquash' option is enabled by default using the
391configuration variable `rebase.autosquash`, this option can be
392used to override and disable this setting.
Junio C Hamanoef8fbf92010-04-04 19:12:02393
394--no-ff::
395With --interactive, cherry-pick all rebased commits instead of
396fast-forwarding over the unchanged ones. This ensures that the
397entire history of the rebased branch is composed of new commits.
398+
399Without --interactive, this is a synonym for --force-rebase.
400+
401You may find this helpful after reverting a topic branch merge, as this option
402recreates the topic branch with fresh commits so it can be remerged
403successfully without needing to "revert the reversion" (see the
404link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
Junio C Hamanoa9701f02010-01-21 00:42:16405
Junio C Hamano97f518c2006-06-22 19:49:35406include::merge-strategies.txt[]
407
Junio C Hamano6112cad2006-05-02 07:28:06408NOTES
409-----
Junio C Hamano7d06a8a2008-10-20 05:42:33410
Junio C Hamano1aa40d22010-01-21 17:46:43411You should understand the implications of using 'git rebase' on a
Junio C Hamano7d06a8a2008-10-20 05:42:33412repository that you share. See also RECOVERING FROM UPSTREAM REBASE
413below.
Junio C Hamano6112cad2006-05-02 07:28:06414
Junio C Hamanoba4b9282008-07-06 05:20:31415When the git-rebase command is run, it will first execute a "pre-rebase"
Junio C Hamano6112cad2006-05-02 07:28:06416hook if one exists. You can use this hook to do sanity checks and
417reject the rebase if it isn't appropriate. Please see the template
418pre-rebase hook script for an example.
419
Junio C Hamano0578b222008-03-11 22:50:03420Upon completion, <branch> will be the current branch.
Junio C Hamano6112cad2006-05-02 07:28:06421
Junio C Hamano1d90cb02007-07-03 07:05:31422INTERACTIVE MODE
423----------------
424
425Rebasing interactively means that you have a chance to edit the commits
426which are rebased. You can reorder the commits, and you can
427remove them (weeding out bad or otherwise unwanted patches).
428
429The interactive mode is meant for this type of workflow:
430
4311. have a wonderful idea
4322. hack on the code
4333. prepare a series for submission
4344. submit
435
436where point 2. consists of several instances of
437
Junio C Hamano0ff98162012-03-31 18:19:09438a) regular use
439
Junio C Hamano1d90cb02007-07-03 07:05:31440 1. finish something worthy of a commit
441 2. commit
Junio C Hamano0ff98162012-03-31 18:19:09442
443b) independent fixup
444
Junio C Hamano1d90cb02007-07-03 07:05:31445 1. realize that something does not work
446 2. fix that
447 3. commit it
448
449Sometimes the thing fixed in b.2. cannot be amended to the not-quite
450perfect commit it fixes, because that commit is buried deeply in a
451patch series. That is exactly what interactive rebase is for: use it
452after plenty of "a"s and "b"s, by rearranging and editing
453commits, and squashing multiple commits into one.
454
455Start it with the last commit you want to retain as-is:
456
457git rebase -i <after-this-commit>
458
459An editor will be fired up with all the commits in your current branch
460(ignoring merge commits), which come after the given commit. You can
461reorder the commits in this list to your heart's content, and you can
462remove them. The list looks more or less like this:
463
464-------------------------------------------
465pick deadbee The oneline of this commit
466pick fa1afe1 The oneline of the next commit
467...
468-------------------------------------------
469
Junio C Hamano1aa40d22010-01-21 17:46:43470The oneline descriptions are purely for your pleasure; 'git rebase' will
Junio C Hamano1d90cb02007-07-03 07:05:31471not look at them but at the commit names ("deadbee" and "fa1afe1" in this
472example), so do not delete or edit the names.
473
474By replacing the command "pick" with the command "edit", you can tell
Junio C Hamano1aa40d22010-01-21 17:46:43475'git rebase' to stop after applying that commit, so that you can edit
Junio C Hamano1d90cb02007-07-03 07:05:31476the files and/or the commit message, amend the commit, and continue
477rebasing.
478
Junio C Hamano3d23a0a2009-10-19 08:04:30479If you just want to edit the commit message for a commit, replace the
480command "pick" with the command "reword".
481
Junio C Hamano1d90cb02007-07-03 07:05:31482If you want to fold two or more commits into one, replace the command
Junio C Hamanoa9701f02010-01-21 00:42:16483"pick" for the second and subsequent commits with "squash" or "fixup".
484If the commits had different authors, the folded commit will be
485attributed to the author of the first commit. The suggested commit
486message for the folded commit is the concatenation of the commit
487messages of the first commit and of those with the "squash" command,
488but omits the commit messages of commits with the "fixup" command.
Junio C Hamano1d90cb02007-07-03 07:05:31489
Junio C Hamano1aa40d22010-01-21 17:46:43490'git rebase' will stop when "pick" has been replaced with "edit" or
Junio C Hamano3d23a0a2009-10-19 08:04:30491when a command fails due to merge errors. When you are done editing
492and/or resolving conflicts you can continue with `git rebase --continue`.
Junio C Hamano1d90cb02007-07-03 07:05:31493
494For example, if you want to reorder the last 5 commits, such that what
495was HEAD~4 becomes the new HEAD. To achieve that, you would call
Junio C Hamano1aa40d22010-01-21 17:46:43496'git rebase' like this:
Junio C Hamano1d90cb02007-07-03 07:05:31497
498----------------------
499$ git rebase -i HEAD~5
500----------------------
501
502And move the first patch to the end of the list.
503
504You might want to preserve merges, if you have a history like this:
505
506------------------
507 X
508 \
509 A---M---B
510 /
511---o---O---P---Q
512------------------
513
514Suppose you want to rebase the side branch starting at "A" to "Q". Make
515sure that the current HEAD is "B", and call
516
517-----------------------------
518$ git rebase -i -p --onto Q O
519-----------------------------
520
Junio C Hamano53ba6d02010-08-22 07:25:12521Reordering and editing commits usually creates untested intermediate
522steps. You may want to check that your history editing did not break
523anything by running a test, or at least recompiling at intermediate
524points in history by using the "exec" command (shortcut "x"). You may
525do so by creating a todo list like this one:
526
527-------------------------------------------
528pick deadbee Implement feature XXX
529fixup f1a5c00 Fix to feature XXX
530exec make
531pick c0ffeee The oneline of the next commit
532edit deadbab The oneline of the commit after
533exec cd subdir; make test
534...
535-------------------------------------------
536
537The interactive rebase will stop when a command fails (i.e. exits with
538non-0 status) to give you an opportunity to fix the problem. You can
539continue with `git rebase --continue`.
540
541The "exec" command launches the command in a shell (the one specified
542in `$SHELL`, or the default shell if `$SHELL` is not set), so you can
543use shell features (like "cd", ">", ";" ...). The command is run from
544the root of the working tree.
Junio C Hamanodbb64592007-09-01 11:17:39545
Junio C Hamano644936c2012-06-28 23:05:14546----------------------------------
547$ git rebase -i --exec "make test"
548----------------------------------
549
550This command lets you check that intermediate commits are compilable.
551The todo list becomes like that:
552
553--------------------
554pick 5928aea one
555exec make test
556pick 04d0fda two
557exec make test
558pick ba46169 three
559exec make test
560pick f4593f9 four
561exec make test
562--------------------
563
Junio C Hamanodbb64592007-09-01 11:17:39564SPLITTING COMMITS
565-----------------
566
567In interactive mode, you can mark commits with the action "edit". However,
Junio C Hamano1aa40d22010-01-21 17:46:43568this does not necessarily mean that 'git rebase' expects the result of this
Junio C Hamanodbb64592007-09-01 11:17:39569edit to be exactly one commit. Indeed, you can undo the commit, or you can
570add other commits. This can be used to split a commit into two:
571
Junio C Hamanofce7c7e2008-07-02 03:06:38572- Start an interactive rebase with `git rebase -i <commit>^`, where
Junio C Hamanodbb64592007-09-01 11:17:39573 <commit> is the commit you want to split. In fact, any commit range
574 will do, as long as it contains that commit.
575
576- Mark the commit you want to split with the action "edit".
577
Junio C Hamanofce7c7e2008-07-02 03:06:38578- When it comes to editing that commit, execute `git reset HEAD^`. The
Junio C Hamanodbb64592007-09-01 11:17:39579 effect is that the HEAD is rewound by one, and the index follows suit.
580 However, the working tree stays the same.
581
582- Now add the changes to the index that you want to have in the first
Junio C Hamanofce7c7e2008-07-02 03:06:38583 commit. You can use `git add` (possibly interactively) or
Junio C Hamano1aa40d22010-01-21 17:46:43584 'git gui' (or both) to do that.
Junio C Hamanodbb64592007-09-01 11:17:39585
586- Commit the now-current index with whatever commit message is appropriate
587 now.
588
589- Repeat the last two steps until your working tree is clean.
590
Junio C Hamanofce7c7e2008-07-02 03:06:38591- Continue the rebase with `git rebase --continue`.
Junio C Hamanodbb64592007-09-01 11:17:39592
593If you are not absolutely sure that the intermediate revisions are
594consistent (they compile, pass the testsuite, etc.) you should use
Junio C Hamano1aa40d22010-01-21 17:46:43595'git stash' to stash away the not-yet-committed changes
Junio C Hamanodbb64592007-09-01 11:17:39596after each commit, test, and amend the commit if fixes are necessary.
597
598
Junio C Hamano7d06a8a2008-10-20 05:42:33599RECOVERING FROM UPSTREAM REBASE
600-------------------------------
601
602Rebasing (or any other form of rewriting) a branch that others have
603based work on is a bad idea: anyone downstream of it is forced to
604manually fix their history. This section explains how to do the fix
605from the downstream's point of view. The real fix, however, would be
606to avoid rebasing the upstream in the first place.
607
608To illustrate, suppose you are in a situation where someone develops a
609'subsystem' branch, and you are working on a 'topic' that is dependent
610on this 'subsystem'. You might end up with a history like the
611following:
612
613------------
614 o---o---o---o---o---o---o---o---o master
615 \
616 o---o---o---o---o subsystem
617 \
618 *---*---* topic
619------------
620
621If 'subsystem' is rebased against 'master', the following happens:
622
623------------
624 o---o---o---o---o---o---o---o master
625 \ \
626 o---o---o---o---o o'--o'--o'--o'--o' subsystem
627 \
628 *---*---* topic
629------------
630
631If you now continue development as usual, and eventually merge 'topic'
632to 'subsystem', the commits from 'subsystem' will remain duplicated forever:
633
634------------
635 o---o---o---o---o---o---o---o master
636 \ \
637 o---o---o---o---o o'--o'--o'--o'--o'--M subsystem
638 \ /
639 *---*---*-..........-*--* topic
640------------
641
642Such duplicates are generally frowned upon because they clutter up
643history, making it harder to follow. To clean things up, you need to
644transplant the commits on 'topic' to the new 'subsystem' tip, i.e.,
645rebase 'topic'. This becomes a ripple effect: anyone downstream from
646'topic' is forced to rebase too, and so on!
647
648There are two kinds of fixes, discussed in the following subsections:
649
650Easy case: The changes are literally the same.::
651
652This happens if the 'subsystem' rebase was a simple rebase and
653had no conflicts.
654
655Hard case: The changes are not the same.::
656
657This happens if the 'subsystem' rebase had conflicts, or used
Junio C Hamanob76a6862012-05-02 22:02:46658`--interactive` to omit, edit, squash, or fixup commits; or
659if the upstream used one of `commit --amend`, `reset`, or
Junio C Hamano7d06a8a2008-10-20 05:42:33660`filter-branch`.
661
662
663The easy case
664~~~~~~~~~~~~~
665
666Only works if the changes (patch IDs based on the diff contents) on
667'subsystem' are literally the same before and after the rebase
668'subsystem' did.
669
Junio C Hamano1aa40d22010-01-21 17:46:43670In that case, the fix is easy because 'git rebase' knows to skip
Junio C Hamano7d06a8a2008-10-20 05:42:33671changes that are already present in the new upstream. So if you say
672(assuming you're on 'topic')
673------------
674 $ git rebase subsystem
675------------
676you will end up with the fixed history
677------------
678 o---o---o---o---o---o---o---o master
679 \
680 o'--o'--o'--o'--o' subsystem
681 \
682 *---*---* topic
683------------
684
685
686The hard case
687~~~~~~~~~~~~~
688
689Things get more complicated if the 'subsystem' changes do not exactly
690correspond to the ones before the rebase.
691
692NOTE: While an "easy case recovery" sometimes appears to be successful
693 even in the hard case, it may have unintended consequences. For
694 example, a commit that was removed via `git rebase
Junio C Hamanob76a6862012-05-02 22:02:46695 --interactive` will be **resurrected**!
Junio C Hamano7d06a8a2008-10-20 05:42:33696
Junio C Hamano1aa40d22010-01-21 17:46:43697The idea is to manually tell 'git rebase' "where the old 'subsystem'
Junio C Hamano7d06a8a2008-10-20 05:42:33698ended and your 'topic' began", that is, what the old merge-base
699between them was. You will have to find a way to name the last commit
700of the old 'subsystem', for example:
701
Junio C Hamano1aa40d22010-01-21 17:46:43702* With the 'subsystem' reflog: after 'git fetch', the old tip of
Junio C Hamanob76a6862012-05-02 22:02:46703 'subsystem' is at `subsystem@{1}`. Subsequent fetches will
Junio C Hamano7d06a8a2008-10-20 05:42:33704 increase the number. (See linkgit:git-reflog[1].)
705
706* Relative to the tip of 'topic': knowing that your 'topic' has three
707 commits, the old tip of 'subsystem' must be `topic~3`.
708
709You can then transplant the old `subsystem..topic` to the new tip by
710saying (for the reflog case, and assuming you are on 'topic' already):
711------------
712 $ git rebase --onto subsystem subsystem@{1}
713------------
714
715The ripple effect of a "hard case" recovery is especially bad:
716'everyone' downstream from 'topic' will now have to perform a "hard
717case" recovery too!
718
Junio C Hamanobb88cf42010-06-21 15:23:55719BUGS
720----
721The todo list presented by `--preserve-merges --interactive` does not
722represent the topology of the revision graph. Editing commits and
723rewording their commit messages should work fine, but attempts to
724reorder commits tend to produce counterintuitive results.
725
726For example, an attempt to rearrange
727------------
7281 --- 2 --- 3 --- 4 --- 5
729------------
730to
731------------
7321 --- 2 --- 4 --- 3 --- 5
733------------
734by moving the "pick 4" line will result in the following history:
735------------
7363
737 /
7381 --- 2 --- 4 --- 5
739------------
740
Junio C Hamano1a4e8412005-12-27 08:17:23741GIT
742---
Junio C Hamanof7c042d2008-06-06 22:50:53743Part of the linkgit:git[1] suite