6 git-rebase - Forward-port local commits to the updated upstream head
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
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.
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`.
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>).
30 The commits that were previously saved into the temporary area are
31 then reapplied to the current branch, one by one, in order.
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.
40 Assume the following history exists and the current branch is "topic":
48 From this point, the result of either of the following commands:
52 git-rebase master topic
62 The latter form is just a short-hand of `git checkout topic`
63 followed by `git rebase master`.
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`.
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'.
74 o---o---o---o---o master
76 o---o---o---o---o next
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:
86 o---o---o---o---o master
90 o---o---o---o---o next
93 We can get this using the following command:
95 git-rebase --onto master next topic
98 Another example of --onto option is to rebase part of a
99 branch. If we have the following situation:
111 git-rebase --onto master topicA topicB
123 This is useful when topicB does not depend on topicA.
125 A range of commits could also be removed with rebase. If we have
126 the following situation:
129 E---F---G---H---I---J topicA
134 git-rebase --onto topicA~5 topicA~3 topicA
136 would result in the removal of commits F and G:
139 E---H'---I'---J' topicA
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.
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
156 After resolving the conflict manually and updating the index with the
157 desired resolution, you can continue the rebasing process with
160 git rebase --continue
163 Alternatively, you can undo the git-rebase with
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.
177 Upstream branch to compare against. May be any valid commit,
178 not just an existing branch name.
181 Working branch; defaults to HEAD.
184 Restart the rebasing process after having resolved a merge conflict.
187 Restore the original branch and abort the rebase operation.
190 Restart the rebasing process by skipping the current patch.
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
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.
205 Display a diffstat of what changed upstream since the last rebase.
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
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.
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).
222 -p, \--preserve-merges::
223 Instead of ignoring merges, try to recreate them. This option
224 only works in interactive mode.
226 include::merge-strategies.txt[]
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
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.
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.
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).
251 The interactive mode is meant for this type of workflow:
253 1. have a wonderful idea
255 3. prepare a series for submission
258 where point 2. consists of several instances of
261 1. finish something worthy of a commit
264 1. realize that something does not work
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.
274 Start it with the last commit you want to retain as-is:
276 git rebase -i <after-this-commit>
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:
283 -------------------------------------------
284 pick deadbee The oneline of this commit
285 pick fa1afe1 The oneline of the next commit
287 -------------------------------------------
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.
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
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 last commit.
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`.
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:
311 ----------------------
312 $ git rebase -i HEAD~5
313 ----------------------
315 And move the first patch to the end of the list.
317 You might want to preserve merges, if you have a history like this:
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
330 -----------------------------
331 $ git rebase -i -p --onto Q O
332 -----------------------------
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:
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.
347 - Mark the commit you want to split with the action "edit".
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.
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.
357 - Commit the now-current index with whatever commit message is appropriate
360 - Repeat the last two steps until your working tree is clean.
362 - Continue the rebase with 'git rebase --continue'.
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.
372 Written by Junio C Hamano <junkio@cox.net> and
373 Johannes E. Schindelin <johannes.schindelin@gmx.de>
377 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
381 Part of the gitlink:git[7] suite