Cherry Pick as a word seems to be very interesting, literally cherry picking in git means to choose a commit from one branch and apply it onto another.
This functionality is in contrast with merge and rebase which normally applies many commits onto a another branch.
In this example we shall see example usage of
git cherry-pick command.
1. A brief on git cherry-pick
Many a times it happens that a version control user has to merge only specific commits from another branch into the current one. The reason why this might be needed is one wants to merge only specific changes, leaving other code changes that are not required behind. For this purpose
git cherry-pick comes into role.
git cherry-pick command as in git documentation is as follows:
git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] [-S] … git cherry-pick --continue git cherry-pick --quit git cherry-pick --abort
2. Git cherry-pick in action
To see this example in action, we shall start with creating a git repository.
First we shall create a directory for master branch.
And in this directory we shall be initializing Git repository using
git init command.
Here, we shall create a new file, and will then add and commit it to master branch using
git add and
git commit command respectively.
Now we shall create a new feature branch using command
git branch <branch name>. Available branches can be checked using command
After our branch has been created out of master branch, we shall switch to it using
git checkout <branch name> command.
In this branch let’s do some two or three changes and each change followed by a commit.
Now, we shall use command
git log while on feature branch and then again after switching to master branch.
In the logs, we can see series of changes made to feature branch.
As in the above image, let’s say we want to merge change with hash tag 090634c956d0c0c1f95875e85ff34a9f35f4a3ee where b.txt was added into master branch.
For this purpose we shall first switch to master branch and then use command:
git cherry-pick 090634c956d0c0c1f95875e85ff34a9f35f4a3ee
Merge done using command
git cherry-pick can be verified using command
git log on master branch.
In this example we learnt usage of
git cherry-pick command that is used to merge specific commit from another branch to current one.