{"id":30819,"date":"2022-07-24T14:17:48","date_gmt":"2022-07-24T14:17:48","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30819"},"modified":"2022-12-23T05:48:03","modified_gmt":"2022-12-23T05:48:03","slug":"git-lab-exercise-assignment-git-cherry-pick-part-12","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-lab-exercise-assignment-git-cherry-pick-part-12\/","title":{"rendered":"Git Lab Exercise &#038; Assignment: Git cherry-pick: Part &#8211; 12"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>git-cherry-pick &#8211; Apply the changes introduced by some existing commits. Cherry picking in git means to choose a commit from one branch and apply it onto another. This is in contrast with other ways such as merge and rebase which normally applies many commits onto a another branch.<br><br>In another word, Cherry picking in Git is designed to apply some commit from one branch into another branch. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Make sure you are on the branch you want apply the commit to.\r\n\r\n\r\n$ git checkout master\r\nExecute the following:\r\n\r\n\r\n$ git cherry-pick \r\n\r\n<span class=\"hljs-comment\">#In this case, 62ecb3 is the cherry and you want to pick it!<\/span>\r\n$ git checkout master\r\n$ git cherry-pick <span class=\"hljs-number\">62<\/span>ecb3\r\nCherry picking a range of commits\r\n\r\nIn some cases picking one single commit is not enough. You need, let\u2019s say three consecutive commits. cherry-pick is not the right tool <span class=\"hljs-keyword\">for<\/span> this. rebase is. From the previous example, you\u2019d want commit <span class=\"hljs-number\">76<\/span>cada <span class=\"hljs-keyword\">and<\/span> <span class=\"hljs-number\">62<\/span>ecb3 in master. The flow is to first create a <span class=\"hljs-keyword\">new<\/span> branch from feature at the last commit you want, in this <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">62<\/span>ecb3.\r\n\r\n\r\n$ git checkout -b newbranch <span class=\"hljs-number\">62<\/span>ecb3\r\nNext up, you rebase the newbranch commit --onto master. The <span class=\"hljs-number\">76<\/span>cada^ indicates that you want to start from that specific commit.\r\n\r\n\r\n$ git rebase --onto master <span class=\"hljs-number\">76<\/span>cada^\r\n<span class=\"hljs-comment\"># The result is that commits 76cada through 62ecb3 are applied to master.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>git-cherry-pick &#8211; Apply the changes introduced by some existing commits. Cherry picking in git means to choose a commit from one branch and apply it onto another. This is in&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30819","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30819"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30819\/revisions"}],"predecessor-version":[{"id":30820,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30819\/revisions\/30820"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}