blob: e8e75790fc21b403f428b8cca72ab42a072e1b38 [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 Hamano250f03e2007-09-10 01:33:2811'git-rebase' [-i | --interactive] [-v | --verbose] [-m | --merge]
12[-C<n>] [ --whitespace=<option>] [-p | --preserve-merges]
13[--onto <newbase>] <upstream> [<branch>]
Junio C Hamano6959c6c2006-05-17 10:34:1114'git-rebase' --continue | --skip | --abort
Junio C Hamano6112cad2006-05-02 07:28:0615
Junio C Hamano1a4e8412005-12-27 08:17:2316DESCRIPTION
17-----------
Junio C Hamano89d4e0f2007-02-18 00:34:5918If <branch> is specified, git-rebase will perform an automatic
19`git checkout <branch>` before doing anything else. Otherwise
20it remains on the current branch.
21
22All changes made by commits in the current branch but that are not
23in <upstream> are saved to a temporary area. This is the same set
24of commits that would be shown by `git log <upstream>..HEAD`.
25
26The current branch is reset to <upstream>, or <newbase> if the
27--onto option was supplied. This has the exact same effect as
28`git reset --hard <upstream>` (or <newbase>).
29
30The commits that were previously saved into the temporary area are
31then reapplied to the current branch, one by one, in order.
Junio C Hamano7e9f6b72006-02-22 10:44:5532
Junio C Hamano6112cad2006-05-02 07:28:0633It is possible that a merge failure will prevent this process from being
34completely automatic. You will have to resolve any such merge failure
Junio C Hamano6959c6c2006-05-17 10:34:1135and run `git rebase --continue`. Another option is to bypass the commit
36that caused the merge failure with `git rebase --skip`. To restore the
37original <branch> and remove the .dotest working files, use the command
38`git rebase --abort` instead.
Junio C Hamano6112cad2006-05-02 07:28:0639
Junio C Hamano7e9f6b72006-02-22 10:44:5540Assume the following history exists and the current branch is "topic":
41
Junio C Hamano6112cad2006-05-02 07:28:0642------------
Junio C Hamano7e9f6b72006-02-22 10:44:5543 A---B---C topic
44 /
45 D---E---F---G master
Junio C Hamano6112cad2006-05-02 07:28:0646------------
Junio C Hamano7e9f6b72006-02-22 10:44:5547
Junio C Hamano2b135272006-03-18 07:45:4248From this point, the result of either of the following commands:
Junio C Hamano7e9f6b72006-02-22 10:44:5549
Junio C Hamano6112cad2006-05-02 07:28:0650
Junio C Hamano7e9f6b72006-02-22 10:44:5551 git-rebase master
52 git-rebase master topic
53
54would be:
55
Junio C Hamano6112cad2006-05-02 07:28:0656------------
Junio C Hamano7e9f6b72006-02-22 10:44:5557 A'--B'--C' topic
58 /
59 D---E---F---G master
Junio C Hamano6112cad2006-05-02 07:28:0660------------
Junio C Hamano7e9f6b72006-02-22 10:44:5561
Junio C Hamanod8c9d432006-11-07 07:19:1362The latter form is just a short-hand of `git checkout topic`
63followed by `git rebase master`.
Junio C Hamano7e9f6b72006-02-22 10:44:5564
Junio C Hamanod8c9d432006-11-07 07:19:1365Here is how you would transplant a topic branch based on one
66branch to another, to pretend that you forked the topic branch
67from the latter branch, using `rebase --onto`.
Junio C Hamano7e9f6b72006-02-22 10:44:5568
Junio C Hamanod8c9d432006-11-07 07:19:1369First let's assume your 'topic' is based on branch 'next'.
70For example feature developed in 'topic' depends on some
71functionality which is found in 'next'.
Junio C Hamano7e9f6b72006-02-22 10:44:5572
Junio C Hamano6112cad2006-05-02 07:28:0673------------
Junio C Hamanod8c9d432006-11-07 07:19:1374 o---o---o---o---o master
75 \
76 o---o---o---o---o next
77 \
78 o---o---o topic
Junio C Hamano6112cad2006-05-02 07:28:0679------------
Junio C Hamano1a4e8412005-12-27 08:17:2380
Junio C Hamanod8c9d432006-11-07 07:19:1381We would want to make 'topic' forked from branch 'master',
82for example because the functionality 'topic' branch depend on
83got merged into more stable 'master' branch, like this:
84
85------------
86 o---o---o---o---o master
87 | \
88 | o'--o'--o' topic
89 \
90 o---o---o---o---o next
91------------
92
93We can get this using the following command:
94
95 git-rebase --onto master next topic
96
97
98Another example of --onto option is to rebase part of a
99branch. If we have the following situation:
100
101------------
102 H---I---J topicB
103 /
104 E---F---G topicA
105 /
106 A---B---C---D master
107------------
108
109then the command
110
111 git-rebase --onto master topicA topicB
112
113would result in:
114
115------------
116 H'--I'--J' topicB
117 /
118 | E---F---G topicA
119 |/
120 A---B---C---D master
121------------
122
123This is useful when topicB does not depend on topicA.
124
Junio C Hamano42f855f2007-02-06 00:09:38125A range of commits could also be removed with rebase. If we have
126the following situation:
127
128------------
129 E---F---G---H---I---J topicA
130------------
131
132then the command
133
Junio C Hamano3c1f2c72007-08-17 07:48:05134 git-rebase --onto topicA~5 topicA~3 topicA
Junio C Hamano42f855f2007-02-06 00:09:38135
136would result in the removal of commits F and G:
137
138------------
139 E---H'---I'---J' topicA
140------------
141
142This is useful if F and G were flawed in some way, or should not be
143part of topicA. Note that the argument to --onto and the <upstream>
144parameter can be any valid commit-ish.
145
Junio C Hamanof02e09f2006-03-27 07:51:03146In case of conflict, git-rebase will stop at the first problematic commit
Junio C Hamano6112cad2006-05-02 07:28:06147and leave conflict markers in the tree. You can use git diff to locate
148the markers (<<<<<<) and make edits to resolve the conflict. For each
149file you edit, you need to tell git that the conflict has been resolved,
150typically this would be done with
Junio C Hamanof02e09f2006-03-27 07:51:03151
Junio C Hamano6112cad2006-05-02 07:28:06152
Junio C Hamano89d4e0f2007-02-18 00:34:59153 git add <filename>
Junio C Hamano6112cad2006-05-02 07:28:06154
155
156After resolving the conflict manually and updating the index with the
157desired resolution, you can continue the rebasing process with
158
159
160 git rebase --continue
161
Junio C Hamanof02e09f2006-03-27 07:51:03162
163Alternatively, you can undo the git-rebase with
164
Junio C Hamano6112cad2006-05-02 07:28:06165
166 git rebase --abort
Junio C Hamanof02e09f2006-03-27 07:51:03167
Junio C Hamano1a4e8412005-12-27 08:17:23168OPTIONS
169-------
Junio C Hamano7e9f6b72006-02-22 10:44:55170<newbase>::
171Starting point at which to create the new commits. If the
172--onto option is not specified, the starting point is
Junio C Hamano42f855f2007-02-06 00:09:38173<upstream>. May be any valid commit, and not just an
174existing branch name.
Junio C Hamano7e9f6b72006-02-22 10:44:55175
Junio C Hamano1a4e8412005-12-27 08:17:23176<upstream>::
Junio C Hamano42f855f2007-02-06 00:09:38177Upstream branch to compare against. May be any valid commit,
178not just an existing branch name.
Junio C Hamano1a4e8412005-12-27 08:17:23179
Junio C Hamano2b135272006-03-18 07:45:42180<branch>::
Junio C Hamano1a4e8412005-12-27 08:17:23181Working branch; defaults to HEAD.
182
Junio C Hamano6112cad2006-05-02 07:28:06183--continue::
184Restart the rebasing process after having resolved a merge conflict.
185
186--abort::
187Restore the original branch and abort the rebase operation.
188
Junio C Hamano97f518c2006-06-22 19:49:35189--skip::
190Restart the rebasing process by skipping the current patch.
Junio C Hamano97f518c2006-06-22 19:49:35191
Junio C Hamanoc5653d72007-09-02 06:25:16192-m, \--merge::
Junio C Hamano97f518c2006-06-22 19:49:35193Use merging strategies to rebase. When the recursive (default) merge
194strategy is used, this allows rebase to be aware of renames on the
195upstream side.
196
197-s <strategy>, \--strategy=<strategy>::
198Use the given merge strategy; can be supplied more than
199once to specify them in the order they should be tried.
200If there is no `-s` option, a built-in list of strategies
201is used instead (`git-merge-recursive` when merging a single
202head, `git-merge-octopus` otherwise). This implies --merge.
203
Junio C Hamanofbe00522006-10-19 05:58:48204-v, \--verbose::
205Display a diffstat of what changed upstream since the last rebase.
206
Junio C Hamanod3339982007-02-09 08:38:48207-C<n>::
208Ensure at least <n> lines of surrounding context match before
209and after each change. When fewer lines of surrounding
210context exist they all must match. By default no context is
211ever ignored.
212
Junio C Hamano250f03e2007-09-10 01:33:28213--whitespace=<nowarn|warn|error|error-all|strip>::
214This flag is passed to the `git-apply` program
215(see gitlink:git-apply[1]) that applies the patch.
216
Junio C Hamano1d90cb02007-07-03 07:05:31217-i, \--interactive::
218Make a list of the commits which are about to be rebased. Let the
Junio C Hamanodbb64592007-09-01 11:17:39219user edit that list before rebasing. This mode can also be used to
220split commits (see SPLITTING COMMITS below).
Junio C Hamano1d90cb02007-07-03 07:05:31221
222-p, \--preserve-merges::
223Instead of ignoring merges, try to recreate them. This option
224only works in interactive mode.
225
Junio C Hamano97f518c2006-06-22 19:49:35226include::merge-strategies.txt[]
227
Junio C Hamano6112cad2006-05-02 07:28:06228NOTES
229-----
230When you rebase a branch, you are changing its history in a way that
231will cause problems for anyone who already has a copy of the branch
232in their repository and tries to pull updates from you. You should
233understand the implications of using 'git rebase' on a repository that
234you share.
235
236When the git rebase command is run, it will first execute a "pre-rebase"
237hook if one exists. You can use this hook to do sanity checks and
238reject the rebase if it isn't appropriate. Please see the template
239pre-rebase hook script for an example.
240
241You must be in the top directory of your project to start (or continue)
242a rebase. Upon completion, <branch> will be the current branch.
243
Junio C Hamano1d90cb02007-07-03 07:05:31244INTERACTIVE MODE
245----------------
246
247Rebasing interactively means that you have a chance to edit the commits
248which are rebased. You can reorder the commits, and you can
249remove them (weeding out bad or otherwise unwanted patches).
250
251The interactive mode is meant for this type of workflow:
252
2531. have a wonderful idea
2542. hack on the code
2553. prepare a series for submission
2564. submit
257
258where point 2. consists of several instances of
259
260a. regular use
261 1. finish something worthy of a commit
262 2. commit
263b. independent fixup
264 1. realize that something does not work
265 2. fix that
266 3. commit it
267
268Sometimes the thing fixed in b.2. cannot be amended to the not-quite
269perfect commit it fixes, because that commit is buried deeply in a
270patch series. That is exactly what interactive rebase is for: use it
271after plenty of "a"s and "b"s, by rearranging and editing
272commits, and squashing multiple commits into one.
273
274Start it with the last commit you want to retain as-is:
275
276git rebase -i <after-this-commit>
277
278An editor will be fired up with all the commits in your current branch
279(ignoring merge commits), which come after the given commit. You can
280reorder the commits in this list to your heart's content, and you can
281remove them. The list looks more or less like this:
282
283-------------------------------------------
284pick deadbee The oneline of this commit
285pick fa1afe1 The oneline of the next commit
286...
287-------------------------------------------
288
289The oneline descriptions are purely for your pleasure; `git-rebase` will
290not look at them but at the commit names ("deadbee" and "fa1afe1" in this
291example), so do not delete or edit the names.
292
293By replacing the command "pick" with the command "edit", you can tell
294`git-rebase` to stop after applying that commit, so that you can edit
295the files and/or the commit message, amend the commit, and continue
296rebasing.
297
298If you want to fold two or more commits into one, replace the command
299"pick" with "squash" for the second and subsequent commit. If the
300commits had different authors, it will attribute the squashed commit to
Junio C Hamanod41d6d02007-09-30 08:11:15301the author of the first commit.
Junio C Hamano1d90cb02007-07-03 07:05:31302
303In both cases, or when a "pick" does not succeed (because of merge
304errors), the loop will stop to let you fix things, and you can continue
305the loop with `git rebase --continue`.
306
307For example, if you want to reorder the last 5 commits, such that what
308was HEAD~4 becomes the new HEAD. To achieve that, you would call
309`git-rebase` like this:
310
311----------------------
312$ git rebase -i HEAD~5
313----------------------
314
315And move the first patch to the end of the list.
316
317You might want to preserve merges, if you have a history like this:
318
319------------------
320 X
321 \
322 A---M---B
323 /
324---o---O---P---Q
325------------------
326
327Suppose you want to rebase the side branch starting at "A" to "Q". Make
328sure that the current HEAD is "B", and call
329
330-----------------------------
331$ git rebase -i -p --onto Q O
332-----------------------------
333
Junio C Hamanodbb64592007-09-01 11:17:39334
335SPLITTING COMMITS
336-----------------
337
338In interactive mode, you can mark commits with the action "edit". However,
339this does not necessarily mean that 'git rebase' expects the result of this
340edit to be exactly one commit. Indeed, you can undo the commit, or you can
341add other commits. This can be used to split a commit into two:
342
343- Start an interactive rebase with 'git rebase -i <commit>^', where
344 <commit> is the commit you want to split. In fact, any commit range
345 will do, as long as it contains that commit.
346
347- Mark the commit you want to split with the action "edit".
348
349- When it comes to editing that commit, execute 'git reset HEAD^'. The
350 effect is that the HEAD is rewound by one, and the index follows suit.
351 However, the working tree stays the same.
352
353- Now add the changes to the index that you want to have in the first
354 commit. You can use gitlink:git-add[1] (possibly interactively) and/or
355 gitlink:git-gui[1] to do that.
356
357- Commit the now-current index with whatever commit message is appropriate
358 now.
359
360- Repeat the last two steps until your working tree is clean.
361
362- Continue the rebase with 'git rebase --continue'.
363
364If you are not absolutely sure that the intermediate revisions are
365consistent (they compile, pass the testsuite, etc.) you should use
366gitlink:git-stash[1] to stash away the not-yet-committed changes
367after each commit, test, and amend the commit if fixes are necessary.
368
369
Junio C Hamano1d90cb02007-07-03 07:05:31370Authors
Junio C Hamano1a4e8412005-12-27 08:17:23371------
Junio C Hamano1d90cb02007-07-03 07:05:31372Written by Junio C Hamano <junkio@cox.net> and
373Johannes E. Schindelin <johannes.schindelin@gmx.de>
Junio C Hamano1a4e8412005-12-27 08:17:23374
375Documentation
376--------------
377Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
378
379GIT
380---
381Part of the gitlink:git[7] suite