What is cherry picking?
Cherry picking is to choose a commit from one branch and apply it to another. The command used is git cherry–pick , and here we actually use forward- or back-port commits from a maintenance branch to a development branch.
command in git allows us to copy only one commit at a time.
Steps on git cherry-picking:
- If you are on a particular branch and you want to apply a commit to the master branch, then checkout to master branch :
git checkout master
- Then run the cherry-pick command :
git cherry-pick <commit-hash>
- If you cherry-pick from a public branch, then you should use this,
git cherry-pick -x <commit-hash>which will generate a standardized commit message.
- If you have notes attached to the commit they do not follow the cherry-pick, to bring them over as well, you have to use:
git notes copy <from> <to>
- A range of commits :
git cherry-pick start_commit~..end_commit
- Multiple commits (non-sequential) :
git cherry-pick commit_1 commit_2
Sequencer SubcommandsCherry-pick uses continue, quit and abort subcommands in git.The continue command is used for continuing the operation in progress using .git/sequencer information.It can be also used to continue operations after resolving conflicts in a failed cherry-pick.The quit command forget about the current operation in progress and can be used to clear the sequencer state after a failed cherry-pick.And finally, the abort command cancels the operation and return to the pre-sequence state.
Where can cherry-picking be applied?Whenever defect occurs in the production, fixation must be performed. Once the defect is fixed, you’ll want to bring this code change back to the development branch so the defect won’t be re-introduced into production with a future update.The first option is to consider whether a simple git merge would work. If a merge is possible, that would be the ideal solution. However, if there are other changes made on the production branch, shouldn’t be brought back to the development branch, a merge would not produce the desired result since it will include all the changes made to the production branch. Example: Figure 1: Problem is found in the Production branch. A fix for the problem is developed in commit H, but commit G does not need to be applied to the Development branch. Figure 2: Commit H is cherry-picked onto the Development branch, resulting in commit H’. Note that the changes made in commit G are not included on the Development branch.
git cherry-pickdoes? It takes a commit from somewhere else, and “play it back” wherever you are right now.It can be explained by an example given below:
Figure :3Consider you were at node H in this graph, and you typed,
git cherry-pick Eyou’d wind up with a copy of commit E—let’s call it “E prime” or E’—that pointed to H as its parent, and can be represented as,
git cherry-pick C D Ethen the graph can be as,