Patricia Sauer - Authentic Programming

Git cherry picking

A bowl containig cherries

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:

commit 61b04017a359339d191143cc5a2269bf06acd9e2 (HEAD -> master, origin/master, origin/HEAD)
Author: xyz
Date:   Sun Aug 18 22:03:19 2019 +0200

Implemented some stuff

For the git reflog command, the result might look something like this:

61b0401 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: Implemented some stuff

To cherry pick a commit you then need to use the git cherry-pick command:

git cherry-pick insertCommitShaHere

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:

The commits before cherry picking

After cherry picking commit D on the second branch, the result will look like this:

The commits after cherry picking

The example above could have been resulting from the following command:

git cherry-pick 6b2a3f2d5140d2f8b9871dc607f0599555bf0ee1

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:

git cherry-pick insertCommitShaHere -n

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:

git cherry-pick insertCommitShaHere -e

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:

git cherry-pick insertCommitShaHere -x

The result will look like the following (pay attention to the last line):

commit 7dae9c14c7e411b926adb280a19834ce0b6e35d7 (HEAD -> cherry-picking)
Author: xyz
Date:   Mon Aug 19 09:30:11 2020 +0200

    Implemented feature xyz
    
    (cherry picked from commit 6b2a3f2d5140d2f8b9871dc607f0599555bf0ee1)

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:

error: could not apply 6b2a3f2... Implemented feature xyz
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

When using the Git status command, you will see something like the following:

On branch cherry-picking
You are currently cherry-picking commit 6b2a3f2.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
    both modified:   file3.txt

no changes added to commit (use "git add" and/or "git commit -a")

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

git cherry-pick --abort

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.

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer