0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ====================
0004 Rebasing and merging
0005 ====================
0006
0007 Maintaining a subsystem, as a general rule, requires a familiarity with the
0008 Git source-code management system. Git is a powerful tool with a lot of
0009 features; as is often the case with such tools, there are right and wrong
0010 ways to use those features. This document looks in particular at the use
0011 of rebasing and merging. Maintainers often get in trouble when they use
0012 those tools incorrectly, but avoiding problems is not actually all that
0013 hard.
0014
0015 One thing to be aware of in general is that, unlike many other projects,
0016 the kernel community is not scared by seeing merge commits in its
0017 development history. Indeed, given the scale of the project, avoiding
0018 merges would be nearly impossible. Some problems encountered by
0019 maintainers result from a desire to avoid merges, while others come from
0020 merging a little too often.
0021
0022 Rebasing
0023 ========
0024
0025 "Rebasing" is the process of changing the history of a series of commits
0026 within a repository. There are two different types of operations that are
0027 referred to as rebasing since both are done with the ``git rebase``
0028 command, but there are significant differences between them:
0029
0030 - Changing the parent (starting) commit upon which a series of patches is
0031 built. For example, a rebase operation could take a patch set built on
0032 the previous kernel release and base it, instead, on the current
0033 release. We'll call this operation "reparenting" in the discussion
0034 below.
0035
0036 - Changing the history of a set of patches by fixing (or deleting) broken
0037 commits, adding patches, adding tags to commit changelogs, or changing
0038 the order in which commits are applied. In the following text, this
0039 type of operation will be referred to as "history modification"
0040
0041 The term "rebasing" will be used to refer to both of the above operations.
0042 Used properly, rebasing can yield a cleaner and clearer development
0043 history; used improperly, it can obscure that history and introduce bugs.
0044
0045 There are a few rules of thumb that can help developers to avoid the worst
0046 perils of rebasing:
0047
0048 - History that has been exposed to the world beyond your private system
0049 should usually not be changed. Others may have pulled a copy of your
0050 tree and built on it; modifying your tree will create pain for them. If
0051 work is in need of rebasing, that is usually a sign that it is not yet
0052 ready to be committed to a public repository.
0053
0054 That said, there are always exceptions. Some trees (linux-next being
0055 a significant example) are frequently rebased by their nature, and
0056 developers know not to base work on them. Developers will sometimes
0057 expose an unstable branch for others to test with or for automated
0058 testing services. If you do expose a branch that may be unstable in
0059 this way, be sure that prospective users know not to base work on it.
0060
0061 - Do not rebase a branch that contains history created by others. If you
0062 have pulled changes from another developer's repository, you are now a
0063 custodian of their history. You should not change it. With few
0064 exceptions, for example, a broken commit in a tree like this should be
0065 explicitly reverted rather than disappeared via history modification.
0066
0067 - Do not reparent a tree without a good reason to do so. Just being on a
0068 newer base or avoiding a merge with an upstream repository is not
0069 generally a good reason.
0070
0071 - If you must reparent a repository, do not pick some random kernel commit
0072 as the new base. The kernel is often in a relatively unstable state
0073 between release points; basing development on one of those points
0074 increases the chances of running into surprising bugs. When a patch
0075 series must move to a new base, pick a stable point (such as one of
0076 the -rc releases) to move to.
0077
0078 - Realize that reparenting a patch series (or making significant history
0079 modifications) changes the environment in which it was developed and,
0080 likely, invalidates much of the testing that was done. A reparented
0081 patch series should, as a general rule, be treated like new code and
0082 retested from the beginning.
0083
0084 A frequent cause of merge-window trouble is when Linus is presented with a
0085 patch series that has clearly been reparented, often to a random commit,
0086 shortly before the pull request was sent. The chances of such a series
0087 having been adequately tested are relatively low - as are the chances of
0088 the pull request being acted upon.
0089
0090 If, instead, rebasing is limited to private trees, commits are based on a
0091 well-known starting point, and they are well tested, the potential for
0092 trouble is low.
0093
0094 Merging
0095 =======
0096
0097 Merging is a common operation in the kernel development process; the 5.1
0098 development cycle included 1,126 merge commits - nearly 9% of the total.
0099 Kernel work is accumulated in over 100 different subsystem trees, each of
0100 which may contain multiple topic branches; each branch is usually developed
0101 independently of the others. So naturally, at least one merge will be
0102 required before any given branch finds its way into an upstream repository.
0103
0104 Many projects require that branches in pull requests be based on the
0105 current trunk so that no merge commits appear in the history. The kernel
0106 is not such a project; any rebasing of branches to avoid merges will, most
0107 likely, lead to trouble.
0108
0109 Subsystem maintainers find themselves having to do two types of merges:
0110 from lower-level subsystem trees and from others, either sibling trees or
0111 the mainline. The best practices to follow differ in those two situations.
0112
0113 Merging from lower-level trees
0114 ------------------------------
0115
0116 Larger subsystems tend to have multiple levels of maintainers, with the
0117 lower-level maintainers sending pull requests to the higher levels. Acting
0118 on such a pull request will almost certainly generate a merge commit; that
0119 is as it should be. In fact, subsystem maintainers may want to use
0120 the --no-ff flag to force the addition of a merge commit in the rare cases
0121 where one would not normally be created so that the reasons for the merge
0122 can be recorded. The changelog for the merge should, for any kind of
0123 merge, say *why* the merge is being done. For a lower-level tree, "why" is
0124 usually a summary of the changes that will come with that pull.
0125
0126 Maintainers at all levels should be using signed tags on their pull
0127 requests, and upstream maintainers should verify the tags when pulling
0128 branches. Failure to do so threatens the security of the development
0129 process as a whole.
0130
0131 As per the rules outlined above, once you have merged somebody else's
0132 history into your tree, you cannot rebase that branch, even if you
0133 otherwise would be able to.
0134
0135 Merging from sibling or upstream trees
0136 --------------------------------------
0137
0138 While merges from downstream are common and unremarkable, merges from other
0139 trees tend to be a red flag when it comes time to push a branch upstream.
0140 Such merges need to be carefully thought about and well justified, or
0141 there's a good chance that a subsequent pull request will be rejected.
0142
0143 It is natural to want to merge the master branch into a repository; this
0144 type of merge is often called a "back merge". Back merges can help to make
0145 sure that there are no conflicts with parallel development and generally
0146 gives a warm, fuzzy feeling of being up-to-date. But this temptation
0147 should be avoided almost all of the time.
0148
0149 Why is that? Back merges will muddy the development history of your own
0150 branch. They will significantly increase your chances of encountering bugs
0151 from elsewhere in the community and make it hard to ensure that the work
0152 you are managing is stable and ready for upstream. Frequent merges can
0153 also obscure problems with the development process in your tree; they can
0154 hide interactions with other trees that should not be happening (often) in
0155 a well-managed branch.
0156
0157 That said, back merges are occasionally required; when that happens, be
0158 sure to document *why* it was required in the commit message. As always,
0159 merge to a well-known stable point, rather than to some random commit.
0160 Even then, you should not back merge a tree above your immediate upstream
0161 tree; if a higher-level back merge is really required, the upstream tree
0162 should do it first.
0163
0164 One of the most frequent causes of merge-related trouble is when a
0165 maintainer merges with the upstream in order to resolve merge conflicts
0166 before sending a pull request. Again, this temptation is easy enough to
0167 understand, but it should absolutely be avoided. This is especially true
0168 for the final pull request: Linus is adamant that he would much rather see
0169 merge conflicts than unnecessary back merges. Seeing the conflicts lets
0170 him know where potential problem areas are. He does a lot of merges (382
0171 in the 5.1 development cycle) and has gotten quite good at conflict
0172 resolution - often better than the developers involved.
0173
0174 So what should a maintainer do when there is a conflict between their
0175 subsystem branch and the mainline? The most important step is to warn
0176 Linus in the pull request that the conflict will happen; if nothing else,
0177 that demonstrates an awareness of how your branch fits into the whole. For
0178 especially difficult conflicts, create and push a *separate* branch to show
0179 how you would resolve things. Mention that branch in your pull request,
0180 but the pull request itself should be for the unmerged branch.
0181
0182 Even in the absence of known conflicts, doing a test merge before sending a
0183 pull request is a good idea. It may alert you to problems that you somehow
0184 didn't see from linux-next and helps to understand exactly what you are
0185 asking upstream to do.
0186
0187 Another reason for doing merges of upstream or another subsystem tree is to
0188 resolve dependencies. These dependency issues do happen at times, and
0189 sometimes a cross-merge with another tree is the best way to resolve them;
0190 as always, in such situations, the merge commit should explain why the
0191 merge has been done. Take a moment to do it right; people will read those
0192 changelogs.
0193
0194 Often, though, dependency issues indicate that a change of approach is
0195 needed. Merging another subsystem tree to resolve a dependency risks
0196 bringing in other bugs and should almost never be done. If that subsystem
0197 tree fails to be pulled upstream, whatever problems it had will block the
0198 merging of your tree as well. Preferable alternatives include agreeing
0199 with the maintainer to carry both sets of changes in one of the trees or
0200 creating a topic branch dedicated to the prerequisite commits that can be
0201 merged into both trees. If the dependency is related to major
0202 infrastructural changes, the right solution might be to hold the dependent
0203 commits for one development cycle so that those changes have time to
0204 stabilize in the mainline.
0205
0206 Finally
0207 =======
0208
0209 It is relatively common to merge with the mainline toward the beginning of
0210 the development cycle in order to pick up changes and fixes done elsewhere
0211 in the tree. As always, such a merge should pick a well-known release
0212 point rather than some random spot. If your upstream-bound branch has
0213 emptied entirely into the mainline during the merge window, you can pull it
0214 forward with a command like::
0215
0216 git merge v5.2-rc1^0
0217
0218 The "^0" will cause Git to do a fast-forward merge (which should be
0219 possible in this situation), thus avoiding the addition of a spurious merge
0220 commit.
0221
0222 The guidelines laid out above are just that: guidelines. There will always
0223 be situations that call out for a different solution, and these guidelines
0224 should not prevent developers from doing the right thing when the need
0225 arises. But one should always think about whether the need has truly
0226 arisen and be prepared to explain why something abnormal needs to be done.