{"id":98285,"date":"2018-03-21T07:00:00","date_gmt":"2018-03-21T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98285"},"modified":"2020-09-24T06:54:40","modified_gmt":"2020-09-24T13:54:40","slug":"20180321-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180321-00\/?p=98285","title":{"rendered":"Stop cherry-picking, start merging, Part 8: How to merge a partial cherry-pick"},"content":{"rendered":"<p>Continuing our exploration of using merges as a replacement for cherry-picking, here&#8217;s another scenario you can now solve:<\/p>\n<p><b>What if I want to take only part of a commit into another branch?<\/b><\/p>\n<p>Well, if you haven&#8217;t committed the change yet, then you can follow the usual workflow: Create a patch branch, commit only the part that you want to go into both branches, and then merge that patch branch into the master and feature branches. Once that&#8217;s done, you can make additional commits in the feature branch for the parts of the change you don&#8217;t want to go into master immediately.<\/p>\n<p><b>What if I already committed a change to my feature branch, and I want to take only part of it to the master branch?<\/b><\/p>\n<p>You can follow the retroactive merge pattern described earlier under <i>What if I already made the fix in my feature branch by committing directly to it, rather than creating a patch branch?<\/i> Put into the patch branch the piece of the commit that you want to share with the master branch.<\/p>\n<div id=\"p20180315_head\" style=\"display: none;\">\u00a0<\/div>\n<div id=\"p20180315_defs\" style=\"height: 0;\">\u00a0<\/div>\n<table style=\"text-align: center;\" title=\"Described in text.\" border=\"0\" cellspacing=\"1\">\n<tbody>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M1<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M2<\/td>\n<td>&nbsp;<\/td>\n<td>master<\/td>\n<\/tr>\n<tr>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">A<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">P<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>patch<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1a<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F2<\/td>\n<td>&nbsp;<\/td>\n<td>feature<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>From a starting commit A where the lines are <code>apple<\/code> and <code>apricot<\/code>, we create a feature branch. On the master and feature branches, we make unrelated commits M1 and F1, respectively, that don&#8217;t change either of the two lines. We then make a commit F1a on the feature branch that changes both lines to <code>berry<\/code> and <code>banana<\/code>. We want to propagate the <code>berry<\/code> part to the master branch, but not the <code>banana<\/code> part.<\/p>\n<p>To do this, we create a patch branch starting at the common commit A. On the patch branch, we create a commit P that changes the first line from <code>apple<\/code> to <code>berry<\/code>, but leaves the second line unchanged; it remains <code>apricot<\/code>. We merge this patch branch into the master branch as M2, resulting in <code>berry<\/code> and <code>apricot<\/code> in the master branch. We also merge this patch branch into the feature branch as F2, resulting in no change in the feature branch because the first line is already <code>berry<\/code>; the lines in the feature branch are still <code>berry<\/code> and <code>banana<\/code>.<\/p>\n<p>When this merges, the merge base will be <code>berry apricot<\/code>, which is identical to what&#8217;s in the master branch, which means that the change from the feature branch will be taken, resulting in <code>berry banana<\/code>.<\/p>\n<p>But let&#8217;s not merge yet. Suppose that the master branch makes a commit M3 which changes <code>berry<\/code> to <code>blackberry<\/code> but leaves <code>apricot<\/code> unchanged.<\/p>\n<table style=\"text-align: center;\" title=\"Described in text.\" border=\"0\" cellspacing=\"1\">\n<tbody>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">blackberry<br \/>\napricot<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M1<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M2<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M3<\/td>\n<td>&nbsp;<\/td>\n<td>master<\/td>\n<\/tr>\n<tr>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">A<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">P<\/td>\n<td>&nbsp;<\/td>\n<td>patch<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1a<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F2<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F3<\/td>\n<td>&nbsp;<\/td>\n<td>feature<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>What happens when we merge? Let&#8217;s look at the three-way merge:<\/p>\n<table style=\"text-align: center;\" title=\"Described in text.\" border=\"0\" cellspacing=\"1\">\n<tbody>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">blackberry<br \/>\napricot<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M3<\/td>\n<td>&nbsp;<\/td>\n<td>master<\/td>\n<\/tr>\n<tr>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td id=\"p20180315_svg01\">\u2199\ufe0e<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">P<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_svg02\">\u2196\ufe0e<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F3<\/td>\n<td>&nbsp;<\/td>\n<td>feature<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The three-way merge chooses commit P as the merge base, and in that commit, the lines are <code>berry<\/code> and <code>apricot<\/code>. In the master branch, the lines are <code>blackberry<\/code> and <code>apricot<\/code>: The net change is that the first line changed from <code>berry<\/code> to <code>blackberry<\/code>. In the feature branch, the lines are <code>berry<\/code> and <code>banana<\/code>: The net change is that the second line changed from <code>apricot<\/code> to <code>banana<\/code>.<\/p>\n<p>Therefore, the merge of the two branches is to accept the change of the first line from the master branch and the change of the second line from the feature branch, resulting in <code>blackberry<\/code> and <code>banana<\/code>, as desired.<\/p>\n<table style=\"text-align: center;\" title=\"Described in text.\" border=\"0\" cellspacing=\"1\">\n<tbody>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">blackberry<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">blackberry<br \/>\nbanana<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M1<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M2<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M3<\/td>\n<td id=\"p20180315_larr3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">M4<\/td>\n<td>&nbsp;<\/td>\n<td>master<\/td>\n<\/tr>\n<tr>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\napricot<\/td>\n<td id=\"p20180315_swarr\">\u2199\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_swarr3\" rowspan=\"3\">\u2199\ufe0e<\/td>\n<td colspan=\"3\" rowspan=\"3\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">A<\/td>\n<td id=\"p20180315_larr3\" colspan=\"3\">\u2190 \u2190 \u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">P<\/td>\n<td>&nbsp;<\/td>\n<td>patch<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td id=\"p20180315_nwarr\">\u2196\ufe0e<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F1a<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F2<\/td>\n<td id=\"p20180315_larr\">\u2190<\/td>\n<td style=\"border: solid 1px black; width: 50px; font-size: 120%;\">F3<\/td>\n<td>feature<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">apple<br \/>\napricot<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<td>&nbsp;<\/td>\n<td nowrap=\"nowrap\">berry<br \/>\nbanana<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n<script>\nwindow.addEventListener(\"load\", function() {\n  var fullFF = getComputedStyle(document.body).fontFamily;\n  var simpleFF = fullFF.replace(\/ Emoji\/g, \"\");\n  \/\/ break up \"style\" to prevent wordpress from injecting random junk\n  document.getElementById(\"p20180315_head\").innerHTML =\n`<s` + `tyle>\nbody { font-family: ${simpleFF}; }\n.emoji { font-family: ${fullFF}; }\n.entry-content th { padding: 1px; } \/* stylesheet workaround *\/\n.entry-content td { padding: 1px; } \/* stylesheet workaround *\/\n<\/s` + `tyle>`;\n}); \/\/ wacky comment to prevent wordpress from injecting random junk\n(function() {\n  var svg = {\n    defs: `<svg width=\"0\" height=\"0\">\n <defs>\n  <marker id=\"arrowhead\" markerWidth=\"5\" markerHeight=\"5\" refX=\"-2\" refY=\"0\"\n    viewBox=\"-6 -6 12 12\" orient=\"auto\">\n    <polygon points=\"-2,0 -5,5 5,0 -5,-5\" fill=\"black\" stroke=\"black\"\n      stroke-dasharray=\"1 0\" \/>\n  <\/marker>\n  <path id=\"larr\" d=\"M20,10 L3,10\" stroke=\"black\"\n   marker-end=\"url(#arrowhead)\" \/>\n  <path id=\"larr3\" d=\"M98,10 L3,10\" stroke=\"black\"\n   marker-end=\"url(#arrowhead)\" \/>\n  <path id=\"nwarr\" d=\"M20,20 L3,3\" stroke=\"black\"\n   marker-end=\"url(#arrowhead)\" \/>\n  <path id=\"swarr\" d=\"M20,2 L3,17\" stroke=\"black\"\n   marker-end=\"url(#arrowhead)\" \/>\n  <path id=\"swarr3\" d=\"M98,2 L6,57\" stroke=\"black\"\n   marker-end=\"url(#arrowhead)\" \/>\n <\/defs>\n<\/svg>`,\n    larr: `<svg width=\"20\" height=\"20\"><use href=\"#larr\"\/><\/svg>`,\n    larr3: `<svg width=\"98\" height=\"20\"><use href=\"#larr3\"\/><\/svg>`,\n    nwarr: `<svg width=\"20\" height=\"20\"><use href=\"#nwarr\"\/><\/svg>`,\n    swarr: `<svg width=\"20\" height=\"20\"><use href=\"#swarr\"\/><\/svg>`,\n    swarr3: `<svg width=\"98\" height=\"60\"><use href=\"#swarr3\"\/><\/svg>`,\n    blank: `<svg width=\"20\" height=\"20\"><\/svg>`,\n    svg01: `<svg width=\"98\" height=\"20\" style=\"overflow: visible\">\n     <path d=\"M98,-15 L50,-15 C30,-15 20,0 3,17\" fill=\"transparent\" stroke=\"black\" marker-end=\"url(#arrowhead)\" \/>\n    <\/svg>`,\n    svg02: `<svg width=\"98\" height=\"20\" style=\"overflow: visible\">\n     <path d=\"M98,35 L50,35 C30,35 20,20 3,3\" fill=\"transparent\" stroke=\"black\" marker-end=\"url(#arrowhead)\" \/>\n    <\/svg>`\n  };\n  Object.keys(svg).forEach(function (key) {\n    Array.prototype.forEach.call(document.querySelectorAll(\"#p20180315_\" + key),\n      function (e) {\n        e.innerHTML = svg[key];\n      });\n  });\n})();\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pick what you want.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-98285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Pick what you want.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=98285"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98285\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=98285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}