Understanding Git Cherry-pick and how it is useful

Git Cherry-pick is used by many Organizations as an alternative to the rebase and merge. Cherry-pick is used to copy selective commits from one branch to another branch. Unlike a merge or rebase, cherry-pick only brings the changes from the commits you select, instead of all the changes in a branch. You can also choose whether to apply only one commit from another branch or a number of commits from another branch.

Why Git Cherry-pick

Cherry-pick is a great way to tackle below common problems:

  1. Accidentally committing on the wrong branch. In this case, one can use git Cherry-pick to apply the change(s) over to the correct branch and then reset the original branch to the previous commit.
  2. Pulling out a set of commits made in a feature branch so you merge them back to your master branch sooner.
  3. Porting in specific commits from the master branch without rebasing your branch. This can be useful to bring certain hotfixes to your branch without bringing whole branch.
  4. Porting common changes in incompatible branches in the source repo. One can use commit id for common change and then apply on all the branches one by one.

How to Do Cherry-Pick in Git

Let’s consider below commmit history for all branches in one of our source code repo created for this post:

viewing git commit history of all branches

We can see that we are currently on the branch newQuickFix and it is two commits ahead of the master branch. Those two commits are:

  • 8afc7ce Added Class10.cs
  • 25fd5e2 Added Class09.cs

Let’s say we needed to apply only the commit 8afc7ce to our master branch. To do the same, first we need to checkout master branch:

git checkout master

And then run git cherry-pick command:

git cherry-pick 8afc7ce

applying git cherry pick to port one commit

Now we can see that our commit was not successful, because we have a conflict. So we only need to fix the conflict by usual process to complete the cherry-pick process. This would have also happened if we used git merge or rebase, so this has nothing to specifically with the cherry-pick itself.

fixing git cherry-pick conflicts

Another choice is to run git cherry-pick –-continue like rebase. It opens up commit comment editor.

If you need to cherry-pick a range of commits, you can use two commit IDs separated by … to specify a range in your history. For example:

git cherry-pick commit-01…commit-0n

If you need to cherry-pick number of commits, you can do that in below manner:

git cherry-pick commit-01 commit-02 commit-03

You can choose to use either partial commit hash or full commit hash.

How to do Cherry-pick in Visual Studio

Open up Team Explorer and checkout the branch you want to cherry-pick changes into using the Branches view. So we would checkout the master branch first:

checkout master branch in the visual studio

Right-click the branch containing the changes you want (In our case, it would be newQuickFix branch) and select View History:

Right click the branch to import commits from


Right-click the commit you want to cherry-pick and select Cherry-pick:

Right click the commit you want to apply

Visual Studio copies the changes made in that commit into a new one on your current branch. Again, we can resolve conflict inside VS itself, if there are any:

resolve merge conflict inside visual studio

And then complete the cherry-pick process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s