What is Cherry Pick in Git – Guidelines and examples

what is cherry picking in git

Today in this article, we will see in detail what is Cherry Pick in Git.

Cherry-pick in Git (Version Control System) is a technique of picking up a commit from a branch and applying it to another branch.

Cherry-pick can address multiple use cases like bug fixing or patching purposes or undoing the changes.

Today in this article, we will cover below aspects,

Cherry-pick concept is common to services like GitHub, Git Lab, or Bit Bucket or using Azure DevOps services which are built on top of Git open-source VCS( Version Control System).

What is Cherry Pick in Git

Cherry-pick in Git (Version Control System) is a technique of picking up a commit from a branch and applying it to another branch.

In Git, you do a cherry-pick for any of the below use cases. This characteristic could be any of the below and not limited to,

  • Picking a commit and applying it at the appropriate place typically a branch.
  • Bug Fix/Patch on a released branch.
  • Undoing the changes previously committed causes issues
  • Cherry-pick with the intention of code reuse.
  • Collaboration in a team with the intention of code sharing.
  • Reverting to the previous best version of code (as it was a good code before someone introduced a bug).

Perform cherry-pick

Performing cherry-pick is a two-step process.

Step 1 – Checkout branch

Check out the Target Branch where cherry-pick needs to be applied. You can reuse the existing branch also if needed. If you check out an existing branch then cherry-pick can be applied the to target branch

git checkout -b R1

Step 2 – Perform cherry-pick

Do a cherry-pick for a given commit hash ID. One can pick a commit ID from any branch to any branch.

Cherry Pick in Git

So let’s get the relevant commit ID hash.

As we know @7 the code has been checked into git with a commit ID.

So we shall use the same commit hash ID and cherry-pick it as shown below,

The last commit ID is as below from the master branch

Or

you can use the complete commit ID,

cherry pick multiple commits from different branches

git cherry-pick [commit hash-id]

Example

git cherry-pick -x 17784261c69c95649c24c86b52ca35a353e8504e 

Cherry Pick in Git – Cherry pick commits from another branch

Cherry pick commits from another branch are similar and you just need to provide the proper commit hash ID with the command.

Command :

git cherry-pick [commit hash-id]

Examples:

git cherry-pick -x 17784261c69c95649c24c86b52ca35a353e8504e 

Or

git cherry-pick -x 1778426

Cherry-pick multiple commit ID

Command:

git cherry-pick [commit hash1] [commit hash2] [commit hash3]

Examples:

git cherry-pick 12caab 67qwtr 78tyer

how to do cherry pick in git

After successful execution, the code based on the above commit ID will be integrated and applied to your existing code base i.e R1 branch.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *