Cherry Pick in Git for Trunk-Based Development

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.

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

Apart from this Cherry-pick could also be useful in code collaboration.

Today in this article, we will cover below aspects,

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

Why cherry-pick?

In Git, you a cherry-pick for any of the below use cases.

These below characteristics 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
  • Cherry-pick with the intention of code reuse
  • Collaboration in a team with code sharing.
  • Reverting back to the previous best version (as it was a good code before someone introduced a bug ).

Picking up code from one branch to another branch is not dependent on the branching model following. You could follow,

  • TBD (Trunk Based Development)
  • GitFlow or any other legacy way of multi-branch type to support multiple branches.

Cherry-Pick in Git- Step by step

Trunk Based Development

Let’s understand the example above.

The above example might resemble Trunk Based Development but the concept we shall be learning can be applied to any approach.

If you are not following Trunk based branching, please refer to section @7 Perform cherry-pick directly which explains a simple 2 step process for performing cherry-pick from one branch to target the branch.

The above example shows that,

  • The project was released as per the plan for @3 commit.
  • The released project was tagged as @R (Code released on May 23)
  • A bug was reported requiring an immediate fix. ( Bug reported on June 5 )
  • The main branch already progressed in developing new other functionality with @4-5 and 6 commits.

When Code was released @3, it had only 1 file (ValuesController.cs) and now the code has 3 more new files as shown below.

There is a bug in the file ValuesController.cs that needs to be fixed.

This fix is urgent and needs to be applied to the release branch @R1


1. Check Out @ 3 commit

blank
  • Get the commit ID for a commit @3.

Ex. This commit ID is ‘914c9fac373ae3a27ce1025943664a0e04f550a4’

blank

Command :

git checkout - b [branch-name] [commit-id]
blank

Successful execution of the above command will bring the released codebase where we would be applying a bug fix.

Let’s modify the code now. The changes are highlighted below,

blank

@3B Apply Code changes for Bug fixing

blank

So you are all set with changes @3B in the working branch(br-cherry pick@3).

Next, we will merge these changes to master before cherry-picking @7 for the release.

(This step will not be needed if you are not following Trunk Based Development)

@ 3B Pull the latest code from master @6

It’s important to take the latest code from the master branch and make sure the changes or bug fixes are fine in our working branch (short-lived) before merging it back to the master or trunk.

blank

Once everything is working fine please perform staging and commit

@3B – Stage and commit bug fix file only

Stage your bug fix as shown below,

blank

Commit bug fix to the master branch as shown below,

Cherry Pick
Cherry Pick

Once done a pull request can be raised to merge changes to the master branch

blank

Merge your changes to master,

blank
blank

So finally we are @7 with the last commit.

@7 Perform cherry-pick

Performing cherry-pick is a two-step process.

  1. Check out the Target Branch where cherry-pick needs to be applied.

git checkout -b R1

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

blank

So let’s get the relevant commit ID

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,

blank
Git hash commit ID

The last commit ID is as below from master branch,

Cherry Pick GitHub

Command :

git cherry-pick -x 17784261c69c95649c24c86b52ca35a353e8504e
blank

Changes are successfully applied to the R1 branch,

Cherry Pick Git ADO

You are all set with cherry-picking and your bug fix is ready to be pushed to production release (•_•). I hope your code works best !!

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

Please sound off your comments below.

Happy Coding !!

Happy

Cherry Pick Git ADO

Picking !!

References:

Summary

Cherry-pick is a technique of picking up a commit from a branch and applying it to another branch. If used correctly Cherry-pick can address multiple use cases like bug fixing or patching purposes or un-doing the changes. Apart from this cherry-picking could also be useful in code collaboration.



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.



2 thoughts on “Cherry Pick in Git for Trunk-Based Development

Leave a Reply

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