@@ -54,6 +54,12 @@ problem with applying the patch to the "wrong" base is that it may pull
5454in more unrelated changes in the context of the diff when cherry-picking
5555it to the older branch.
5656
57+ A good reason to prefer ``git cherry-pick `` over ``git am `` is that git
58+ knows the precise history of an existing commit, so it will know when
59+ code has moved around and changed the line numbers; this in turn makes
60+ it less likely to apply the patch to the wrong place (which can result
61+ in silent mistakes or messy conflicts).
62+
5763If you are using `b4 `_. and you are applying the patch directly from an
5864email, you can use ``b4 am `` with the options ``-g ``/``--guess-base ``
5965and ``-3 ``/``--prep-3way `` to do some of this automatically (see the
@@ -112,6 +118,7 @@ each method, and sometimes there's value in using both.
112118We will not cover using dedicated merge tools here beyond providing some
113119pointers to various tools that you could use:
114120
121+ - `Emacs Ediff mode <https://www.emacswiki.org/emacs/EdiffMode >`__
115122- `vimdiff/gvimdiff <https://linux.die.net/man/1/vimdiff >`__
116123- `KDiff3 <http://kdiff3.sourceforge.net/ >`__
117124- `TortoiseMerge <https://tortoisesvn.net/TortoiseMerge.html >`__
@@ -255,7 +262,7 @@ something like::
255262 >>>>>>> <commit>... title
256263
257264This is what you would see if you opened the file in your editor.
258- However, if you were to run run ``git diff `` without any arguments, the
265+ However, if you were to run ``git diff `` without any arguments, the
259266output would look something like this::
260267
261268 $ git diff
@@ -320,11 +327,11 @@ style, which looks like this::
320327 >>>>>>> <commit> (title)
321328
322329As you can see, this has 3 parts instead of 2, and includes what git
323- expected to find there but didn't. Some people vastly prefer this style
324- as it makes it much clearer what the patch actually changed; i.e., it
325- allows you to compare the before-and-after versions of the file for the
326- commit you are cherry-picking. This allows you to make better decisions
327- about how to resolve the conflict.
330+ expected to find there but didn't. It is * highly recommended * to use
331+ this conflict style as it makes it much clearer what the patch actually
332+ changed; i.e., it allows you to compare the before-and-after versions
333+ of the file for the commit you are cherry-picking. This allows you to
334+ make better decisions about how to resolve the conflict.
328335
329336To change conflict marker styles, you can use the following command::
330337
@@ -370,6 +377,42 @@ get them out of the way; this also lets you use ``git diff HEAD`` to
370377always see what remains to be resolved or ``git diff --cached `` to see
371378what your patch looks like so far.
372379
380+ Dealing with file renames
381+ ~~~~~~~~~~~~~~~~~~~~~~~~~
382+
383+ One of the most annoying things that can happen while backporting a
384+ patch is discovering that one of the files being patched has been
385+ renamed, as that typically means git won't even put in conflict markers,
386+ but will just throw up its hands and say (paraphrased): "Unmerged path!
387+ You do the work..."
388+
389+ There are generally a few ways to deal with this. If the patch to the
390+ renamed file is small, like a one-line change, the easiest thing is to
391+ just go ahead and apply the change by hand and be done with it. On the
392+ other hand, if the change is big or complicated, you definitely don't
393+ want to do it by hand.
394+
395+ As a first pass, you can try something like this, which will lower the
396+ rename detection threshold to 30% (by default, git uses 50%, meaning
397+ that two files need to have at least 50% in common for it to consider
398+ an add-delete pair to be a potential rename)::
399+
400+ git cherry-pick -strategy=recursive -Xrename-threshold=30
401+
402+ Sometimes the right thing to do will be to also backport the patch that
403+ did the rename, but that's definitely not the most common case. Instead,
404+ what you can do is to temporarily rename the file in the branch you're
405+ backporting to (using ``git mv `` and committing the result), restart the
406+ attempt to cherry-pick the patch, rename the file back (``git mv `` and
407+ committing again), and finally squash the result using ``git rebase -i ``
408+ (see the `rebase tutorial `_) so it appears as a single commit when you
409+ are done.
410+
411+ .. _rebase tutorial : https://medium.com/@slamflipstrom/a-beginners-guide-to-squashing-commits-with-git-rebase-8185cf6e62ec
412+
413+ Gotchas
414+ -------
415+
373416Function arguments
374417~~~~~~~~~~~~~~~~~~
375418
@@ -385,6 +428,9 @@ Error handling
385428If you cherry-pick a patch that includes a ``goto `` statement (typically
386429for error handling), it is absolutely imperative to double check that
387430the target label is still correct in the branch you are backporting to.
431+ The same goes for added ``return ``, ``break ``, and ``continue ``
432+ statements.
433+
388434Error handling is typically located at the bottom of the function, so it
389435may not be part of the conflict even though could have been changed by
390436other patches.
@@ -398,31 +444,31 @@ on either of the branches that you're backporting from or to. By
398444including the whole function in the diff you get more context and can
399445more easily spot problems that might otherwise go unnoticed.
400446
401- Dealing with file renames
402- ~~~~~~~~~~~~~~~~~~~~~~~~~
403-
404- One of the most annoying things that can happen while backporting a
405- patch is discovering that one of the files being patched has been
406- renamed, as that typically means git won't even put in conflict markers,
407- but will just throw up its hands and say (paraphrased): "Unmerged path!
408- You do the work..."
409-
410- There are generally a few ways to deal with this. If the patch to the
411- renamed file is small, like a one-line change, the easiest thing is to
412- just go ahead and apply the change by hand and be done with it. On the
413- other hand, if the change is big or complicated, you definitely don't
414- want to do it by hand.
415-
416- Sometimes the right thing to do will be to also backport the patch that
417- did the rename, but that's definitely not the most common case. Instead,
418- what you can do is to temporarily rename the file in the branch you're
419- backporting to (using ``git mv `` and committing the result), restart the
420- attempt to cherry-pick the patch, rename the file back ( ``git mv `` and
421- committing again), and finally squash the result using `` git rebase -i ``
422- (see the ` rebase tutorial `_) so it appears as a single commit when you
423- are done.
424-
425- .. _ rebase tutorial : https://medium.com/@slamflipstrom/a-beginners-guide-to-squashing-commits-with-git-rebase-8185cf6e62ec
447+ Refactored code
448+ ~~~~~~~~~~~~~~~
449+
450+ Something that happens quite often is that code gets refactored by
451+ "factoring out" a common code sequence or pattern into a helper
452+ function. When backporting patches to an area where such a refactoring
453+ has taken place, you effectively need to do the reverse when
454+ backporting: a patch to a single location may need to be applied to
455+ multiple locations in the backported version. (One giveaway for this
456+ scenario is that a function was renamed -- but that's not always the
457+ case.)
458+
459+ To avoid incomplete backports, it's worth trying to figure out if the
460+ patch fixes a bug that appears in more than one place. One way to do
461+ this would be to use `` git grep ``. (This is actually a good idea to do
462+ in general, not just for backports.) If you do find that the same kind
463+ of fix would apply to other places, it's also worth seeing if those
464+ places exist upstream -- if they don't, it's likely the patch may need
465+ to be adjusted. ``git log `` is your friend to figure out what happened
466+ to these areas as ``git blame `` won't show you code that has been
467+ removed.
468+
469+ If you do find other instances of the same pattern in the upstream tree
470+ and you're not sure whether it's also a bug, it may be worth asking the
471+ patch author. It's not uncommon to find new bugs during backporting!
426472
427473Verifying the result
428474====================
@@ -503,6 +549,50 @@ as you would (or should) give to any other patch. Having unit tests and
503549regression tests or other types of automatic testing can help increase
504550the confidence in the correctness of a backport.
505551
552+ Submitting backports to stable
553+ ==============================
554+
555+ As the stable maintainers try to cherry-pick mainline fixes onto their
556+ stable kernels, they may send out emails asking for backports when when
557+ encountering conflicts, see e.g.
558+ <https://lore.kernel.org/stable/2023101528-jawed-shelving-071a@gregkh/>.
559+ These emails typically include the exact steps you need to cherry-pick
560+ the patch to the correct tree and submit the patch.
561+
562+ One thing to make sure is that your changelog conforms to the expected
563+ format::
564+
565+ <original patch title>
566+
567+ [ Upstream commit <mainline rev> ]
568+
569+ <rest of the original changelog>
570+ [ <summary of the conflicts and their resolutions> ]
571+ Signed-off-by: <your name and email>
572+
573+ The "Upstream commit" line is sometimes slightly different depending on
574+ the stable version. Older version used this format::
575+
576+ commit <mainline rev> upstream.
577+
578+ It is most common to indicate the kernel version the patch applies to
579+ in the email subject line (using e.g.
580+ ``git send-email --subject-prefix='PATCH 6.1.y' ``), but you can also put
581+ it in the Signed-off-by:-area or below the ``--- `` line.
582+
583+ The stable maintainers expect separate submissions for each active
584+ stable version, and each submission should also be tested separately.
585+
586+ A few final words of advice
587+ ===========================
588+
589+ 1) Approach the backporting process with humility.
590+ 2) Understand the patch you are backporting; this means reading both
591+ the changelog and the code.
592+ 3) Be honest about your confidence in the result when submitting the
593+ patch.
594+ 4) Ask relevant maintainers for explicit acks.
595+
506596Examples
507597========
508598
0 commit comments