162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci====================
462306a36Sopenharmony_ciRebasing and merging
562306a36Sopenharmony_ci====================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciMaintaining a subsystem, as a general rule, requires a familiarity with the
862306a36Sopenharmony_ciGit source-code management system.  Git is a powerful tool with a lot of
962306a36Sopenharmony_cifeatures; as is often the case with such tools, there are right and wrong
1062306a36Sopenharmony_ciways to use those features.  This document looks in particular at the use
1162306a36Sopenharmony_ciof rebasing and merging.  Maintainers often get in trouble when they use
1262306a36Sopenharmony_cithose tools incorrectly, but avoiding problems is not actually all that
1362306a36Sopenharmony_cihard.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciOne thing to be aware of in general is that, unlike many other projects,
1662306a36Sopenharmony_cithe kernel community is not scared by seeing merge commits in its
1762306a36Sopenharmony_cidevelopment history.  Indeed, given the scale of the project, avoiding
1862306a36Sopenharmony_cimerges would be nearly impossible.  Some problems encountered by
1962306a36Sopenharmony_cimaintainers result from a desire to avoid merges, while others come from
2062306a36Sopenharmony_cimerging a little too often.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciRebasing
2362306a36Sopenharmony_ci========
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci"Rebasing" is the process of changing the history of a series of commits
2662306a36Sopenharmony_ciwithin a repository.  There are two different types of operations that are
2762306a36Sopenharmony_cireferred to as rebasing since both are done with the ``git rebase``
2862306a36Sopenharmony_cicommand, but there are significant differences between them:
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci - Changing the parent (starting) commit upon which a series of patches is
3162306a36Sopenharmony_ci   built.  For example, a rebase operation could take a patch set built on
3262306a36Sopenharmony_ci   the previous kernel release and base it, instead, on the current
3362306a36Sopenharmony_ci   release.  We'll call this operation "reparenting" in the discussion
3462306a36Sopenharmony_ci   below.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci - Changing the history of a set of patches by fixing (or deleting) broken
3762306a36Sopenharmony_ci   commits, adding patches, adding tags to commit changelogs, or changing
3862306a36Sopenharmony_ci   the order in which commits are applied.  In the following text, this
3962306a36Sopenharmony_ci   type of operation will be referred to as "history modification"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThe term "rebasing" will be used to refer to both of the above operations.
4262306a36Sopenharmony_ciUsed properly, rebasing can yield a cleaner and clearer development
4362306a36Sopenharmony_cihistory; used improperly, it can obscure that history and introduce bugs.
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciThere are a few rules of thumb that can help developers to avoid the worst
4662306a36Sopenharmony_ciperils of rebasing:
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci - History that has been exposed to the world beyond your private system
4962306a36Sopenharmony_ci   should usually not be changed.  Others may have pulled a copy of your
5062306a36Sopenharmony_ci   tree and built on it; modifying your tree will create pain for them.  If
5162306a36Sopenharmony_ci   work is in need of rebasing, that is usually a sign that it is not yet
5262306a36Sopenharmony_ci   ready to be committed to a public repository.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci   That said, there are always exceptions.  Some trees (linux-next being
5562306a36Sopenharmony_ci   a significant example) are frequently rebased by their nature, and
5662306a36Sopenharmony_ci   developers know not to base work on them.  Developers will sometimes
5762306a36Sopenharmony_ci   expose an unstable branch for others to test with or for automated
5862306a36Sopenharmony_ci   testing services.  If you do expose a branch that may be unstable in
5962306a36Sopenharmony_ci   this way, be sure that prospective users know not to base work on it.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci - Do not rebase a branch that contains history created by others.  If you
6262306a36Sopenharmony_ci   have pulled changes from another developer's repository, you are now a
6362306a36Sopenharmony_ci   custodian of their history.  You should not change it.  With few
6462306a36Sopenharmony_ci   exceptions, for example, a broken commit in a tree like this should be
6562306a36Sopenharmony_ci   explicitly reverted rather than disappeared via history modification.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci - Do not reparent a tree without a good reason to do so.  Just being on a
6862306a36Sopenharmony_ci   newer base or avoiding a merge with an upstream repository is not
6962306a36Sopenharmony_ci   generally a good reason.
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci - If you must reparent a repository, do not pick some random kernel commit
7262306a36Sopenharmony_ci   as the new base.  The kernel is often in a relatively unstable state
7362306a36Sopenharmony_ci   between release points; basing development on one of those points
7462306a36Sopenharmony_ci   increases the chances of running into surprising bugs.  When a patch
7562306a36Sopenharmony_ci   series must move to a new base, pick a stable point (such as one of
7662306a36Sopenharmony_ci   the -rc releases) to move to.
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci - Realize that reparenting a patch series (or making significant history
7962306a36Sopenharmony_ci   modifications) changes the environment in which it was developed and,
8062306a36Sopenharmony_ci   likely, invalidates much of the testing that was done.  A reparented
8162306a36Sopenharmony_ci   patch series should, as a general rule, be treated like new code and
8262306a36Sopenharmony_ci   retested from the beginning.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciA frequent cause of merge-window trouble is when Linus is presented with a
8562306a36Sopenharmony_cipatch series that has clearly been reparented, often to a random commit,
8662306a36Sopenharmony_cishortly before the pull request was sent.  The chances of such a series
8762306a36Sopenharmony_cihaving been adequately tested are relatively low - as are the chances of
8862306a36Sopenharmony_cithe pull request being acted upon.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciIf, instead, rebasing is limited to private trees, commits are based on a
9162306a36Sopenharmony_ciwell-known starting point, and they are well tested, the potential for
9262306a36Sopenharmony_citrouble is low.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciMerging
9562306a36Sopenharmony_ci=======
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciMerging is a common operation in the kernel development process; the 5.1
9862306a36Sopenharmony_cidevelopment cycle included 1,126 merge commits - nearly 9% of the total.
9962306a36Sopenharmony_ciKernel work is accumulated in over 100 different subsystem trees, each of
10062306a36Sopenharmony_ciwhich may contain multiple topic branches; each branch is usually developed
10162306a36Sopenharmony_ciindependently of the others.  So naturally, at least one merge will be
10262306a36Sopenharmony_cirequired before any given branch finds its way into an upstream repository.
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ciMany projects require that branches in pull requests be based on the
10562306a36Sopenharmony_cicurrent trunk so that no merge commits appear in the history.  The kernel
10662306a36Sopenharmony_ciis not such a project; any rebasing of branches to avoid merges will, most
10762306a36Sopenharmony_cilikely, lead to trouble.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciSubsystem maintainers find themselves having to do two types of merges:
11062306a36Sopenharmony_cifrom lower-level subsystem trees and from others, either sibling trees or
11162306a36Sopenharmony_cithe mainline.  The best practices to follow differ in those two situations.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciMerging from lower-level trees
11462306a36Sopenharmony_ci------------------------------
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciLarger subsystems tend to have multiple levels of maintainers, with the
11762306a36Sopenharmony_cilower-level maintainers sending pull requests to the higher levels.  Acting
11862306a36Sopenharmony_cion such a pull request will almost certainly generate a merge commit; that
11962306a36Sopenharmony_ciis as it should be.  In fact, subsystem maintainers may want to use
12062306a36Sopenharmony_cithe --no-ff flag to force the addition of a merge commit in the rare cases
12162306a36Sopenharmony_ciwhere one would not normally be created so that the reasons for the merge
12262306a36Sopenharmony_cican be recorded.  The changelog for the merge should, for any kind of
12362306a36Sopenharmony_cimerge, say *why* the merge is being done.  For a lower-level tree, "why" is
12462306a36Sopenharmony_ciusually a summary of the changes that will come with that pull.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciMaintainers at all levels should be using signed tags on their pull
12762306a36Sopenharmony_cirequests, and upstream maintainers should verify the tags when pulling
12862306a36Sopenharmony_cibranches.  Failure to do so threatens the security of the development
12962306a36Sopenharmony_ciprocess as a whole.
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciAs per the rules outlined above, once you have merged somebody else's
13262306a36Sopenharmony_cihistory into your tree, you cannot rebase that branch, even if you
13362306a36Sopenharmony_ciotherwise would be able to.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciMerging from sibling or upstream trees
13662306a36Sopenharmony_ci--------------------------------------
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciWhile merges from downstream are common and unremarkable, merges from other
13962306a36Sopenharmony_citrees tend to be a red flag when it comes time to push a branch upstream.
14062306a36Sopenharmony_ciSuch merges need to be carefully thought about and well justified, or
14162306a36Sopenharmony_cithere's a good chance that a subsequent pull request will be rejected.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciIt is natural to want to merge the master branch into a repository; this
14462306a36Sopenharmony_citype of merge is often called a "back merge".  Back merges can help to make
14562306a36Sopenharmony_cisure that there are no conflicts with parallel development and generally
14662306a36Sopenharmony_cigives a warm, fuzzy feeling of being up-to-date.  But this temptation
14762306a36Sopenharmony_cishould be avoided almost all of the time.
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciWhy is that?  Back merges will muddy the development history of your own
15062306a36Sopenharmony_cibranch.  They will significantly increase your chances of encountering bugs
15162306a36Sopenharmony_cifrom elsewhere in the community and make it hard to ensure that the work
15262306a36Sopenharmony_ciyou are managing is stable and ready for upstream.  Frequent merges can
15362306a36Sopenharmony_cialso obscure problems with the development process in your tree; they can
15462306a36Sopenharmony_cihide interactions with other trees that should not be happening (often) in
15562306a36Sopenharmony_cia well-managed branch.
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciThat said, back merges are occasionally required; when that happens, be
15862306a36Sopenharmony_cisure to document *why* it was required in the commit message.  As always,
15962306a36Sopenharmony_cimerge to a well-known stable point, rather than to some random commit.
16062306a36Sopenharmony_ciEven then, you should not back merge a tree above your immediate upstream
16162306a36Sopenharmony_citree; if a higher-level back merge is really required, the upstream tree
16262306a36Sopenharmony_cishould do it first.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciOne of the most frequent causes of merge-related trouble is when a
16562306a36Sopenharmony_cimaintainer merges with the upstream in order to resolve merge conflicts
16662306a36Sopenharmony_cibefore sending a pull request.  Again, this temptation is easy enough to
16762306a36Sopenharmony_ciunderstand, but it should absolutely be avoided.  This is especially true
16862306a36Sopenharmony_cifor the final pull request: Linus is adamant that he would much rather see
16962306a36Sopenharmony_cimerge conflicts than unnecessary back merges.  Seeing the conflicts lets
17062306a36Sopenharmony_cihim know where potential problem areas are.  He does a lot of merges (382
17162306a36Sopenharmony_ciin the 5.1 development cycle) and has gotten quite good at conflict
17262306a36Sopenharmony_ciresolution - often better than the developers involved.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciSo what should a maintainer do when there is a conflict between their
17562306a36Sopenharmony_cisubsystem branch and the mainline?  The most important step is to warn
17662306a36Sopenharmony_ciLinus in the pull request that the conflict will happen; if nothing else,
17762306a36Sopenharmony_cithat demonstrates an awareness of how your branch fits into the whole.  For
17862306a36Sopenharmony_ciespecially difficult conflicts, create and push a *separate* branch to show
17962306a36Sopenharmony_cihow you would resolve things.  Mention that branch in your pull request,
18062306a36Sopenharmony_cibut the pull request itself should be for the unmerged branch.
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciEven in the absence of known conflicts, doing a test merge before sending a
18362306a36Sopenharmony_cipull request is a good idea.  It may alert you to problems that you somehow
18462306a36Sopenharmony_cididn't see from linux-next and helps to understand exactly what you are
18562306a36Sopenharmony_ciasking upstream to do.
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ciAnother reason for doing merges of upstream or another subsystem tree is to
18862306a36Sopenharmony_ciresolve dependencies.  These dependency issues do happen at times, and
18962306a36Sopenharmony_cisometimes a cross-merge with another tree is the best way to resolve them;
19062306a36Sopenharmony_cias always, in such situations, the merge commit should explain why the
19162306a36Sopenharmony_cimerge has been done.  Take a moment to do it right; people will read those
19262306a36Sopenharmony_cichangelogs.
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ciOften, though, dependency issues indicate that a change of approach is
19562306a36Sopenharmony_cineeded.  Merging another subsystem tree to resolve a dependency risks
19662306a36Sopenharmony_cibringing in other bugs and should almost never be done.  If that subsystem
19762306a36Sopenharmony_citree fails to be pulled upstream, whatever problems it had will block the
19862306a36Sopenharmony_cimerging of your tree as well.  Preferable alternatives include agreeing
19962306a36Sopenharmony_ciwith the maintainer to carry both sets of changes in one of the trees or
20062306a36Sopenharmony_cicreating a topic branch dedicated to the prerequisite commits that can be
20162306a36Sopenharmony_cimerged into both trees.  If the dependency is related to major
20262306a36Sopenharmony_ciinfrastructural changes, the right solution might be to hold the dependent
20362306a36Sopenharmony_cicommits for one development cycle so that those changes have time to
20462306a36Sopenharmony_cistabilize in the mainline.
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciFinally
20762306a36Sopenharmony_ci=======
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciIt is relatively common to merge with the mainline toward the beginning of
21062306a36Sopenharmony_cithe development cycle in order to pick up changes and fixes done elsewhere
21162306a36Sopenharmony_ciin the tree.  As always, such a merge should pick a well-known release
21262306a36Sopenharmony_cipoint rather than some random spot.  If your upstream-bound branch has
21362306a36Sopenharmony_ciemptied entirely into the mainline during the merge window, you can pull it
21462306a36Sopenharmony_ciforward with a command like::
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci  git merge --ff-only v5.2-rc1
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciThe guidelines laid out above are just that: guidelines.  There will always
21962306a36Sopenharmony_cibe situations that call out for a different solution, and these guidelines
22062306a36Sopenharmony_cishould not prevent developers from doing the right thing when the need
22162306a36Sopenharmony_ciarises.  But one should always think about whether the need has truly
22262306a36Sopenharmony_ciarisen and be prepared to explain why something abnormal needs to be done. 
223