Note that there are some explanatory texts on larger screens.

plurals
  1. POHow-to git backport (rebase/cherry-pick) an already merged branch
    primarykey
    data
    text
    <p>In our Git process, "master" is the integration branch for topic and fix branches for the current release cycle, but we also maintain a "stable" branch where we have to backport carefully some of our fixes already successfully tested on master.</p> <p><strong>All the difficulty is that the branch has already been merged back in "master"</strong> (else it is really easy with rebase --onto)</p> <ul> <li>We don't want to change the process the other way because a) we don't want to fix everything in the "stable" branch, and b) we sometimes have to make some changes to the "stable" branch that we don't want to merge in "master".</li> <li>Clearly, we cannot merge the fix into the "stable" branch because this will backports many unwanted features.</li> </ul> <p>Graph of the initial situation I describe :</p> <pre><code> I--J (stable) / / / - A - B - C - D - E - F - G (master) \ / X -- Y (fix/123) </code></pre> <p>Graph of the kind of situation we want to reach :</p> <pre><code> I--J (stable) / \ / X'- Y' (fix/123-stable) / - A - B - C - D - E - F - G (master) \ / X -- Y (fix/123) </code></pre> <p>More complex cases are possible, such as multiple merge to complete a fix :</p> <pre><code>- A - B - C - D - E - F - G - H (master) \ / / X - Y ----- Z (fix/123) </code></pre> <p>But we don't allow merge into a fix branch, so we shall never have something like this :</p> <pre><code>- A - B - C - D - E - F - G (master) \ \ / X - Y - Z (fix/123) </code></pre> <p>To achieve this, we can cherry-pick or rebase the fix branch :</p> <p>1) cherry-pick (typicaly <a href="https://stackoverflow.com/questions/1440181/how-do-i-backport-a-commit-in-git">How do I backport a commit in git?</a>) :</p> <pre><code>git checkout -b fix/123-stable stable git cherry-pick X Y </code></pre> <p>This seems easy, but it is not when dealing with real life examples ; there is always a risk to forget some commits, or to pick wrong ones!</p> <p>2) rebase --onto (<a href="https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html" rel="nofollow noreferrer">https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html</a>) :</p> <p>2.a) the "not working" way :</p> <pre><code>git rebase --onto stable master fix/123 </code></pre> <p>This does nothing since fix/123 has already been merged to master! 2.b) the "not far better than cherry-pick" way :</p> <pre><code>git rebase --onto stable D fix/123 </code></pre> <p>This is still kind of risky because you need to take the SHA of D (and NOT X for instance).</p> <p>2.c) the "use a temporary starting ref" way :</p> <pre><code>git tag begin D git rebase --onto stable begin fix/123 git tag -d begin </code></pre> <p>This improve the previous situation, as the tag make it easier to do it or picture it in a graphical tool, but it is still lot of manual work.</p> <p>3.d) the "reset hard master before the merge" (to the first branching point) Hum, seems hard to describe and to do.</p> <p>So, what I am looking for is a git <strong>portable</strong> (no bash/grep/cut/sed implied) way to either;</p> <p>1) list all commits made on a branch already merged back into "master" (here X and Y, and also Z in the "multi-merged" case) to cherry-pick them easily </p> <p>2) get the commit of the first branch point of a branch already merged back into "master"</p> <p>2.a) this cannot be done by the "git merge-base" command because the merge is already done (even multiple time)</p> <p>2.b) I've found here <a href="https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git/2373815#2373815">Finding a branch point with Git?</a> the following bash command I tweaked a bit:</p> <pre><code>git rev-list --boundary --date-order --reverse fix/123..master | grep -m 1 - | cut -c2- </code></pre> <p>but his is not a git easy nor portable command (ie not working without Bash or Cygwin tools)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload