{"id":3203,"date":"2024-04-17T22:58:39","date_gmt":"2024-04-17T20:58:39","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3203"},"modified":"2024-10-21T10:05:02","modified_gmt":"2024-10-21T08:05:02","slug":"git-cherry-pick-what-it-is-and-how-to-use-it","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/04\/17\/git-cherry-pick-what-it-is-and-how-to-use-it\/","title":{"rendered":"git cherry-pick : what it is and how to use it"},"content":{"rendered":"\n<p>This command basically allows us to copy a single commit from one branch to another.<\/p>\n\n\n\n<p>It&#8217;s very useful when we need to add a specific change from a commit without merging the entire branch, for example.<\/p>\n\n\n\n<p>Contrast this with the way commit integration&nbsp;<em>normally<\/em>&nbsp;works in Git: when performing a Merge or Rebase,&nbsp;<strong>all commits<\/strong>&nbsp;from one branch are integrated.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-5.png\" alt=\"\" class=\"wp-image-3204\" width=\"430\" height=\"154\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-5.png 595w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-5-300x107.png 300w\" sizes=\"(max-width: 430px) 100vw, 430px\" \/><\/figure><\/div>\n\n\n\n<p>Cherry-pick, on the other hand, allows us to select&nbsp;<strong>individual<\/strong>&nbsp;commits for integration. In this example, only C2 is integrated into the master branch, but not C4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-6.png\" alt=\"\" class=\"wp-image-3205\" width=\"359\" height=\"131\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-6.png 542w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/04\/image-6-300x110.png 300w\" sizes=\"(max-width: 359px) 100vw, 359px\" \/><\/figure><\/div>\n\n\n\n<h4>How to use the git cherry-pick command?<\/h4>\n\n\n\n<p>In its most basic form, we only need to provide the SHA identifier of the commit we want to integrate into our current HEAD branch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git cherry-pick af01e0c<\/code><\/pre>\n\n\n\n<p>This way, the specified revision will directly be committed to our currently checked-out branch. If we would like to make some further modifications, we can also instruct Git to only add the commit&#8217;s&nbsp;<strong>changes<\/strong>&nbsp;to our Working Copy &#8211; without directly committing them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git cherry-pick af01e0c --no-commit<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Example: <\/h4>\n\n\n\n<p>To use the <code>git cherry-pick<\/code> command to copy a commit from branch B to branch A, follow these steps:<\/p>\n\n\n\n<ol><li><strong>Switch to Branch A<\/strong>:<br>First, ensure you&#8217;re on the branch where you want to apply the commit (Branch A).<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git checkout A<\/code><\/pre>\n\n\n\n<ol start=\"2\"><li><strong>Identify the Commit<\/strong>:<br>Find the commit hash from Branch B that you want to cherry-pick. You can do this by checking the log:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git log B<\/code><\/pre>\n\n\n\n<ol start=\"3\"><li><strong>Cherry-Pick the Commit<\/strong>:<br>Once you have the commit hash (let&#8217;s say it&#8217;s <code>abc1234<\/code>), run:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git cherry-pick abc1234<\/code><\/pre>\n\n\n\n<ol start=\"4\"><li><strong>Resolve Conflicts (if any)<\/strong>:<br>If there are conflicts, Git will notify you. You need to resolve these conflicts manually in your files. After resolving, stage the changes:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git add &lt;file_with_conflicts&gt;<\/code><\/pre>\n\n\n\n<p>Then, complete the cherry-pick:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   git cherry-pick --continue<\/code><\/pre>\n\n\n\n<ol start=\"5\"><li><strong>Verify the Commit<\/strong>:<br>Check your branch A to ensure the commit has been applied:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git log<\/code><\/pre>\n\n\n\n<h4>Summary:<\/h4>\n\n\n\n<ul><li>Switch to Branch A.<\/li><li>Find the commit hash in Branch B.<\/li><li>Use <code>git cherry-pick &lt;commit-hash&gt;<\/code> to copy it.<\/li><li>Resolve any conflicts if they arise.<\/li><\/ul>\n\n\n\n<p>This method allows us to selectively apply commits from one branch to another without merging the entire branch.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This command basically allows us to copy a single commit from one branch to another. It&#8217;s very useful when we need to add a specific change from a commit without merging the entire branch, for example. Contrast this with the way commit integration&nbsp;normally&nbsp;works in Git: when performing a Merge or Rebase,&nbsp;all commits&nbsp;from one branch are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[62,459,458,495,494],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3203"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3203"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3203\/revisions"}],"predecessor-version":[{"id":3494,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3203\/revisions\/3494"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}