Merge branch 'maint'
[git/git.git] / Documentation / git-rebase.txt
1 git-rebase(1)
2 =============
3
4 NAME
5 ----
6 git-rebase - Forward-port local commits to the updated upstream head
7
8 SYNOPSIS
9 --------
10 [verse]
11 'git-rebase' [-i | --interactive] [-v | --verbose] [-m | --merge]
12 [-C<n>] [ --whitespace=<option>] [-p | --preserve-merges]
13 [--onto <newbase>] <upstream> [<branch>]
14 'git-rebase' --continue | --skip | --abort
15
16 DESCRIPTION
17 -----------
18 If <branch> is specified, git-rebase will perform an automatic
19 `git checkout <branch>` before doing anything else. Otherwise
20 it remains on the current branch.
21
22 All changes made by commits in the current branch but that are not
23 in <upstream> are saved to a temporary area. This is the same set
24 of commits that would be shown by `git log <upstream>..HEAD`.
25
26 The 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
30 The commits that were previously saved into the temporary area are
31 then reapplied to the current branch, one by one, in order.
32
33 It is possible that a merge failure will prevent this process from being
34 completely automatic. You will have to resolve any such merge failure
35 and run `git rebase --continue`. Another option is to bypass the commit
36 that caused the merge failure with `git rebase --skip`. To restore the
37 original <branch> and remove the .dotest working files, use the command
38 `git rebase --abort` instead.
39
40 Assume the following history exists and the current branch is "topic":
41
42 ------------
43 A---B---C topic
44 /
45 D---E---F---G master
46 ------------
47
48 From this point, the result of either of the following commands:
49
50
51 git-rebase master
52 git-rebase master topic
53
54 would be:
55
56 ------------
57 A'--B'--C' topic
58 /
59 D---E---F---G master
60 ------------
61
62 The latter form is just a short-hand of `git checkout topic`
63 followed by `git rebase master`.
64
65 Here is how you would transplant a topic branch based on one
66 branch to another, to pretend that you forked the topic branch
67 from the latter branch, using `rebase --onto`.
68
69 First let's assume your 'topic' is based on branch 'next'.
70 For example feature developed in 'topic' depends on some
71 functionality which is found in 'next'.
72
73 ------------
74 o---o---o---o---o master
75 \
76 o---o---o---o---o next
77 \
78 o---o---o topic
79 ------------
80
81 We would want to make 'topic' forked from branch 'master',
82 for example because the functionality 'topic' branch depend on
83 got 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
93 We can get this using the following command:
94
95 git-rebase --onto master next topic
96
97
98 Another example of --onto option is to rebase part of a
99 branch. 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
109 then the command
110
111 git-rebase --onto master topicA topicB
112
113 would result in:
114
115 ------------
116 H'--I'--J' topicB
117 /
118 | E---F---G topicA
119 |/
120 A---B---C---D master
121 ------------
122
123 This is useful when topicB does not depend on topicA.
124
125 A range of commits could also be removed with rebase. If we have
126 the following situation:
127
128 ------------
129 E---F---G---H---I---J topicA
130 ------------
131
132 then the command
133
134 git-rebase --onto topicA~5 topicA~3 topicA
135
136 would result in the removal of commits F and G:
137
138 ------------
139 E---H'---I'---J' topicA
140 ------------
141
142 This is useful if F and G were flawed in some way, or should not be
143 part of topicA. Note that the argument to --onto and the <upstream>
144 parameter can be any valid commit-ish.
145
146 In case of conflict, git-rebase will stop at the first problematic commit
147 and leave conflict markers in the tree. You can use git diff to locate
148 the markers (<<<<<<) and make edits to resolve the conflict. For each
149 file you edit, you need to tell git that the conflict has been resolved,
150 typically this would be done with
151
152
153 git add <filename>
154
155
156 After resolving the conflict manually and updating the index with the
157 desired resolution, you can continue the rebasing process with
158
159
160 git rebase --continue
161
162
163 Alternatively, you can undo the git-rebase with
164
165
166 git rebase --abort
167
168 OPTIONS
169 -------
170 <newbase>::
171 Starting point at which to create the new commits. If the
172 --onto option is not specified, the starting point is
173 <upstream>. May be any valid commit, and not just an
174 existing branch name.
175
176 <upstream>::
177 Upstream branch to compare against. May be any valid commit,
178 not just an existing branch name.
179
180 <branch>::
181 Working branch; defaults to HEAD.
182
183 --continue::
184 Restart the rebasing process after having resolved a merge conflict.
185
186 --abort::
187 Restore the original branch and abort the rebase operation.
188
189 --skip::
190 Restart the rebasing process by skipping the current patch.
191
192 -m, \--merge::
193 Use merging strategies to rebase. When the recursive (default) merge
194 strategy is used, this allows rebase to be aware of renames on the
195 upstream side.
196
197 -s <strategy>, \--strategy=<strategy>::
198 Use the given merge strategy; can be supplied more than
199 once to specify them in the order they should be tried.
200 If there is no `-s` option, a built-in list of strategies
201 is used instead (`git-merge-recursive` when merging a single
202 head, `git-merge-octopus` otherwise). This implies --merge.
203
204 -v, \--verbose::
205 Display a diffstat of what changed upstream since the last rebase.
206
207 -C<n>::
208 Ensure at least <n> lines of surrounding context match before
209 and after each change. When fewer lines of surrounding
210 context exist they all must match. By default no context is
211 ever ignored.
212
213 --whitespace=<nowarn|warn|error|error-all|strip>::
214 This flag is passed to the `git-apply` program
215 (see gitlink:git-apply[1]) that applies the patch.
216
217 -i, \--interactive::
218 Make a list of the commits which are about to be rebased. Let the
219 user edit that list before rebasing. This mode can also be used to
220 split commits (see SPLITTING COMMITS below).
221
222 -p, \--preserve-merges::
223 Instead of ignoring merges, try to recreate them. This option
224 only works in interactive mode.
225
226 include::merge-strategies.txt[]
227
228 NOTES
229 -----
230 When you rebase a branch, you are changing its history in a way that
231 will cause problems for anyone who already has a copy of the branch
232 in their repository and tries to pull updates from you. You should
233 understand the implications of using 'git rebase' on a repository that
234 you share.
235
236 When the git rebase command is run, it will first execute a "pre-rebase"
237 hook if one exists. You can use this hook to do sanity checks and
238 reject the rebase if it isn't appropriate. Please see the template
239 pre-rebase hook script for an example.
240
241 You must be in the top directory of your project to start (or continue)
242 a rebase. Upon completion, <branch> will be the current branch.
243
244 INTERACTIVE MODE
245 ----------------
246
247 Rebasing interactively means that you have a chance to edit the commits
248 which are rebased. You can reorder the commits, and you can
249 remove them (weeding out bad or otherwise unwanted patches).
250
251 The interactive mode is meant for this type of workflow:
252
253 1. have a wonderful idea
254 2. hack on the code
255 3. prepare a series for submission
256 4. submit
257
258 where point 2. consists of several instances of
259
260 a. regular use
261 1. finish something worthy of a commit
262 2. commit
263 b. independent fixup
264 1. realize that something does not work
265 2. fix that
266 3. commit it
267
268 Sometimes the thing fixed in b.2. cannot be amended to the not-quite
269 perfect commit it fixes, because that commit is buried deeply in a
270 patch series. That is exactly what interactive rebase is for: use it
271 after plenty of "a"s and "b"s, by rearranging and editing
272 commits, and squashing multiple commits into one.
273
274 Start it with the last commit you want to retain as-is:
275
276 git rebase -i <after-this-commit>
277
278 An 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
280 reorder the commits in this list to your heart's content, and you can
281 remove them. The list looks more or less like this:
282
283 -------------------------------------------
284 pick deadbee The oneline of this commit
285 pick fa1afe1 The oneline of the next commit
286 ...
287 -------------------------------------------
288
289 The oneline descriptions are purely for your pleasure; `git-rebase` will
290 not look at them but at the commit names ("deadbee" and "fa1afe1" in this
291 example), so do not delete or edit the names.
292
293 By 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
295 the files and/or the commit message, amend the commit, and continue
296 rebasing.
297
298 If 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
300 commits had different authors, it will attribute the squashed commit to
301 the author of the first commit.
302
303 In both cases, or when a "pick" does not succeed (because of merge
304 errors), the loop will stop to let you fix things, and you can continue
305 the loop with `git rebase --continue`.
306
307 For example, if you want to reorder the last 5 commits, such that what
308 was 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
315 And move the first patch to the end of the list.
316
317 You 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
327 Suppose you want to rebase the side branch starting at "A" to "Q". Make
328 sure that the current HEAD is "B", and call
329
330 -----------------------------
331 $ git rebase -i -p --onto Q O
332 -----------------------------
333
334
335 SPLITTING COMMITS
336 -----------------
337
338 In interactive mode, you can mark commits with the action "edit". However,
339 this does not necessarily mean that 'git rebase' expects the result of this
340 edit to be exactly one commit. Indeed, you can undo the commit, or you can
341 add 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
364 If you are not absolutely sure that the intermediate revisions are
365 consistent (they compile, pass the testsuite, etc.) you should use
366 gitlink:git-stash[1] to stash away the not-yet-committed changes
367 after each commit, test, and amend the commit if fixes are necessary.
368
369
370 Authors
371 ------
372 Written by Junio C Hamano <junkio@cox.net> and
373 Johannes E. Schindelin <johannes.schindelin@gmx.de>
374
375 Documentation
376 --------------
377 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
378
379 GIT
380 ---
381 Part of the gitlink:git[7] suite