What is GIT cherry pick?
Today, that's not a problem to be a powerful and professional developers team and work in the different corners of our world. Developers will immediately be surprised to ask, how to manage the code, new changes, fixes? Git already took into account all these important aspects and today no one developer cannot imagine his/her life without powerful GIT. Its feature will significantly improve the quality of your teamwork. 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. This article will be helpful for software developers, who would like ideas on how best to organize their project in a Git. Cherry-pick almost sounds like it basically applying the changes introduced by existing commits onto two branches that you have and this is often used for things like hotfixes if you pull some stuff and the current state of your repository is broken and somebody what to fix some issue by adding some commit from somewhere.
When to use cherry pick Git
When working in multiple branches you may find that a commit in another branch can help to complete your work in your branch. This is unusual and can happen in a team and even when working on your own. A good example of a commit you may need is a bug fix, this is where Cherry-picking comes very useful.
You’ll find the cherry-pick command useful when you need to copy changes made in a specific commit and then create a new commit on a branch, that would contain the copied changes. Respectively, to fully apply the corresponding changes from provided commit, there most likely would be a need to resolved appeared conflicts. This is the same situation as with its operations that introduce changes via the process of applying a diff. Thus, this can be averted when using cherry-pick in git.
How to cherry pick a commit from another branch git?
Let’s imagine that we have a repository that having two branches and master feature/one.
Master branch has one commit and feature has 3 new commits. For example, we are going to commit C3 from feature/one applied it to master. We are not picking all new commits since feature/one, but I’m tested only in a specific commit in feature/one
That's why we use cherry-pick. If you are going to pick all commits from feature/one into master then you will skip much. We going to run git cherry-pick on muster by pointing to C3 and that comes it is applied to the master.
Is a wonderful and easy tool to be executed as follows:
git cherry-pick <commit-hash>
Firstly, let's ensure that we work in the master branch:
git checkout master
The provided example above: <commit-hash> is a commit reference. You can use git log to find a commit reference.
The next step is the execution of the git cherry-pick С3 to simply copy C3 commit from Feature branch into master:
The С3 commit has been successfully picked into the feature branch.
How to revert a cherry pick git
Yeah, it’s also easy-peasy. Copy the SHA of the cherry-pick you want to REMOVE (pay your attention, please copy NOT the SHA of the last commit you want to keep):
git rebase -p --onto SHA^ SHA
git rebase - this is an "automated" cherry-pick. This command perform the same job, but for a chain of commits, thereby moving the branch to a new location.
git revert - the exact opposite of git cherry-pick. It creates an "anti-commit" for the specified commit, thus canceling the changes made to it.
- In case the project workflow allow using the special tools, why not? In case, you would like to select more than one commit at once, you can use git cherry-pick command and add the commit identifications, separated by space, for example: git cherry-pick 001a 001b
- You can cherry-pick a merge instead of a commit: git cherry-pick -m 1 <merge_hash>. It is preferred to use git merge instead of git cherry-pick. When you cherry-pick a merge commit, it collapses all the changes made into that one commit. But please notice, there is a risk to lose the commit history.
We think at the end of this article, you would like to argue with us and appeal by facts that it's much better to use git merge instead of cherry-picking, the main reason is that Git cherry-pick produces commits duplicates and because of that you are not able to track the commit history. Maybe yes, and maybe no. Everyone chooses for themselves and we declare, that you have to look at this unique Git feature! Share your experience with us.
Please overview the Git documentation to deeper dive into the magic world of the Git tools such as git cherry-pick.