Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/gitworkflows.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / gitworkflows.adoc` 475 lines ยท 17432 bytes Raw Blame History 1 gitworkflows(7) 2 =============== 3 4 NAME 5 ---- 6 gitworkflows - An overview of recommended workflows with Git 7 8 DESCRIPTION 9 ----------- 10 11 This document attempts to write down and motivate some of the workflow 12 elements used for `git.git` itself. Many ideas apply in general, 13 though the full workflow is rarely required for smaller projects with 14 fewer people involved. 15 16 We formulate a set of 'rules' for quick reference, while the prose 17 tries to motivate each of them. Do not always take them literally; 18 you should value good reasons for your actions higher than manpages 19 such as this one. 20 21 22 SEPARATE CHANGES 23 ---------------- 24 25 As a general rule, you should try to split your changes into small 26 logical steps, and commit each of them. They should be consistent, 27 working independently of any later commits, pass the test suite, etc. 28 This makes the review process much easier, and the history much more 29 useful for later inspection and analysis, for example with 30 linkgit:git-blame[1] and linkgit:git-bisect[1]. 31 32 To achieve this, try to split your work into small steps from the very 33 beginning. It is always easier to squash a few commits together than 34 to split one big commit into several. Don't be afraid of making too 35 small or imperfect steps along the way. You can always go back later 36 and edit the commits with `git rebase --interactive` before you 37 publish them. You can use `git stash push --keep-index` to run the 38 test suite independent of other uncommitted changes; see the EXAMPLES 39 section of linkgit:git-stash[1]. 40 41 42 MANAGING BRANCHES 43 ----------------- 44 45 There are two main tools that can be used to include changes from one 46 branch on another: linkgit:git-merge[1] and 47 linkgit:git-cherry-pick[1]. 48 49 Merges have many advantages, so we try to solve as many problems as 50 possible with merges alone. Cherry-picking is still occasionally 51 useful; see "Merging upwards" below for an example. 52 53 Most importantly, merging works at the branch level, while 54 cherry-picking works at the commit level. This means that a merge can 55 carry over the changes from 1, 10, or 1000 commits with equal ease, 56 which in turn means the workflow scales much better to a large number 57 of contributors (and contributions). Merges are also easier to 58 understand because a merge commit is a "promise" that all changes from 59 all its parents are now included. 60 61 There is a tradeoff of course: merges require a more careful branch 62 management. The following subsections discuss the important points. 63 64 65 Graduation 66 ~~~~~~~~~~ 67 68 As a given feature goes from experimental to stable, it also 69 "graduates" between the corresponding branches of the software. 70 `git.git` uses the following 'integration branches': 71 72 * 'maint' tracks the commits that should go into the next "maintenance 73 release", i.e., update of the last released stable version; 74 75 * 'master' tracks the commits that should go into the next release; 76 77 * 'next' is intended as a testing branch for topics being tested for 78 stability for master. 79 80 There is a fourth official branch that is used slightly differently: 81 82 * 'seen' (patches seen by the maintainer) is an integration branch for 83 things that are not quite ready for inclusion yet (see "Integration 84 Branches" below). 85 86 Each of the four branches is usually a direct descendant of the one 87 above it. 88 89 Conceptually, the feature enters at an unstable branch (usually 'next' 90 or 'seen'), and "graduates" to 'master' for the next release once it is 91 considered stable enough. 92 93 94 Merging upwards 95 ~~~~~~~~~~~~~~~ 96 97 The "downwards graduation" discussed above cannot be done by actually 98 merging downwards, however, since that would merge 'all' changes on 99 the unstable branch into the stable one. Hence the following: 100 101 .Merge upwards 102 [caption="Rule: "] 103 ===================================== 104 Always commit your fixes to the oldest supported branch that requires 105 them. Then (periodically) merge the integration branches upwards into each 106 other. 107 ===================================== 108 109 This gives a very controlled flow of fixes. If you notice that you 110 have applied a fix to e.g. 'master' that is also required in 'maint', 111 you will need to cherry-pick it (using linkgit:git-cherry-pick[1]) 112 downwards. This will happen a few times and is nothing to worry about 113 unless you do it very frequently. 114 115 116 Topic branches 117 ~~~~~~~~~~~~~~ 118 119 Any nontrivial feature will require several patches to implement, and 120 may get extra bugfixes or improvements during its lifetime. 121 122 Committing everything directly on the integration branches leads to many 123 problems: Bad commits cannot be undone, so they must be reverted one 124 by one, which creates confusing histories and further error potential 125 when you forget to revert part of a group of changes. Working in 126 parallel mixes up the changes, creating further confusion. 127 128 Use of "topic branches" solves these problems. The name is pretty 129 self explanatory, with a caveat that comes from the "merge upwards" 130 rule above: 131 132 .Topic branches 133 [caption="Rule: "] 134 ===================================== 135 Make a side branch for every topic (feature, bugfix, ...). Fork it off 136 at the oldest integration branch that you will eventually want to merge it 137 into. 138 ===================================== 139 140 Many things can then be done very naturally: 141 142 * To get the feature/bugfix into an integration branch, simply merge 143 it. If the topic has evolved further in the meantime, merge again. 144 (Note that you do not necessarily have to merge it to the oldest 145 integration branch first. For example, you can first merge a bugfix 146 to 'next', give it some testing time, and merge to 'maint' when you 147 know it is stable.) 148 149 * If you find you need new features from the branch 'other' to continue 150 working on your topic, merge 'other' to 'topic'. (However, do not 151 do this "just habitually", see below.) 152 153 * If you find you forked off the wrong branch and want to move it 154 "back in time", use linkgit:git-rebase[1]. 155 156 Note that the last point clashes with the other two: a topic that has 157 been merged elsewhere should not be rebased. See the section on 158 RECOVERING FROM UPSTREAM REBASE in linkgit:git-rebase[1]. 159 160 We should point out that "habitually" (regularly for no real reason) 161 merging an integration branch into your topics -- and by extension, 162 merging anything upstream into anything downstream on a regular basis 163 -- is frowned upon: 164 165 .Merge to downstream only at well-defined points 166 [caption="Rule: "] 167 ===================================== 168 Do not merge to downstream except with a good reason: upstream API 169 changes affect your branch; your branch no longer merges to upstream 170 cleanly; etc. 171 ===================================== 172 173 Otherwise, the topic that was merged to suddenly contains more than a 174 single (well-separated) change. The many resulting small merges will 175 greatly clutter up history. Anyone who later investigates the history 176 of a file will have to find out whether that merge affected the topic 177 in development. An upstream might even inadvertently be merged into a 178 "more stable" branch. And so on. 179 180 181 Throw-away integration 182 ~~~~~~~~~~~~~~~~~~~~~~ 183 184 If you followed the last paragraph, you will now have many small topic 185 branches, and occasionally wonder how they interact. Perhaps the 186 result of merging them does not even work? But on the other hand, we 187 want to avoid merging them anywhere "stable" because such merges 188 cannot easily be undone. 189 190 The solution, of course, is to make a merge that we can undo: merge 191 into a throw-away branch. 192 193 .Throw-away integration branches 194 [caption="Rule: "] 195 ===================================== 196 To test the interaction of several topics, merge them into a 197 throw-away branch. You must never base any work on such a branch! 198 ===================================== 199 200 If you make it (very) clear that this branch is going to be deleted 201 right after the testing, you can even publish this branch, for example 202 to give the testers a chance to work with it, or other developers a 203 chance to see if their in-progress work will be compatible. `git.git` 204 has such an official throw-away integration branch called 'seen'. 205 206 207 Branch management for a release 208 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 209 210 Assuming you are using the merge approach discussed above, when you 211 are releasing your project you will need to do some additional branch 212 management work. 213 214 A feature release is created from the 'master' branch, since 'master' 215 tracks the commits that should go into the next feature release. 216 217 The 'master' branch is supposed to be a superset of 'maint'. If this 218 condition does not hold, then 'maint' contains some commits that 219 are not included on 'master'. The fixes represented by those commits 220 will therefore not be included in your feature release. 221 222 To verify that 'master' is indeed a superset of 'maint', use git log: 223 224 .Verify 'master' is a superset of 'maint' 225 [caption="Recipe: "] 226 ===================================== 227 `git log master..maint` 228 ===================================== 229 230 This command should not list any commits. Otherwise, check out 231 'master' and merge 'maint' into it. 232 233 Now you can proceed with the creation of the feature release. Apply a 234 tag to the tip of 'master' indicating the release version: 235 236 .Release tagging 237 [caption="Recipe: "] 238 ===================================== 239 `git tag -s -m "Git X.Y.Z" vX.Y.Z master` 240 ===================================== 241 242 You need to push the new tag to a public Git server 243 (see <> below). 244 This makes the tag available to 245 others tracking your project. The push could also trigger a 246 post-update hook to perform release-related items such as building 247 release tarballs and preformatted documentation pages. 248 249 Similarly, for a maintenance release, 'maint' is tracking the commits 250 to be released. Therefore, in the steps above simply tag and push 251 'maint' rather than 'master'. 252 253 254 Maintenance branch management after a feature release 255 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 256 257 After a feature release, you need to manage your maintenance branches. 258 259 First, if you wish to continue to release maintenance fixes for the 260 feature release made before the recent one, then you must create 261 another branch to track commits for that previous release. 262 263 To do this, the current maintenance branch is copied to another branch 264 named with the previous release version number (e.g. maint-X.Y.(Z-1) 265 where X.Y.Z is the current release). 266 267 .Copy maint 268 [caption="Recipe: "] 269 ===================================== 270 `git branch maint-X.Y.(Z-1) maint` 271 ===================================== 272 273 The 'maint' branch should now be fast-forwarded to the newly released 274 code so that maintenance fixes can be tracked for the current release: 275 276 .Update maint to new release 277 [caption="Recipe: "] 278 ===================================== 279 * `git checkout maint` 280 * `git merge --ff-only master` 281 ===================================== 282 283 If the merge fails because it is not a fast-forward, then it is 284 possible some fixes on 'maint' were missed in the feature release. 285 This will not happen if the content of the branches was verified as 286 described in the previous section. 287 288 289 Branch management for next and seen after a feature release 290 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 291 292 After a feature release, the integration branch 'next' may optionally be 293 rewound and rebuilt from the tip of 'master' using the surviving 294 topics on 'next': 295 296 .Rewind and rebuild next 297 [caption="Recipe: "] 298 ===================================== 299 * `git switch -C next master` 300 * `git merge ai/topic_in_next1` 301 * `git merge ai/topic_in_next2` 302 * ... 303 ===================================== 304 305 The advantage of doing this is that the history of 'next' will be 306 clean. For example, some topics merged into 'next' may have initially 307 looked promising, but were later found to be undesirable or premature. 308 In such a case, the topic is reverted out of 'next' but the fact 309 remains in the history that it was once merged and reverted. By 310 recreating 'next', you give another incarnation of such topics a clean 311 slate to retry, and a feature release is a good point in history to do 312 so. 313 314 If you do this, then you should make a public announcement indicating 315 that 'next' was rewound and rebuilt. 316 317 The same rewind and rebuild process may be followed for 'seen'. A public 318 announcement is not necessary since 'seen' is a throw-away branch, as 319 described above. 320 321 322 [[DISTRIBUTED_WORKFLOWS]] 323 DISTRIBUTED WORKFLOWS 324 --------------------- 325 326 After the last section, you should know how to manage topics. In 327 general, you will not be the only person working on the project, so 328 you will have to share your work. 329 330 Roughly speaking, there are two important workflows: merge and patch. 331 The important difference is that the merge workflow can propagate full 332 history, including merges, while patches cannot. Both workflows can 333 be used in parallel: in `git.git`, only subsystem maintainers use 334 the merge workflow, while everyone else sends patches. 335 336 Note that the maintainer(s) may impose restrictions, such as 337 "Signed-off-by" requirements, that all commits/patches submitted for 338 inclusion must adhere to. Consult your project's documentation for 339 more information. 340 341 342 Merge workflow 343 ~~~~~~~~~~~~~~ 344 345 The merge workflow works by copying branches between upstream and 346 downstream. Upstream can merge contributions into the official 347 history; downstream base their work on the official history. 348 349 There are three main tools that can be used for this: 350 351 * linkgit:git-push[1] copies your branches to a remote repository, 352 usually to one that can be read by all involved parties; 353 354 * linkgit:git-fetch[1] that copies remote branches to your repository; 355 and 356 357 * linkgit:git-pull[1] that does fetch and merge in one go. 358 359 Note the last point. Do 'not' use 'git pull' unless you actually want 360 to merge the remote branch. 361 362 Getting changes out is easy: 363 364 .Push/pull: Publishing branches/topics 365 [caption="Recipe: "] 366 ===================================== 367 `git push ` and tell everyone where they can fetch 368 from. 369 ===================================== 370 371 You will still have to tell people by other means, such as mail. (Git 372 provides the linkgit:git-request-pull[1] to send preformatted pull 373 requests to upstream maintainers to simplify this task.) 374 375 If you just want to get the newest copies of the integration branches, 376 staying up to date is easy too: 377 378 .Push/pull: Staying up to date 379 [caption="Recipe: "] 380 ===================================== 381 Use `git fetch ` or `git remote update` to stay up to date. 382 ===================================== 383 384 Then simply fork your topic branches from the stable remotes as 385 explained earlier. 386 387 If you are a maintainer and would like to merge other people's topic 388 branches to the integration branches, they will typically send a 389 request to do so by mail. Such a request looks like 390 391 ------------------------------------- 392 Please pull from 393 394 ------------------------------------- 395 396 In that case, 'git pull' can do the fetch and merge in one go, as 397 follows. 398 399 .Push/pull: Merging remote topics 400 [caption="Recipe: "] 401 ===================================== 402 `git pull ` 403 ===================================== 404 405 Occasionally, the maintainer may get merge conflicts when they try to 406 pull changes from downstream. In this case, they can ask downstream to 407 do the merge and resolve the conflicts themselves (perhaps they will 408 know better how to resolve them). It is one of the rare cases where 409 downstream 'should' merge from upstream. 410 411 412 Patch workflow 413 ~~~~~~~~~~~~~~ 414 415 If you are a contributor that sends changes upstream in the form of 416 emails, you should use topic branches as usual (see above). Then use 417 linkgit:git-format-patch[1] to generate the corresponding emails 418 (highly recommended over manually formatting them because it makes the 419 maintainer's life easier). 420 421 .format-patch/am: Publishing branches/topics 422 [caption="Recipe: "] 423 ===================================== 424 * `git format-patch -M upstream..topic` to turn them into preformatted 425 patch files 426 * `git send-email --to= ` 427 ===================================== 428 429 See the linkgit:git-format-patch[1] and linkgit:git-send-email[1] 430 manpages for further usage notes. 431 432 If the maintainer tells you that your patch no longer applies to the 433 current upstream, you will have to rebase your topic (you cannot use a 434 merge because you cannot format-patch merges): 435 436 .format-patch/am: Keeping topics up to date 437 [caption="Recipe: "] 438 ===================================== 439 `git pull --rebase ` 440 ===================================== 441 442 You can then fix the conflicts during the rebase. Presumably you have 443 not published your topic other than by mail, so rebasing it is not a 444 problem. 445 446 If you receive such a patch series (as maintainer, or perhaps as a 447 reader of the mailing list it was sent to), save the mails to files, 448 create a new topic branch and use 'git am' to import the commits: 449 450 .format-patch/am: Importing patches 451 [caption="Recipe: "] 452 ===================================== 453 `git am < patch` 454 ===================================== 455 456 One feature worth pointing out is the three-way merge, which can help 457 if you get conflicts: `git am -3` will use index information contained 458 in patches to figure out the merge base. See linkgit:git-am[1] for 459 other options. 460 461 462 SEE ALSO 463 -------- 464 linkgit:gittutorial[7], 465 linkgit:git-push[1], 466 linkgit:git-pull[1], 467 linkgit:git-merge[1], 468 linkgit:git-rebase[1], 469 linkgit:git-format-patch[1], 470 linkgit:git-send-email[1], 471 linkgit:git-am[1] 472 473 GIT 474 --- 475 Part of the linkgit:git[1] suite