GIT CHERRY PICK

What is cherry picking? Cherry picking is to choose a commit from one branch and apply it to another. The command used is git cherrypick , and here we actually use forward- or back-port commits from a maintenance branch to a development branch.cherry-pickcommand in git allows us to copy only one commit at a time.

Steps on git cherry-picking:

  1. 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
  2. Then run the cherry-pick command : git cherry-pick <commit-hash>
  3. 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.
  4. 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>
  5. A range of commits : git cherry-pick start_commit~..end_commit
  6. Multiple commits (non-sequential) : git cherry-pick commit_1 commit_2

 Sequencer Subcommands

Cherry-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. Problem Is Found On Production 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.

Results In Commit H

What git cherry-pick does? 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 :3

  Consider 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,

Figure: 4

fig:b
And if you typed,git cherry-pick C D E then the graph can be as,

Figure: 5

From this graph, we can infer that git has copied changes made in one place, and replayed them somewhere else.
 If H is in the production and we are taking the difference  from B to C and apply to H, the C prime (C’) can be obtained to H, same way after cherry-picking  D and E obtains  D’ and E’, and this can be diagrammatically represented as in the Figure: 5
 
]]>