Introduction to GIT Cherry-pick
In this article, we’re going to learn about the GIT Cherry-pick in detail. There are many programmers working on the same software development from different corners of the world. Then how to manage the codes? How they will make others understand what changes they have made? How to commit the codes and maintain different versions? How to merge the codes?
To solve these problems, GIT came into the Development world. GIT is an outstanding Source Code Management [SCM] and Distributed Version Control System. GIT was created by Linux Torvald, the person who developed the Linux kernel. Obviously, it is an open-source tool where every programmer can contribute to building a piece of software from anywhere in the world.
GIT has many features. It may have multiple branches. A developer can write codes after creating his own branch in the local system and merge it with the master branch or other branches of the remote GIT repository.
What is GIT Cherry-pick?
Imagine, project work is going on to write a script on the history and evolution of cell phones. So, there are many people working on the same project and all are working separately. However, in the end, everyone’s script will be compiled together.
Now, member A is writing about Apple phones and suddenly realizes it can be better. So, he informed the matter to the other team members who are working on the same project. Another member X told him he is writing a script on Android phones and asked member A to have a look.
Then member A looked into the teammate’s script and found that some of the portions are the same with some changes which are really good. Therefore, he cherry-picked those changes and pasted in his own script. This is the same what cherry-pick called in GIT in the matter of software coding industry.
4.8 (3,610 ratings)
Git-cherry-pick is a powerful git command and cherry-picking is a process to pick up a commit from a branch and apply it to some other branch. In simple words, there can be multiple branches where developers commit their codes. Now, one developer supposed to commit his codes in branch A, however, he committed the codes in branch B by mistake. That time cherry-pick may switch back the commit to the correct branch.
Use below command [in Unix system] to know different options for git-cherry-pick,
The syntax for cherry-pick command,
git cherry-pick [–edit] [-n] [-m parent-number] [-x] <commit>
When we Use GIT Cherry-pick?
Git-cherry-pick is a useful tool however not a best practice for all the time. git-cherry-pick can be used in the following scenarios,
- To make it correct when a commit made in a different branch accidentally.
- Preferable traditional merges
- To apply the changes in an existing commit.
- Duplicate commit
- Bug fixing
How GIT Cherry-pick works?
A defect found in code in the Production environment and a fix needs to be implemented. Once the change implemented and a defect is fixed, now it is time to bring this code change back in the Development environment so that the defect will not occur again and again in the Production environment in the future.
The first option is a simple git merge and it is an ideal solution if it works. However, there are other changes done in the Production environment also and those can not be brought back to the Development environment while merging. And in that case, cherry-pick is the correct option.
Cherry-pick brings that commit which was made for bug fix only. It does not pick the other commits.
Here is an illustration,
Fig 1: G and H are the Production branch commits. A to F Development branch commits. A problem is found in the Production branch. A fix is developed in H commit which needs to apply in the Development branch but commit G is not required to be applied.
Fig 2: Now commit H is cherry-picked on the Development branch and resulting commit is H’. Commit G changes are not included in the Development branch.
How to Use GIT Cherry-pick with Example?
Suppose we have two branches [master and new_feature] [command used to see the branch-git branch]
We made the commit [Highlighted] in the new_feature branch by mistake. [command used to see the committed logs-git log]
However, it supposed to be in the master branch only. First copy the highlighted SHA in a notepad.
Now we will use a git-cherry-pick command to move this commit to master branch however before that we need to switch to master branch [command used to switch the branch-git checkout <branchname>]
[command used git–cherry–pick <commit id>] [same SHA should be paste which was copied to notepad earlier with git cherry-pick command]
Now we can see the same commit is available in master branch [command used-git log]
For more example of git cherry-pick commands, please see the below link,
Important Things to Remember
Three things need to remember while using cherry-pick and working in a team.
1. standardize commit message: It is better to use standardize commit message and -x if we cherry-pick from a public branch.
git cherry-pick -x <commit-hash>
It will avoid merging conflict in the future.
2. copy over the notes: Sometimes some of cherry-pick have notes and when we run cherry-pick, the notes do not get copied. Therefore, it’s better to use it.
git notes copy <from-commit-hash> <to-commit-hash>
3. Cherry-pick multiple commits when they are linear only: We want to cherry-pick multiple commits like G, H [Fig 1], if they are linear, then only use the below command,
git cherry-pick G^..H
Suppose we want to pick up a specific commit from a different branch and apply to the current branch, here are steps recommended,
1. Find the commit hash that needs to be cherry-picked first.
2. Go to the destination branch.
git cherry-pick -x <commit-hash>
Resolve the conflicts if they happen. If there are notes in the original commit, those need to be copied.
git notes copy <original-commit-hash> <new-commit-hash>
This is a guide to GIT Cherry-pick. Here we discuss its working and how to use git cherry-pick with examples in detail. You may also look at the following articles to learn more –