{"id":136891,"date":"2020-12-29T14:52:59","date_gmt":"2020-12-29T14:52:59","guid":{"rendered":"https:\/\/onet.com.vn\/how-to-cherry-pick-in-git.html"},"modified":"2020-12-29T14:52:59","modified_gmt":"2020-12-29T14:52:59","slug":"how-to-cherry-pick-in-git","status":"publish","type":"post","link":"https:\/\/onet.com.vn\/how-to-cherry-pick-in-git\/","title":{"rendered":"How to Cherry Pick in Git"},"content":{"rendered":"\n
git cherry pick is a merge feature of Git. But there is a slight difference in git cherry pick and git merge. Also, the use case is different. Let\u2019s see how git merge works first, then compare it to git cherry pick. That way, you will understand when to use git merge and when to use git cherry pick.<\/p>\n

Let\u2019s say, you have a Git repository. You\u2019re working on the master<\/strong> branch and you\u2019ve made a few commits (A, B and C<\/strong>) on the master<\/strong> branch as well.<\/p>\n

<\/p>\n

Now, all of a sudden you have a great idea. So, you create another branch newidea<\/strong>. Then, you started making commits (E, F, and G<\/strong>) there.<\/p>\n

<\/p>\n

You also made some changes to the master<\/strong> branch again and added a new commit H<\/strong>.<\/p>\n

<\/p>\n

Now, if your new idea is a success, then you may want to merge the newidea<\/strong> branch to the master<\/strong> branch. Let\u2019s say, you merged it. It will create a new commit I<\/strong> as you can see in the figure below. The new commit will contain everything (all the changes in the commits E<\/strong>, F<\/strong>, and G<\/strong>) of the branch newidea<\/strong>.<\/p>\n

<\/p>\n

Now, let\u2019s say, you don\u2019t want to merge all the commits of the branch newidea<\/strong> to the master<\/strong> branch. You only want to merge the changes (only the diff changes) in the commit F<\/strong> to the master<\/strong> branch. This is where git cherry pick comes in. Git cherry pick lets you do that. You just find the hash of the commit that you want to cherry pick and apply it to the branch you want. Very simple.<\/p>\n

In this article, I am going to show you how to cherry pick in Git. So, let\u2019s get started.<\/p>\n

Git Cherry Pick Workflow:<\/h3>\n

In this section, I am going to set up a Git repository in a way that you will understand why git cherry pick is used and how to cherry pick in Git.<\/p>\n

First, initialize an empty Git repository cherry-pick-demo\/<\/strong> as follows:<\/p>\n

\n
$ <\/span>git init<\/span> cherry-pick-demo<\/div>\n<\/div>\n

<\/p>\n

Now, navigate to the repository as follows:<\/p>\n

\n
$ <\/span>cd<\/span> cherry-pick-demo\/<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, create a main.c<\/strong> file with the following contents:<\/p>\n

<\/p>\n

Now, add the file to the staging area as follows:<\/p>\n

\n
$ <\/span>git add<\/span> .<\/div>\n<\/div>\n

<\/p>\n

Now, commit the changes as follows:<\/p>\n

\n
$ <\/span>git commit<\/span> -m<\/span> ‘initial commit’<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, create a .gitignore<\/strong> file with the following content:<\/p>\n

<\/p>\n

Add the file to the staging area.<\/p>\n

\n
$ <\/span>git add<\/span> .<\/div>\n<\/div>\n

<\/p>\n

Commit the changes:<\/p>\n

\n
$ <\/span>git commit<\/span> -m<\/span> ‘added .gitignore file’<\/span><\/div>\n<\/div>\n

<\/p>\n

As you can see, I have 2 commits now in my master<\/strong> branch.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, I want to push my local Git repository to a remote Git server so that other people can work on this repository. You can use GitHub here as well. I will use a local SSH server for this in here.<\/p>\n

So, add a remote Git repository URL as follows:<\/p>\n

\n
$ <\/span>git remote<\/span> add origin git<\/span>@<\/span>git.linuxhint.com:~\/<\/span>cherry-pick-demo.git<\/div>\n<\/div>\n

<\/p>\n

Now, push the master<\/strong> branch to the remote Git repository as follows:<\/p>\n

\n
$ <\/span>git push<\/span> origin master<\/div>\n<\/div>\n

<\/p>\n

Now, let\u2019s say bob<\/strong> wants to contribute to the project. So, he cloned the Git repository on his computer.<\/p>\n

\n
$ <\/span>git clone<\/span> git<\/span>@<\/span>git.linuxhint.com:~\/<\/span>cherry-pick-demo.git myproject<\/div>\n<\/div>\n

<\/p>\n

Now, bob navigates to his project directory.<\/p>\n

\n
$ <\/span>cd<\/span> myproject\/<\/span><\/div>\n<\/div>\n

<\/p>\n

He also has the 2 commits that I\u2019ve added.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, bob creates a test<\/strong> branch to try out his ideas.<\/p>\n

\n
$ <\/span>git checkout<\/span> -b<\/span> test<\/span><\/div>\n<\/div>\n

<\/p>\n

He decides to change the return value with a constant EXIT_SUCCESS<\/strong> from the stdlib<\/strong> library.<\/p>\n

<\/p>\n

He adds the changes to the staging area.<\/p>\n

\n
$ <\/span>git add<\/span> .<\/div>\n<\/div>\n

<\/p>\n

Commits the changes.<\/p>\n

\n
$ <\/span>git commit<\/span> -m<\/span> ‘used EXIT_SUCCESS instead of 0 as the return value’<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, he decides to use a function printMessage()<\/strong> to print the message. So, he writes the function.<\/p>\n

<\/p>\n

He commits the changes again.<\/p>\n

\n
$ git add<\/span> .
$ git commit<\/span> -m<\/span> ‘added printMessage() function’<\/span><\/div>\n<\/div>\n

<\/p>\n

Then, bob uses the function in the program.<\/p>\n

<\/p>\n

He commits the changes again.<\/p>\n

\n
$ git add<\/span> .
$ git commit<\/span> -m<\/span> ‘used printMessage() function to print the message’<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, bob has the following commits in the test<\/strong> branch.<\/p>\n

<\/p>\n

Now, bob pushes the test branch to the Git remote repository.<\/p>\n

\n
$ <\/span>git push<\/span> origin test<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, bob calls you and tells you about the awesome changes he made. So, you fetched the changes to the Git remote repository to your own local repository.<\/p>\n

\n
$ <\/span>git fetch<\/span><\/div>\n<\/div>\n

Now, you see a new branch origin\/test<\/strong>.<\/p>\n

<\/p>\n

You also found 3 new commits that bob made.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span> origin\/<\/span>test<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, you would like to know what changes bob made.<\/p>\n

\n
$ <\/span>git log<\/span> -p<\/span> origin\/<\/span>test<\/span><\/div>\n<\/div>\n

<\/p>\n

You decide to not replace the return value with EXIT_SUCCESS as bob did.<\/p>\n

<\/p>\n

You like the concept of using a function to print messages.<\/p>\n

<\/p>\n

You like this commit as well.<\/p>\n

<\/p>\n

So, you want to merge 2 out of the 3 commits bob made. If you\u2019ve used git merge to merge the branch test, then all 3 commits would be applied. But, with git cherry pick feature, you can only merge the commits that you like.<\/p>\n

Note that when you cherry pick commits in Git, you always start with the oldest commit and move forward to the newest little by little.<\/p>\n

Before, I cherry picked, the main.c<\/strong> file looks as follows.<\/p>\n

<\/p>\n

Now, let\u2019s cherry pick the oldest of the 2 commits, 9a4e532 as follows:<\/p>\n

\n
$ <\/span>git cherry-pick<\/span> 9a4e532<\/div>\n<\/div>\n

<\/p>\n

A merge conflict! This can happen.<\/p>\n

<\/p>\n

Now, open the main.c <\/strong>file and fix the merge conflict.<\/p>\n

<\/p>\n

The final file should look as follows.<\/p>\n

<\/p>\n

Now, add the changes to the staging area.<\/p>\n

\n
$ <\/span>git<\/span> add.<\/div>\n<\/div>\n

<\/p>\n

Now, commit the changes as follows:<\/p>\n

\n
$ <\/span>git cherry-pick<\/span> –continue<\/span><\/div>\n<\/div>\n

<\/p>\n

NOTE:<\/u><\/strong> You can also use git commit<\/strong> here as well. It\u2019s up to you. I prefer git cherry-pick –continue<\/strong> as it will automatically use the commit message from the commit I am cherry picking.<\/p>\n

Now, type in your commit message here and save the file.<\/p>\n

<\/p>\n

A new commit should be added.<\/p>\n

<\/p>\n

<\/p>\n

Now, cherry pick the next commit as follows:<\/p>\n

\n
$ <\/span>git cherry-pick<\/span> 08ba5e7<\/div>\n<\/div>\n

<\/p>\n

No merge conflict. Great! A new commit should be added automatically.<\/p>\n

<\/p>\n

<\/p>\n

As you can see, I get exactly what I wanted. I only merged the commits that I needed.<\/p>\n

<\/p>\n

So, that\u2019s how you cherry pick in Git. Thanks for reading this article.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"

git cherry pick is a merge feature of Git. But there is a slight difference in git cherry pick and git merge. Also, the use case is different. Let\u2019s see how git merge works first, then compare it to git cherry pick. That way, you will understand when to use git merge and when to […]<\/p>\n","protected":false},"author":1,"featured_media":136892,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"_links":{"self":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/136891"}],"collection":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/comments?post=136891"}],"version-history":[{"count":0,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/136891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media\/136892"}],"wp:attachment":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media?parent=136891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/categories?post=136891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/tags?post=136891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}