Cherry picking

Suppose we are working on a new feature on its own branch, but there's a commit on that branch that we would like to have on another branch (say master), but we are not ready to merge the entire branch. This is where cherry-picking comes in. The cherry-picking tool allows us to grab a commit from another branch, and apply it to the tip of our current branch.

Cherry-picking

Cherry-picking 1
Cherry-picking 2

As an example, take a look at the git log. Here I've added two commits to the feature branch (f88ecdc and fc6e687), and one commit to the master branch (88c4524). Now suppose that we want to get the changes made in commit f88ecdc and apply them to the tip of the master branch. To cherry-pick this commit we run

git cherry-pick f88ecdc

Before cherry-picking commits, be sure to check your current branch. Your current branch should be the branch into which you want to bring the target commit.

Cherry-picking 3
Cherry-picking 4

If we take a look at the log we can see that all previous commits still exist, but the tip of the master branch is now 86bf257 which is a copy of f88ecdc.

During a cherry-pick, you might run into conflicts. These conflicts are no different than a merge conflict. Solve them using VS Code and then stage and commit your changes.