How to Cherry Pick in Git

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’s 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.

Let’s say, you have a Git repository. You’re working on the master branch and you’ve made a few commits (A, B and C) on the master branch as well.

Now, all of a sudden you have a great idea. So, you create another branch newidea. Then, you started making commits (E, F, and G) there.

You also made some changes to the master branch again and added a new commit H.

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

Now, let’s say, you don’t want to merge all the commits of the branch newidea to the master branch. You only want to merge the changes (only the diff changes) in the commit F to the master 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.

In this article, I am going to show you how to cherry pick in Git. So, let’s get started.

Git Cherry Pick Workflow:

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.

First, initialize an empty Git repository cherry-pick-demo/ as follows:

$ git init cherry-pick-demo

Now, navigate to the repository as follows:

$ cd cherry-pick-demo/

Now, create a main.c file with the following contents:

Now, add the file to the staging area as follows:

$ git add .

Now, commit the changes as follows:

$ git commit -m ‘initial commit’

Now, create a .gitignore file with the following content:

Add the file to the staging area.

$ git add .

Commit the changes:

$ git commit -m ‘added .gitignore file’

As you can see, I have 2 commits now in my master branch.

$ git log –oneline

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.

So, add a remote Git repository URL as follows:

$ git remote add origin git@git.sxi.io:~/cherry-pick-demo.git

Now, push the master branch to the remote Git repository as follows:

$ git push origin master

Now, let’s say bob wants to contribute to the project. So, he cloned the Git repository on his computer.

$ git clone git@git.sxi.io:~/cherry-pick-demo.git myproject

Now, bob navigates to his project directory.

$ cd myproject/

He also has the 2 commits that I’ve added.

$ git log –oneline

Now, bob creates a test branch to try out his ideas.

$ git checkout -b test

He decides to change the return value with a constant EXIT_SUCCESS from the stdlib library.

He adds the changes to the staging area.

$ git add .

Commits the changes.

$ git commit -m ‘used EXIT_SUCCESS instead of 0 as the return value’

Now, he decides to use a function printMessage() to print the message. So, he writes the function.

He commits the changes again.

$ git add .
$ git commit -m ‘added printMessage() function’

Then, bob uses the function in the program.

He commits the changes again.

$ git add .
$ git commit -m ‘used printMessage() function to print the message’

Now, bob has the following commits in the test branch.

Now, bob pushes the test branch to the Git remote repository.

$ git push origin test

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.

$ git fetch

Now, you see a new branch origin/test.

You also found 3 new commits that bob made.

$ git log –oneline origin/test

Now, you would like to know what changes bob made.

$ git log -p origin/test

You decide to not replace the return value with EXIT_SUCCESS as bob did.

You like the concept of using a function to print messages.

You like this commit as well.

So, you want to merge 2 out of the 3 commits bob made. If you’ve 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.

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.

Before, I cherry picked, the main.c file looks as follows.

Now, let’s cherry pick the oldest of the 2 commits, 9a4e532 as follows:

$ git cherry-pick 9a4e532

A merge conflict! This can happen.

Now, open the main.c file and fix the merge conflict.

The final file should look as follows.

Now, add the changes to the staging area.

$ git add.

Now, commit the changes as follows:

$ git cherry-pick –continue

NOTE: You can also use git commit here as well. It’s up to you. I prefer git cherry-pick –continue as it will automatically use the commit message from the commit I am cherry picking.

Now, type in your commit message here and save the file.

A new commit should be added.

Now, cherry pick the next commit as follows:

$ git cherry-pick 08ba5e7

No merge conflict. Great! A new commit should be added automatically.

As you can see, I get exactly what I wanted. I only merged the commits that I needed.

So, that’s how you cherry pick in Git. Thanks for reading this article.

Total
0
Shares
Previous Post

Python: Vectors, Matrices and Arrays with NumPy

Next Post

Jinja 2 Templates

Related Posts

What is Git?

What is Git? Today most software development projects are collaborative efforts. These projects can have hundreds or thousands…
Read More