<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Danyl Fernandes</title>
    <description>The latest articles on DEV Community by Danyl Fernandes (@gitryder).</description>
    <link>https://dev.to/gitryder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F239256%2Feece56de-cd02-436e-8e43-bfaa55a9cd8d.jpg</url>
      <title>DEV Community: Danyl Fernandes</title>
      <link>https://dev.to/gitryder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gitryder"/>
    <language>en</language>
    <item>
      <title>When to use git cherry-pick &amp; how 🍒</title>
      <dc:creator>Danyl Fernandes</dc:creator>
      <pubDate>Wed, 15 Sep 2021 11:16:27 +0000</pubDate>
      <link>https://dev.to/gitryder/when-to-use-git-cherry-pick-how-2fm9</link>
      <guid>https://dev.to/gitryder/when-to-use-git-cherry-pick-how-2fm9</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Intro&lt;/li&gt;
&lt;li&gt;But why?&lt;/li&gt;
&lt;li&gt;
Let's see how

&lt;ul&gt;
&lt;li&gt;Setup&lt;/li&gt;
&lt;li&gt;Simulating the error&lt;/li&gt;
&lt;li&gt;Cherry picking 🍒&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Keep this in mind&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intro &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Without much ado, let's get right into it...&lt;/p&gt;

&lt;p&gt;So, the &lt;a href="https://git-scm.com/docs"&gt;official git docs&lt;/a&gt; describe &lt;code&gt;cherry-pick&lt;/code&gt; as the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given one or more existing commits, apply the change each one introduces, recording a new commit for each.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let us see what that means exactly...&lt;/p&gt;

&lt;h2&gt;
  
  
  But why? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;OK, picture this: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're working on a project and you're inside a local git repository. &lt;/li&gt;
&lt;li&gt;You have been working on an awesome feature on a separate branch.&lt;/li&gt;
&lt;li&gt;You just added some more code and committed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But wait. You had to commit to the feature branch, but you committed to master 😱&lt;/p&gt;

&lt;p&gt;What do you do in this case? Do you copy your code and paste it in the right branch and then commit again? &lt;/p&gt;

&lt;p&gt;Now this is exactly why you need &lt;code&gt;git cherry-pick&lt;/code&gt;. &lt;strong&gt;It allows you to "cherry-pick" commits from one branch and put them elsewhere (on another branch).&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's see how &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you have an understanding of why you might need it, let's see how you can use it with a small example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, I'll &lt;code&gt;cd&lt;/code&gt; to my Desktop and create a brand new folder called "learning" with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;Desktop &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;mkdir &lt;/span&gt;learning
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's &lt;code&gt;cd&lt;/code&gt; into our new directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;learning
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's initialize a new git repository in this new folder with the command &lt;code&gt;git init&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j_mEoyHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny7c13s3v0a87mydbfwj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j_mEoyHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny7c13s3v0a87mydbfwj.png" alt="git init image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OK, our git repository is ready. Let's create a new file now and commit the changes...&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VeDS6i6l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyfwpxd49gbsnnstn05g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VeDS6i6l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyfwpxd49gbsnnstn05g.png" alt="create commit in git"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's create a new branch and add one commit there too:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I84m5XMz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9n5nmlleiecbxq94vz4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I84m5XMz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9n5nmlleiecbxq94vz4.png" alt="new branch commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OK, now let's quickly check our git logs to see the commits we have currently. We'll use this command for each branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log &lt;span class="nt"&gt;--oneline&lt;/span&gt; &lt;span class="nt"&gt;--graph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;ℹ️ &lt;strong&gt;:&lt;/strong&gt; &lt;code&gt;-oneline&lt;/code&gt; will give us all info for each commit in a single line and &lt;code&gt;--graph&lt;/code&gt; will make it look nice in a tree like format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Log for the &lt;code&gt;master&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wMJWoLZZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iz7z2u3clqbk7i9vxkl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wMJWoLZZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iz7z2u3clqbk7i9vxkl.png" alt="master branch log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Log for the &lt;code&gt;some-awesome-feature&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FF5uMVIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gtnbxmokknegwgyz2gl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FF5uMVIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gtnbxmokknegwgyz2gl.png" alt="some new feature log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's simulate the error now 😋&lt;/p&gt;

&lt;h3&gt;
  
  
  Simulating the error &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now to simulate the error, we will make a commit to the &lt;code&gt;master&lt;/code&gt; branch that should have been made to the &lt;code&gt;some-awesome-feature&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Let's do that:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aNMS4FRW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31renaphmcxa05fpq54g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aNMS4FRW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31renaphmcxa05fpq54g.png" alt="wrong commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Good job! 🥳 &lt;br&gt;
Now onto cherry-picking 🍒&lt;/p&gt;
&lt;h3&gt;
  
  
  Cherry Picking 🍒 &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, checking the wrong branch (the &lt;code&gt;master&lt;/code&gt; branch in this case), we see the commit that we want to pick and &lt;em&gt;copy&lt;/em&gt; to the right branch.&lt;/p&gt;
&lt;h4&gt;
  
  
  Log for the &lt;code&gt;master&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--osnVqcBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iq6b1xvmutx92nwuog2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--osnVqcBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iq6b1xvmutx92nwuog2.png" alt="master branch log"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Log for the &lt;code&gt;some-awesome-feature&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FF5uMVIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gtnbxmokknegwgyz2gl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FF5uMVIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gtnbxmokknegwgyz2gl.png" alt="some new feature log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, first to copy the commit over from &lt;code&gt;master&lt;/code&gt; to the right branch, we need the commit hash of that commit.&lt;/p&gt;

&lt;p&gt;If you look at the wrong branch carefully (the &lt;code&gt;master&lt;/code&gt; branch in our case), the commit hash is &lt;code&gt;67d2e2f&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is important because we need to tell cherry-pick which commit has to be copied.&lt;/p&gt;

&lt;p&gt;Now, to cherry-pick, we always &lt;code&gt;git checkout&lt;/code&gt; to the right branch first. Let's do that with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout some-awesome-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick &lt;code&gt;git log&lt;/code&gt; on this branch:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pGEFfOwd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ij4ydy9hr0jthdf4b50w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pGEFfOwd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ij4ydy9hr0jthdf4b50w.png" alt="git log on correct branch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the commit we want here, isn't there.&lt;/p&gt;

&lt;p&gt;Let's use the magic ✨ of &lt;code&gt;cherry-pick&lt;/code&gt;, the one command you have been waiting for!&lt;/p&gt;

&lt;p&gt;On the branch that we want the commit to be on, we'll run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git cherry-pick &amp;lt;commit_hash_of_the_commit_you_want_here&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;As you saw earlier, the hash of the commit we want is &lt;code&gt;67d2e2f&lt;/code&gt; so we'll use that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w_PO5bzD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pklw1nq8qi1s42iw95ay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w_PO5bzD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pklw1nq8qi1s42iw95ay.png" alt="after cherry-pick"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Boom! 💥. We're done! Let's check?&lt;/p&gt;

&lt;p&gt;Let's check the git log for this branch again:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0yiGI4Gf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rhxjwuzp9c81pdlpt37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0yiGI4Gf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rhxjwuzp9c81pdlpt37.png" alt="final log after cherry-pick"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whoaa 🎊 &lt;/p&gt;

&lt;p&gt;However, notice that the commit has a different hash here 😯. That's what we'll discuss in the final part of this post...&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep this in mind &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;For the final time, let's check the commit logs for both the branches&lt;/p&gt;

&lt;h4&gt;
  
  
  Log for the &lt;code&gt;master&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FLs29p5h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stucxvl99scs1ogr80e2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FLs29p5h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stucxvl99scs1ogr80e2.png" alt="master branch log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Log for the &lt;code&gt;some-awesome-feature&lt;/code&gt; branch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--30qufUtr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d19sll625uefuvu2fqm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--30qufUtr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d19sll625uefuvu2fqm4.png" alt="other branch log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  1.
&lt;/h4&gt;

&lt;p&gt;=======&lt;br&gt;
You can see that the commit we cherry-picked has not been moved from our wrong branch (the &lt;code&gt;master&lt;/code&gt; branch in our case), but it has been copied.&lt;/p&gt;

&lt;p&gt;This is what is called a "replay". Git did not move your commit but instead replayed the changes on the right branch, which is exactly why the commit has a different hash than the original one.&lt;/p&gt;

&lt;p&gt;The original commit will always remain where it has been and it is your responsibility to delete it from there (using something like &lt;code&gt;git reset&lt;/code&gt;)&lt;/p&gt;

&lt;h4&gt;
  
  
  2.
&lt;/h4&gt;

&lt;p&gt;=======&lt;br&gt;
You saw how the hash of the copied commit is different form the original, and this is why you should avoid using these commands if you've already pushed the changes to a remote repository (say GitHub for instance), because if you do, other collaborators on the project would have a different history than yours and this could mess a lot of things up.&lt;/p&gt;




&lt;p&gt;Aaaand that's it from me! I thank you so much for reading through. You are awesome! 🥳&lt;/p&gt;

&lt;p&gt;If you loved it, you can show some love on &lt;a href="https://twitter.com/androidanyl"&gt;Twitter&lt;/a&gt; and / or leave your beautiful comments below.&lt;/p&gt;

&lt;p&gt;Corrections &amp;amp; Suggestions welcome.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>intermediate</category>
    </item>
  </channel>
</rss>
