Documentation / git-rebase.txton commit Merge branch 'maint' (2af89f1)
   1git-rebase(1)
   2=============
   3
   4NAME
   5----
   6git-rebase - Forward-port local commits to the updated upstream head
   7
   8SYNOPSIS
   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
  16DESCRIPTION
  17-----------
  18If <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.
  32
  33It is possible that a merge failure will prevent this process from being
  34completely automatic.  You will have to resolve any such merge failure
  35and 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.
  39
  40Assume 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
  48From this point, the result of either of the following commands:
  49
  50
  51    git-rebase master
  52    git-rebase master topic
  53
  54would be:
  55
  56------------
  57                  A'--B'--C' topic
  58                 /
  59    D---E---F---G master
  60------------
  61
  62The latter form is just a short-hand of `git checkout topic`
  63followed by `git rebase master`.
  64
  65Here 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`.
  68
  69First 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'.
  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
  81We 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
 125A 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
 134    git-rebase --onto topicA~5 topicA~3 topicA
 135
 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
 146In case of conflict, git-rebase will stop at the first problematic commit
 147and 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
 151
 152
 153    git add <filename>
 154
 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
 162
 163Alternatively, you can undo the git-rebase with
 164
 165
 166    git rebase --abort
 167
 168OPTIONS
 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
 226include::merge-strategies.txt[]
 227
 228NOTES
 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
 244INTERACTIVE 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
 276        git 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
 301the author of the first commit.
 302
 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
 334
 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
 370Authors
 371------
 372Written by Junio C Hamano <junkio@cox.net> and
 373Johannes E. Schindelin <johannes.schindelin@gmx.de>
 374
 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