Git Cherry-Picking with VS Code -Part 1

Published by on

Hi Geeks,

In this blog, you will learn what is cherry-picking in Git and how to cherry-pick in Visual Studio code. If you want to pick some cherries keep reading.

So let’s talk about what is Git Cherry-pick

Git cherry-picking means select to pick a commit or multiple commits if you want to from one branch and apply that commit to another branch and this process is called git cherry-picking.

For example, let’s consider the following real-time situation, your project has two branches one is a Release branch and another one is a development branch. The release branch is currently pointed to the production(I mean this branch is already released to your customer/client) and you are adding new features and bug fixes in your development branch. One day your client wants a particular feature or bug fix that is already fixed/implemented in your development branch in production, then in this situation, you can use cherry-pick. Off source, you can directly do that fix on your Release branch but is not a good approach because you already did the same fix on the development branch.

How does cherry-pick work

Before Cherry-picking, Assume that you have 2 branches Release( Main branch) and the development branch. The release branch has 2 commits (A &B), similarly, C, D, E, and F are commits of the development branch.

Before Cherry-pick- Branching structure

Next step you want to get Commit E from the development branch to your Release branch. So you need to checkout to Release branch and execute the following code

git cherry-pick <commit-hash of E>

For example, If your commit id of brach E is 32fgse3232yd33ee3576jfre636, then the command is

git cherry-pick 32fgse3232yd33ee3576jfre636

Cherry-pick Multiple commits

git cherry-pick <commit-hash-id-of-1> <commit-hash-id-of-2> <commit-hash-id-of-3>

You can check out below link to know about how to find the commit hash of brach in the git

Before Cherry-picking

After performing the above cherry-picking command, the structure looks like

After the Cherry-picking

Important Note

When you applying to cherry-pick user point of you are applying the same commit(Commit E) to another branch (Release branch) but technically it’s a different commit because here In the example Commit E in Release branch is pointing to commit B. You can test this it’s pretty simple you could also select multiple commits instead of one by that it will make multiple commits for each of them.

How to cherry-pick without commit

A scenario like if you want to bring changes in but don’t want to commit it straight away, maybe you want to add some changes before commit. for doing this just add -n at the end of the cherry-picking command which is stands for no commits. Example

git cherry-pick <commit-hash of E> -n

Happy Coding!


Leave a Reply