Git cherry picking

Cherry picking is a useful command to apply only certain commits. In this article, you will learn how to use this useful command.
What is cherry picking?
Cherry picking means selecting specific commits and applying them to your current branch. These commits can be made on other branches and can be applied to your current branch. All you need is the commit sha of the commit you want to cherry pick. To get the commit sha you can use the git log or the git reflog command.
For the git log command, the result might look like this:
For the git reflog command, the result might look something like this:
To cherry pick a commit you then need to use the git cherry-pick command:
Your cherry picked commit will then be applied to the HEAD of your current branch.
Cherry picking by applying the commit as is
Cherry picking a commit will create a commit by default. Let’s say we made the commits A, B, C, D, and E on one branch and the commits F, G, H, and I on the other:
After cherry picking commit D on the second branch, the result will look like this:
The example above could have been resulting from the following command:
The commit is then taken as is, i.e.
- a new commit is created on your current branch
- the same commit message will be used.
Cherry picking without creating a new commit
You can omit creating a commit by adding the -n option. In this case, only the content of the cherry picked commit will be applied to your current HEAD. You will then be able to include these changes in an upcoming commit. The command, to achieve this, looks like this:
Cherry picking with a new commit message
When cherry picking a commit, you can write a new commit message for the cherry-picked commit. You can do so by passing the -e option. The command to do so, looks like this:
Cherry picking while being able to track where the commit comes from
In general, you won’t be able to track where the cherry picked commit comes from. If you pass the -x option, you will see where the cherry picked commit comes from. The command, to achieve this, looks like this:
The result will look like the following (pay attention to the last line):
Resolving merge conflicts when cherry picking
When you are cherry picking a commit, merge conflicts can occur. Git will show you a message like this:
When using the Git status command, you will see something like the following:
With this output, Git tells you what to do in case that you want to continue cherry picking:
- resolve the conflicts
- run git add command
- run git cherry-pick --continue
In case, that you want to abort the cherry picking process, you can run
When to use cherry picking?
You can use cherry picking when you want only a specific commit to be applied. An example would be a commit for a bugfix which has been applied on the production branch. You then might want to have the bugfix to be applied on your development branch too, to not re-introduce it in another deployment.
What is the difference between cherry picking and merging?
By cherry picking a commit, you only apply the commit to your HEAD. The previous commits are not applied. When merging a specific commit (i.e. by using git merge commitSha), all commits done before the specific commit will be applied.
Reference
For full reference see Git cherry-pick documentation.