1d95ea1a4SJonathan Corbet.. SPDX-License-Identifier: GPL-2.0 2d95ea1a4SJonathan Corbet 3d95ea1a4SJonathan Corbet==================== 4d95ea1a4SJonathan CorbetRebasing and merging 5d95ea1a4SJonathan Corbet==================== 6d95ea1a4SJonathan Corbet 7d95ea1a4SJonathan CorbetMaintaining a subsystem, as a general rule, requires a familiarity with the 8d95ea1a4SJonathan CorbetGit source-code management system. Git is a powerful tool with a lot of 9d95ea1a4SJonathan Corbetfeatures; as is often the case with such tools, there are right and wrong 10d95ea1a4SJonathan Corbetways to use those features. This document looks in particular at the use 11d95ea1a4SJonathan Corbetof rebasing and merging. Maintainers often get in trouble when they use 12d95ea1a4SJonathan Corbetthose tools incorrectly, but avoiding problems is not actually all that 13d95ea1a4SJonathan Corbethard. 14d95ea1a4SJonathan Corbet 15d95ea1a4SJonathan CorbetOne thing to be aware of in general is that, unlike many other projects, 16d95ea1a4SJonathan Corbetthe kernel community is not scared by seeing merge commits in its 17d95ea1a4SJonathan Corbetdevelopment history. Indeed, given the scale of the project, avoiding 18d95ea1a4SJonathan Corbetmerges would be nearly impossible. Some problems encountered by 19d95ea1a4SJonathan Corbetmaintainers result from a desire to avoid merges, while others come from 20d95ea1a4SJonathan Corbetmerging a little too often. 21d95ea1a4SJonathan Corbet 22d95ea1a4SJonathan CorbetRebasing 23d95ea1a4SJonathan Corbet======== 24d95ea1a4SJonathan Corbet 25d95ea1a4SJonathan Corbet"Rebasing" is the process of changing the history of a series of commits 26d95ea1a4SJonathan Corbetwithin a repository. There are two different types of operations that are 27d95ea1a4SJonathan Corbetreferred to as rebasing since both are done with the ``git rebase`` 28d95ea1a4SJonathan Corbetcommand, but there are significant differences between them: 29d95ea1a4SJonathan Corbet 30d95ea1a4SJonathan Corbet - Changing the parent (starting) commit upon which a series of patches is 31d95ea1a4SJonathan Corbet built. For example, a rebase operation could take a patch set built on 32d95ea1a4SJonathan Corbet the previous kernel release and base it, instead, on the current 33d95ea1a4SJonathan Corbet release. We'll call this operation "reparenting" in the discussion 34d95ea1a4SJonathan Corbet below. 35d95ea1a4SJonathan Corbet 36d95ea1a4SJonathan Corbet - Changing the history of a set of patches by fixing (or deleting) broken 37d95ea1a4SJonathan Corbet commits, adding patches, adding tags to commit changelogs, or changing 38d95ea1a4SJonathan Corbet the order in which commits are applied. In the following text, this 39d95ea1a4SJonathan Corbet type of operation will be referred to as "history modification" 40d95ea1a4SJonathan Corbet 41d95ea1a4SJonathan CorbetThe term "rebasing" will be used to refer to both of the above operations. 42d95ea1a4SJonathan CorbetUsed properly, rebasing can yield a cleaner and clearer development 43d95ea1a4SJonathan Corbethistory; used improperly, it can obscure that history and introduce bugs. 44d95ea1a4SJonathan Corbet 45d95ea1a4SJonathan CorbetThere are a few rules of thumb that can help developers to avoid the worst 46d95ea1a4SJonathan Corbetperils of rebasing: 47d95ea1a4SJonathan Corbet 48d95ea1a4SJonathan Corbet - History that has been exposed to the world beyond your private system 49d95ea1a4SJonathan Corbet should usually not be changed. Others may have pulled a copy of your 50d95ea1a4SJonathan Corbet tree and built on it; modifying your tree will create pain for them. If 51d95ea1a4SJonathan Corbet work is in need of rebasing, that is usually a sign that it is not yet 52d95ea1a4SJonathan Corbet ready to be committed to a public repository. 53d95ea1a4SJonathan Corbet 54d95ea1a4SJonathan Corbet That said, there are always exceptions. Some trees (linux-next being 55d95ea1a4SJonathan Corbet a significant example) are frequently rebased by their nature, and 56d95ea1a4SJonathan Corbet developers know not to base work on them. Developers will sometimes 57d95ea1a4SJonathan Corbet expose an unstable branch for others to test with or for automated 58d95ea1a4SJonathan Corbet testing services. If you do expose a branch that may be unstable in 59d95ea1a4SJonathan Corbet this way, be sure that prospective users know not to base work on it. 60d95ea1a4SJonathan Corbet 61d95ea1a4SJonathan Corbet - Do not rebase a branch that contains history created by others. If you 62d95ea1a4SJonathan Corbet have pulled changes from another developer's repository, you are now a 63d95ea1a4SJonathan Corbet custodian of their history. You should not change it. With few 64d95ea1a4SJonathan Corbet exceptions, for example, a broken commit in a tree like this should be 65d95ea1a4SJonathan Corbet explicitly reverted rather than disappeared via history modification. 66d95ea1a4SJonathan Corbet 67d95ea1a4SJonathan Corbet - Do not reparent a tree without a good reason to do so. Just being on a 68d95ea1a4SJonathan Corbet newer base or avoiding a merge with an upstream repository is not 69d95ea1a4SJonathan Corbet generally a good reason. 70d95ea1a4SJonathan Corbet 71d95ea1a4SJonathan Corbet - If you must reparent a repository, do not pick some random kernel commit 72d95ea1a4SJonathan Corbet as the new base. The kernel is often in a relatively unstable state 73d95ea1a4SJonathan Corbet between release points; basing development on one of those points 74d95ea1a4SJonathan Corbet increases the chances of running into surprising bugs. When a patch 75d95ea1a4SJonathan Corbet series must move to a new base, pick a stable point (such as one of 76d95ea1a4SJonathan Corbet the -rc releases) to move to. 77d95ea1a4SJonathan Corbet 78d95ea1a4SJonathan Corbet - Realize that reparenting a patch series (or making significant history 79d95ea1a4SJonathan Corbet modifications) changes the environment in which it was developed and, 80d95ea1a4SJonathan Corbet likely, invalidates much of the testing that was done. A reparented 81d95ea1a4SJonathan Corbet patch series should, as a general rule, be treated like new code and 82d95ea1a4SJonathan Corbet retested from the beginning. 83d95ea1a4SJonathan Corbet 84d95ea1a4SJonathan CorbetA frequent cause of merge-window trouble is when Linus is presented with a 85d95ea1a4SJonathan Corbetpatch series that has clearly been reparented, often to a random commit, 86d95ea1a4SJonathan Corbetshortly before the pull request was sent. The chances of such a series 87d95ea1a4SJonathan Corbethaving been adequately tested are relatively low - as are the chances of 88d95ea1a4SJonathan Corbetthe pull request being acted upon. 89d95ea1a4SJonathan Corbet 90d95ea1a4SJonathan CorbetIf, instead, rebasing is limited to private trees, commits are based on a 91d95ea1a4SJonathan Corbetwell-known starting point, and they are well tested, the potential for 92d95ea1a4SJonathan Corbettrouble is low. 93d95ea1a4SJonathan Corbet 94d95ea1a4SJonathan CorbetMerging 95d95ea1a4SJonathan Corbet======= 96d95ea1a4SJonathan Corbet 97d95ea1a4SJonathan CorbetMerging is a common operation in the kernel development process; the 5.1 98d95ea1a4SJonathan Corbetdevelopment cycle included 1,126 merge commits - nearly 9% of the total. 99d95ea1a4SJonathan CorbetKernel work is accumulated in over 100 different subsystem trees, each of 100d95ea1a4SJonathan Corbetwhich may contain multiple topic branches; each branch is usually developed 101d95ea1a4SJonathan Corbetindependently of the others. So naturally, at least one merge will be 102d95ea1a4SJonathan Corbetrequired before any given branch finds its way into an upstream repository. 103d95ea1a4SJonathan Corbet 104d95ea1a4SJonathan CorbetMany projects require that branches in pull requests be based on the 105d95ea1a4SJonathan Corbetcurrent trunk so that no merge commits appear in the history. The kernel 106d95ea1a4SJonathan Corbetis not such a project; any rebasing of branches to avoid merges will, most 107d95ea1a4SJonathan Corbetlikely, lead to trouble. 108d95ea1a4SJonathan Corbet 109d95ea1a4SJonathan CorbetSubsystem maintainers find themselves having to do two types of merges: 110d95ea1a4SJonathan Corbetfrom lower-level subsystem trees and from others, either sibling trees or 111d95ea1a4SJonathan Corbetthe mainline. The best practices to follow differ in those two situations. 112d95ea1a4SJonathan Corbet 113d95ea1a4SJonathan CorbetMerging from lower-level trees 114d95ea1a4SJonathan Corbet------------------------------ 115d95ea1a4SJonathan Corbet 116d95ea1a4SJonathan CorbetLarger subsystems tend to have multiple levels of maintainers, with the 117d95ea1a4SJonathan Corbetlower-level maintainers sending pull requests to the higher levels. Acting 118d95ea1a4SJonathan Corbeton such a pull request will almost certainly generate a merge commit; that 119d95ea1a4SJonathan Corbetis as it should be. In fact, subsystem maintainers may want to use 120d95ea1a4SJonathan Corbetthe --no-ff flag to force the addition of a merge commit in the rare cases 121d95ea1a4SJonathan Corbetwhere one would not normally be created so that the reasons for the merge 122d95ea1a4SJonathan Corbetcan be recorded. The changelog for the merge should, for any kind of 123d95ea1a4SJonathan Corbetmerge, say *why* the merge is being done. For a lower-level tree, "why" is 124d95ea1a4SJonathan Corbetusually a summary of the changes that will come with that pull. 125d95ea1a4SJonathan Corbet 126d95ea1a4SJonathan CorbetMaintainers at all levels should be using signed tags on their pull 127d95ea1a4SJonathan Corbetrequests, and upstream maintainers should verify the tags when pulling 128d95ea1a4SJonathan Corbetbranches. Failure to do so threatens the security of the development 129d95ea1a4SJonathan Corbetprocess as a whole. 130d95ea1a4SJonathan Corbet 131d95ea1a4SJonathan CorbetAs per the rules outlined above, once you have merged somebody else's 132d95ea1a4SJonathan Corbethistory into your tree, you cannot rebase that branch, even if you 133d95ea1a4SJonathan Corbetotherwise would be able to. 134d95ea1a4SJonathan Corbet 135d95ea1a4SJonathan CorbetMerging from sibling or upstream trees 136d95ea1a4SJonathan Corbet-------------------------------------- 137d95ea1a4SJonathan Corbet 138d95ea1a4SJonathan CorbetWhile merges from downstream are common and unremarkable, merges from other 139d95ea1a4SJonathan Corbettrees tend to be a red flag when it comes time to push a branch upstream. 140d95ea1a4SJonathan CorbetSuch merges need to be carefully thought about and well justified, or 141d95ea1a4SJonathan Corbetthere's a good chance that a subsequent pull request will be rejected. 142d95ea1a4SJonathan Corbet 143d95ea1a4SJonathan CorbetIt is natural to want to merge the master branch into a repository; this 144d95ea1a4SJonathan Corbettype of merge is often called a "back merge". Back merges can help to make 145d95ea1a4SJonathan Corbetsure that there are no conflicts with parallel development and generally 146d95ea1a4SJonathan Corbetgives a warm, fuzzy feeling of being up-to-date. But this temptation 147d95ea1a4SJonathan Corbetshould be avoided almost all of the time. 148d95ea1a4SJonathan Corbet 149d95ea1a4SJonathan CorbetWhy is that? Back merges will muddy the development history of your own 150d95ea1a4SJonathan Corbetbranch. They will significantly increase your chances of encountering bugs 151d95ea1a4SJonathan Corbetfrom elsewhere in the community and make it hard to ensure that the work 152d95ea1a4SJonathan Corbetyou are managing is stable and ready for upstream. Frequent merges can 153d95ea1a4SJonathan Corbetalso obscure problems with the development process in your tree; they can 154d95ea1a4SJonathan Corbethide interactions with other trees that should not be happening (often) in 155d95ea1a4SJonathan Corbeta well-managed branch. 156d95ea1a4SJonathan Corbet 157d95ea1a4SJonathan CorbetThat said, back merges are occasionally required; when that happens, be 158d95ea1a4SJonathan Corbetsure to document *why* it was required in the commit message. As always, 159d95ea1a4SJonathan Corbetmerge to a well-known stable point, rather than to some random commit. 160d95ea1a4SJonathan CorbetEven then, you should not back merge a tree above your immediate upstream 161d95ea1a4SJonathan Corbettree; if a higher-level back merge is really required, the upstream tree 162d95ea1a4SJonathan Corbetshould do it first. 163d95ea1a4SJonathan Corbet 164d95ea1a4SJonathan CorbetOne of the most frequent causes of merge-related trouble is when a 165d95ea1a4SJonathan Corbetmaintainer merges with the upstream in order to resolve merge conflicts 166d95ea1a4SJonathan Corbetbefore sending a pull request. Again, this temptation is easy enough to 167d95ea1a4SJonathan Corbetunderstand, but it should absolutely be avoided. This is especially true 168d95ea1a4SJonathan Corbetfor the final pull request: Linus is adamant that he would much rather see 169d95ea1a4SJonathan Corbetmerge conflicts than unnecessary back merges. Seeing the conflicts lets 170d95ea1a4SJonathan Corbethim know where potential problem areas are. He does a lot of merges (382 171d95ea1a4SJonathan Corbetin the 5.1 development cycle) and has gotten quite good at conflict 172d95ea1a4SJonathan Corbetresolution - often better than the developers involved. 173d95ea1a4SJonathan Corbet 174d95ea1a4SJonathan CorbetSo what should a maintainer do when there is a conflict between their 175d95ea1a4SJonathan Corbetsubsystem branch and the mainline? The most important step is to warn 176d95ea1a4SJonathan CorbetLinus in the pull request that the conflict will happen; if nothing else, 177d95ea1a4SJonathan Corbetthat demonstrates an awareness of how your branch fits into the whole. For 178d95ea1a4SJonathan Corbetespecially difficult conflicts, create and push a *separate* branch to show 179d95ea1a4SJonathan Corbethow you would resolve things. Mention that branch in your pull request, 180d95ea1a4SJonathan Corbetbut the pull request itself should be for the unmerged branch. 181d95ea1a4SJonathan Corbet 182d95ea1a4SJonathan CorbetEven in the absence of known conflicts, doing a test merge before sending a 183d95ea1a4SJonathan Corbetpull request is a good idea. It may alert you to problems that you somehow 184d95ea1a4SJonathan Corbetdidn't see from linux-next and helps to understand exactly what you are 185d95ea1a4SJonathan Corbetasking upstream to do. 186d95ea1a4SJonathan Corbet 187d95ea1a4SJonathan CorbetAnother reason for doing merges of upstream or another subsystem tree is to 188d95ea1a4SJonathan Corbetresolve dependencies. These dependency issues do happen at times, and 189d95ea1a4SJonathan Corbetsometimes a cross-merge with another tree is the best way to resolve them; 190d95ea1a4SJonathan Corbetas always, in such situations, the merge commit should explain why the 191d95ea1a4SJonathan Corbetmerge has been done. Take a moment to do it right; people will read those 192d95ea1a4SJonathan Corbetchangelogs. 193d95ea1a4SJonathan Corbet 194d95ea1a4SJonathan CorbetOften, though, dependency issues indicate that a change of approach is 195d95ea1a4SJonathan Corbetneeded. Merging another subsystem tree to resolve a dependency risks 196d95ea1a4SJonathan Corbetbringing in other bugs and should almost never be done. If that subsystem 197d95ea1a4SJonathan Corbettree fails to be pulled upstream, whatever problems it had will block the 198d95ea1a4SJonathan Corbetmerging of your tree as well. Preferable alternatives include agreeing 199d95ea1a4SJonathan Corbetwith the maintainer to carry both sets of changes in one of the trees or 200d95ea1a4SJonathan Corbetcreating a topic branch dedicated to the prerequisite commits that can be 201d95ea1a4SJonathan Corbetmerged into both trees. If the dependency is related to major 202d95ea1a4SJonathan Corbetinfrastructural changes, the right solution might be to hold the dependent 203d95ea1a4SJonathan Corbetcommits for one development cycle so that those changes have time to 204d95ea1a4SJonathan Corbetstabilize in the mainline. 205d95ea1a4SJonathan Corbet 206d95ea1a4SJonathan CorbetFinally 207d95ea1a4SJonathan Corbet======= 208d95ea1a4SJonathan Corbet 209d95ea1a4SJonathan CorbetIt is relatively common to merge with the mainline toward the beginning of 210d95ea1a4SJonathan Corbetthe development cycle in order to pick up changes and fixes done elsewhere 211d95ea1a4SJonathan Corbetin the tree. As always, such a merge should pick a well-known release 212d95ea1a4SJonathan Corbetpoint rather than some random spot. If your upstream-bound branch has 213d95ea1a4SJonathan Corbetemptied entirely into the mainline during the merge window, you can pull it 214d95ea1a4SJonathan Corbetforward with a command like:: 215d95ea1a4SJonathan Corbet 216*a414684eSUwe Kleine-König git merge --ff-only v5.2-rc1 217d95ea1a4SJonathan Corbet 218d95ea1a4SJonathan CorbetThe guidelines laid out above are just that: guidelines. There will always 219d95ea1a4SJonathan Corbetbe situations that call out for a different solution, and these guidelines 220d95ea1a4SJonathan Corbetshould not prevent developers from doing the right thing when the need 221d95ea1a4SJonathan Corbetarises. But one should always think about whether the need has truly 222d95ea1a4SJonathan Corbetarisen and be prepared to explain why something abnormal needs to be done. 223