What Does Cherry-Picking a Git Commit Mean?

Cherry-picking a git commit means to select a specific commit from any branch and applying it to another. For example, to cherry-pick a commit with the hash "3ba53ff050ef253058088eff5" to a branch called "foo", you would do the following:

# 1: switch to the target branch
git switch foo

# 2: cherry-pick commit and apply it to target branch
git cherry-pick 3ba53ff050ef253058088eff5

When you cherry-pick a git commit, the following is true:

  • The commit in the source branch is not removed;
  • A copy of the commit with the same changes and the same commit message is created in the target branch;
  • The history of the target branch is not altered, but rather added to, as a new, distinct commit is created.

Cherry-picking is useful in certain situations, even though it may not always be a best practice (as it can result in duplicate commits).

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.