Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 1 | git-rebase(1) |
| 2 | ============= |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Junio C Hamano | 7c73c66 | 2007-01-19 00:37:50 | [diff] [blame] | 6 | git-rebase - Forward-port local commits to the updated upstream head |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
Junio C Hamano | a9b8d24 | 2007-05-19 04:51:55 | [diff] [blame] | 10 | [verse] |
Junio C Hamano | 250f03e | 2007-09-10 01:33:28 | [diff] [blame] | 11 | 'git-rebase' [-i | --interactive] [-v | --verbose] [-m | --merge] |
| 12 | [-C<n>] [ --whitespace=<option>] [-p | --preserve-merges] |
| 13 | [--onto <newbase>] <upstream> [<branch>] |
Junio C Hamano | 6959c6c | 2006-05-17 10:34:11 | [diff] [blame] | 14 | 'git-rebase' --continue | --skip | --abort |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 15 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 16 | DESCRIPTION |
| 17 | ----------- |
Junio C Hamano | 89d4e0f | 2007-02-18 00:34:59 | [diff] [blame] | 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. |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 32 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 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 |
Junio C Hamano | 6959c6c | 2006-05-17 10:34:11 | [diff] [blame] | 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. |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 39 | |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 40 | Assume the following history exists and the current branch is "topic": |
| 41 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 42 | ------------ |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 43 | A---B---C topic |
| 44 | / |
| 45 | D---E---F---G master |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 46 | ------------ |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 47 | |
Junio C Hamano | 2b13527 | 2006-03-18 07:45:42 | [diff] [blame] | 48 | From this point, the result of either of the following commands: |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 49 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 50 | |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 51 | git-rebase master |
| 52 | git-rebase master topic |
| 53 | |
| 54 | would be: |
| 55 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 56 | ------------ |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 57 | A'--B'--C' topic |
| 58 | / |
| 59 | D---E---F---G master |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 60 | ------------ |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 61 | |
Junio C Hamano | d8c9d43 | 2006-11-07 07:19:13 | [diff] [blame] | 62 | The latter form is just a short-hand of `git checkout topic` |
| 63 | followed by `git rebase master`. |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 64 | |
Junio C Hamano | d8c9d43 | 2006-11-07 07:19:13 | [diff] [blame] | 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`. |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 68 | |
Junio C Hamano | d8c9d43 | 2006-11-07 07:19:13 | [diff] [blame] | 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'. |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 72 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 73 | ------------ |
Junio C Hamano | d8c9d43 | 2006-11-07 07:19:13 | [diff] [blame] | 74 | o---o---o---o---o master |
| 75 | \ |
| 76 | o---o---o---o---o next |
| 77 | \ |
| 78 | o---o---o topic |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 79 | ------------ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 80 | |
Junio C Hamano | d8c9d43 | 2006-11-07 07:19:13 | [diff] [blame] | 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 | |
Junio C Hamano | 42f855f | 2007-02-06 00:09:38 | [diff] [blame] | 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 | |
Junio C Hamano | 3c1f2c7 | 2007-08-17 07:48:05 | [diff] [blame] | 134 | git-rebase --onto topicA~5 topicA~3 topicA |
Junio C Hamano | 42f855f | 2007-02-06 00:09:38 | [diff] [blame] | 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 | |
Junio C Hamano | f02e09f | 2006-03-27 07:51:03 | [diff] [blame] | 146 | In case of conflict, git-rebase will stop at the first problematic commit |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 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 |
Junio C Hamano | f02e09f | 2006-03-27 07:51:03 | [diff] [blame] | 151 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 152 | |
Junio C Hamano | 89d4e0f | 2007-02-18 00:34:59 | [diff] [blame] | 153 | git add <filename> |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 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 | |
Junio C Hamano | f02e09f | 2006-03-27 07:51:03 | [diff] [blame] | 162 | |
| 163 | Alternatively, you can undo the git-rebase with |
| 164 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 165 | |
| 166 | git rebase --abort |
Junio C Hamano | f02e09f | 2006-03-27 07:51:03 | [diff] [blame] | 167 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 168 | OPTIONS |
| 169 | ------- |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 170 | <newbase>:: |
| 171 | Starting point at which to create the new commits. If the |
| 172 | --onto option is not specified, the starting point is |
Junio C Hamano | 42f855f | 2007-02-06 00:09:38 | [diff] [blame] | 173 | <upstream>. May be any valid commit, and not just an |
| 174 | existing branch name. |
Junio C Hamano | 7e9f6b7 | 2006-02-22 10:44:55 | [diff] [blame] | 175 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 176 | <upstream>:: |
Junio C Hamano | 42f855f | 2007-02-06 00:09:38 | [diff] [blame] | 177 | Upstream branch to compare against. May be any valid commit, |
| 178 | not just an existing branch name. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 179 | |
Junio C Hamano | 2b13527 | 2006-03-18 07:45:42 | [diff] [blame] | 180 | <branch>:: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 181 | Working branch; defaults to HEAD. |
| 182 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 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 | |
Junio C Hamano | 97f518c | 2006-06-22 19:49:35 | [diff] [blame] | 189 | --skip:: |
| 190 | Restart the rebasing process by skipping the current patch. |
Junio C Hamano | 97f518c | 2006-06-22 19:49:35 | [diff] [blame] | 191 | |
Junio C Hamano | c5653d7 | 2007-09-02 06:25:16 | [diff] [blame] | 192 | -m, \--merge:: |
Junio C Hamano | 97f518c | 2006-06-22 19:49:35 | [diff] [blame] | 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 | |
Junio C Hamano | fbe0052 | 2006-10-19 05:58:48 | [diff] [blame] | 204 | -v, \--verbose:: |
| 205 | Display a diffstat of what changed upstream since the last rebase. |
| 206 | |
Junio C Hamano | d333998 | 2007-02-09 08:38:48 | [diff] [blame] | 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 | |
Junio C Hamano | 250f03e | 2007-09-10 01:33:28 | [diff] [blame] | 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 | |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 217 | -i, \--interactive:: |
| 218 | Make a list of the commits which are about to be rebased. Let the |
Junio C Hamano | dbb6459 | 2007-09-01 11:17:39 | [diff] [blame] | 219 | user edit that list before rebasing. This mode can also be used to |
| 220 | split commits (see SPLITTING COMMITS below). |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 221 | |
| 222 | -p, \--preserve-merges:: |
| 223 | Instead of ignoring merges, try to recreate them. This option |
| 224 | only works in interactive mode. |
| 225 | |
Junio C Hamano | 97f518c | 2006-06-22 19:49:35 | [diff] [blame] | 226 | include::merge-strategies.txt[] |
| 227 | |
Junio C Hamano | 6112cad | 2006-05-02 07:28:06 | [diff] [blame] | 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 | |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 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 |
Junio C Hamano | d41d6d0 | 2007-09-30 08:11:15 | [diff] [blame^] | 301 | the author of the first commit. |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 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 | |
Junio C Hamano | dbb6459 | 2007-09-01 11:17:39 | [diff] [blame] | 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 | |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 370 | Authors |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 371 | ------ |
Junio C Hamano | 1d90cb0 | 2007-07-03 07:05:31 | [diff] [blame] | 372 | Written by Junio C Hamano <junkio@cox.net> and |
| 373 | Johannes E. Schindelin <johannes.schindelin@gmx.de> |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 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 |