{"id":315390,"date":"2021-08-05T14:30:20","date_gmt":"2021-08-05T11:30:20","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it\/"},"modified":"2021-08-05T14:30:20","modified_gmt":"2021-08-05T11:30:20","slug":"what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it\/","title":{"rendered":"#What Does Git Cherry Pick Do, And When Should\u00a0You Use It? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2dfb60b9cf4\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2dfb60b9cf4\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it\/#What_Is_Git_Cherry_Pick_And_How_Does_It_Work\" >What Is Git Cherry Pick, And How Does It Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it\/#Why_Cherry_Pick\" >Why Cherry Pick?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-does-git-cherry-pick-do-and-when-should-you-use-it-cloudsavvy-it\/#Using_Git_Cherry_Pick\" >Using Git Cherry Pick<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Does Git Cherry Pick Do, And When Should\u00a0You Use It? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-2266\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Git logo\" width=\"700\" height=\"299\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><code>git cherry-pick<\/code>\u00a0is a simple but powerful tool that allows you to selectively transfer commits from one branch to another. You can use it when you don\u2019t want to merge an entire branch into master, but would still like to include changes from a feature branch.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_Git_Cherry_Pick_And_How_Does_It_Work\"><\/span>What Is Git Cherry Pick, And How Does It Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Git, commits are the units that track changes over time. Each commit might represent a bug fix, new feature, or part of a larger solution. You can think of commits being stored as a linked list pointing back in time; whenever you change your branch HEAD, Git reconstructs your local directory files by taking into account all commits going back to the beginning.<\/p>\n<p>This is a simplification of course, and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.blog\/2020-12-17-commits-are-snapshots-not-diffs\/\">Git doesn\u2019t technically store commits as simple lists of changes<\/a>, it uses file-based blobs, but the principle is the same.<\/p>\n<p>Things get more complicated when branches get involved. Often, work on a feature takes many days or weeks. Rather than clutter up the main repository, it goes into a separate feature branch, and is then code reviewed and merged back in. Merging is basically branching this commit history so that the entire feature branch gets included when Git does its thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13232\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/2518f538.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1167\" height=\"512\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, what if you want to merge some things, but don\u2019t want the entire branch to get included just yet? You can\u2019t do <code>git merge<\/code> in that case.<\/p>\n<p>This is where <code>git cherry-pick<\/code>\u00a0becomes useful. It does what the name implies\u2014takes a single commit from the feature branch, picks it out individually, and <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lies it to the master branch, or vice versa. This copies the commit, so that there\u2019s a brand new commit on the target branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13233\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/3c4e20b0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"500\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Note that there\u2019s no real \u201cline\u201d connecting the new commit on the master branch to the old commit. The new cherry-picked commit created on master does not reference the source commit at all, the commit is simply copied. Git will take care of it when you merge the branches back together, because the commits are seen as copies of each other.<\/p>\n<p>In effect, the commit is \u201ctransferred\u201d over to the new branch, though you will have to keep in mind that the old commit is still applied to the feature branch. You wouldn\u2019t want to revert the source commit though, since that will cause issues when merging later on.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Why_Cherry_Pick\"><\/span>Why Cherry Pick?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are a few uses cases for this. Perhaps you\u2019re working on a feature, and it\u2019s not ready for release, but you fixed a bug on the feature branch that you\u2019d like to get into your weekly release version. You can use cherry-pick to copy the bug fix to Master to deploy it early.<\/p>\n<p>Or perhaps you have multiple branches for production and development, and you\u2019d like to copy an urgent bug fix from production to development. Cherry pick can do that too.<\/p>\n<p>Or you may have even just accidentally made a commit to the wrong branch. You can use <code>git cherry-pick<\/code>\u00a0to copy it to the right one, and then\u00a0<code>git reset<\/code>\u00a0to undo that commit (provided it\u2019s at the HEAD of the branch).<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Git_Cherry_Pick\"><\/span>Using Git Cherry Pick<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you understand what you\u2019re doing, <code>git cherry-pick<\/code>\u00a0is pretty simple to use.<\/p>\n<p>The easiest way to view Git\u2019s commit history on the command line is with the following command, which shows all commits but with a branch and merge history, which is crucial for using cherry-pick:<\/p>\n<pre>git log --pretty=format:\"%h %s\" --graph<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13234\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/7f8a78ad.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"883\" height=\"375\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, you can simply copy the SHA1 hash of the commit, and run the cherry pick:<\/p>\n<pre>git cherry-pick 1da76d3<\/pre>\n<p>Note that you can also cherry pick multiple commits at the same time, just pass in multiple hashes.<\/p>\n<p><code>git cherry-pick<\/code>\u00a0has a few useful options:<\/p>\n<ul>\n<li><code>--no-commit<\/code> only applies the changes to your directory. You will need to stage and commit manually.<\/li>\n<li><code>--edit<\/code>\u00a0will let you change the commit message.<\/li>\n<li><code>-x<\/code>\u00a0will add a message saying \u201ccherry picked from commit: \u2026\u201d<\/li>\n<\/ul>\n<p>Of course, if you\u2019re using a GUI based Git client like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-fork.com\/\">Fork<\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gitkraken.com\/\">GitKraken<\/a>, which we highly recommend, you can just right click on the commit and choose to cherry pick it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13235\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/92fcb7e3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"478\" height=\"229\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><code\/>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13230\/what-does-git-cherry-pick-do-and-when-should-you-use-it\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Does Git Cherry Pick Do, And When Should\u00a0You Use It? \u2013 CloudSavvy IT&#8221; git cherry-pick\u00a0is a simple but powerful tool that allows you to selectively transfer commits from one branch to another. You can use it when you don\u2019t want to merge an entire branch into master, but would still like to include changes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":315391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-315390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/315390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=315390"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/315390\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/315391"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=315390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=315390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=315390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}