Git

Git Cherry Pick Example

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.

Usage of 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.

Create Directory for initializing Git Branch
Create Directory for initializing Git Branch

And in this directory we shall be initializing Git repository using git init command.

Initialize Git repository
Initialize Git repository

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.

Commit file to master repository
Commit file to master repository

Now we shall create a new feature branch using command git branch <branch name>. Available branches can be checked using command git branch.

Create a new branch
Create a new branch

After our branch has been created out of master branch, we shall switch to it using git checkout <branch name> command.

Switch to new branch
Switch to new branch

In this branch let’s do some two or three changes and each change followed by a commit.

Changes made to repository, each change followed by commit
Changes made to repository, each change followed by commit

Now, we shall use command git log while on feature branch and then again after switching to master branch.

git log on feature and master branch
git log on feature and 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

git cherry-pick
git cherry-pick

Merge done using command git cherry-pick can be verified using command git log on master branch.

git log on master branch
git log on master branch

3. Conclusion

In this example we learnt usage of git cherry-pick command that is used to merge specific commit from another branch to current one.

Saurabh Arora

Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.
Back to top button