Git Cherry-Pick

Git Cherry-Pick

In Git, the cherry-pick command is used to make the target commit changes and place them on the HEAD of the currently checked-out branch. It is done to either continue to work on the changes made in the working directory or simultaneously commit to the changes onto the new branch.

The main objective of cherry-picking in Git is to combine or apply the changes made by some previously existing commits. The main function of cherry-picking is to check and commit the repository history and frequently update the changes that were once the part of the last commits made in the present working tree. It may seem unclear to cherry-pick a commit or even a branch since it is handled internally.

It is a tool that may not always seem like the most used. It is used quite less since it can cause redundancy with commits in some scenarios. It may not be suitable to use cherry-pick in that particular situation. It can only be used for a situation involving the above requirements or can be used instead of merge and rebase commands.

Git Cherry-Pick

Note: The case with a cherry-pick is that if an individual accidentally commits to a wrong branch. It allows him/her to get those changes onto the correct branch without spending much or redoing any work.

Scenarios

Committing a wrong branch

Whenever there is a commit made in the wrong branch, cherry-pick seems a great option to revert the changes. To revert the changes without redoing any work, you need to create and check out the destination branch and pass the second argument with checkout reference. The branches don't share the same commit history. Cherry-pick at this moment resolves the incorrect commits, and you can switch back to the previous branch and reset the wrong commits. It is done by using the following commands.

 $ git checkout -b destination-branch good-reference
 $ git cherry-pick 12345
 $ git checkout -
 $ git reset --hard HEAD~1 

These commands resolve the conflict of committing to the wrong branch and reset the previous commits.

Team member proposed changes

Cherry-picking is also used to rectify and make changes proposed by other team members. If a team member finds a feature that can be added to the main project, he can take the changes into account and suggest it for merging. It can be cherry-picked after taking sufficient reviews on the workspace.

Suppose a team member asks to merge only specific commits from a branch into a current branch, and you may want to do this by merging changes that you need immediately. It can be done selectively without affecting the other codes. To do this, you need to check the logs and pick up the changes you exactly want to be merged. Therefore, cherry-picking can be done by identifying the branch's commit ID as seen from the logs, and to do this, we can use the following commands.

$ git log      // It gives you the status of all branches available

For example,

Consider that you have written some code in the commit gfk786b2 of the feature branch, which seems very important to you. It may contain the solution to your bugs and conflicts that may be solved. It is necessary to be fixed so that other members can access it immediately. Here, the cherry-pick comes in handy, and in this case, gfk786b2 is the cherry, and you want to pick it. This can be done by using the following commands.

$ git checkout release
$ git cherry-pick gfk786b2

By doing this, you have now applied or cherry-picked to the master branch and committed to the release branch. It will now behave just like merge.

Sometimes, if the cherry-picking is not enough if you want to pick some consecutive commits. For this, you need to create new a new branch from the feature at the last commit and then rebase your branch into the master branch. This can be done by using the following commands.

$ git checkout -b your_branch_name “ID”

Example:

 $ git checkout -b myBranch 0xdfa8
 // rebasing
 $ git rebase --onto master oxdfa8 

Uses of Cherry-pick

* Used to collaborate.

* Bugs fixing and testing in the development phase.

* Revert the previous changes.

* Rectify useless or less-used commits.

* Merges full branch in case of different versions.

* Accessing changes introduced to sub-branch without altering the main branch.