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:
Now, navigate to the repository as follows:
Now, create a main.c file with the following contents:
Now, add the file to the staging area as follows:
Now, commit the changes as follows:
Now, create a .gitignore file with the following content:
Add the file to the staging area.
Commit the changes:
As you can see, I have 2 commits now in my master branch.
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:
Now, push the master branch to the remote Git repository as follows:
Now, let’s say bob wants to contribute to the project. So, he cloned the Git repository on his computer.
Now, bob navigates to his project directory.
He also has the 2 commits that I’ve added.
Now, bob creates a test branch to try out his ideas.
He decides to change the return value with a constant EXIT_SUCCESS from the stdlib library.
He adds the changes to the staging area.
Commits the changes.
Now, he decides to use a function printMessage() to print the message. So, he writes the function.
He commits the changes again.
$ git commit -m ‘added printMessage() function’
Then, bob uses the function in the program.
He commits the changes again.
$ 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.
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.
Now, you see a new branch origin/test.
You also found 3 new commits that bob made.
Now, you would like to know what changes bob made.
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:
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.
Now, commit the changes as follows:
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:
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.