Cherry pick is re-applying changes of a selected commit into the current HEAD.
It’s often use when we want to apply a patch from a branch into the other branch, where merge or rebase is not preferred.
For example, there is a product with both version 2.x and 3.x in active development. Assume that the team is actively working on 3.x and maintaining 2.x with latest security patch.
If now the developer team just update a security patch to 3.x on a component that shared between both versions. We want to apply the same patch to the version 2 too. Since the branch for version 3 and 2 is diverse, merging them is not possible way. We can apply just the patch to version 2 by cherry pick.
Scenario: Now we just created a security patch as a commit in branch v3. We want to apply this commit on v2.
- Use git log to find out the Hash of the commit. Assume it is
- Checkout branch of version 2
git checkout v2
- Use cherry-pick to apply the patch onto the current branch, which is v2.
git cherry-pick abc1234
This command create a new commit on v2, with the content and comment exactly the same as
abc1234. It is a clone of
abc1234, but applied to a different branch, a different place.