Git

What is the Git Cherry-Pick and How to Resolve Conflicts?

While working on a large team project, developers want to apply some changes to other team members’ projects in their main project. It becomes so complex to apply those changes when it is not required to merge whole branches, and they need to apply only some commits to another branch. In this situation, they perform the cherry-pick operation.

This write-up will illustrate:

What is the Git Cherry-pick?

Git “cherry-pick” means copying or choosing the commits of one branch and putting them into another target branch. The cherry-pick command helps users to get one branch’s changes into another Git branch without redoing work. However, they usually encounter conflicts while performing the cherry-picking operation for some reason.

How to Resolve Git Cherry-pick Conflict?

To resolve the cherry-pick conflict, check out the following steps:

  • Redirect to the local repository.
  • View the commits history and selects the desired commit.
  • Navigate to the target branch.
  • Apply cherry-pick operation.
  • Resolve conflict by aborting the cherry-pick operation.

Step 1: Navigate to Particular Directory

First, switch to the desired local directory utilizing the below-listed command:

$ cd "C:\Git\RepoQ"

Step 2: Check Git Log

Next, view the list of commits in the current branch:

$ git log --oneline

The below-screenshot displays the commits made in the repository. Choose the desired commit id. For instance, we have selected the “6d173e0” commit hash:

Step 3: View List of Branches

Display the list of available branches in the current repository using the below-stated command:

$ git branch

In the below image, it can be seen that the repository contains two branches. Select the target branch and switch to it:

Step 4: Switch to Target Branch

Then, execute the provided command along with the target branch name and navigate to it:

$ git switch alpha

Step 5: Apply “cherry-pick” Operation

Next, cherry-pick the particular commit by running the following command:

$ git cherry-pick 6d173e0

According to the below-provided output, a conflict occurred, and the cherry-pick operation could not perform:

Note: The above-stated conflict occurred because the commit that we specified for cherry-pick operation, has been deleted.

Step 6: Resolve Conflict

Finally, run the “git cherry-pick” command with the “–abort” option to abort the cherry-pick operation:

$ git cherry-pick --abort

The below-provided image indicates that the conflict has been resolved successfully:

We have explained about Git cherry-pick operation and how to resolve Git cherry-pick conflict.

Conclusion

Git cherry-pick” means copying or choosing the commits of one branch and applying them to another target branch. Sometimes, users encounter conflicts while performing the cherry-pick operation. To resolve the cherry-pick conflict, abort the cherry-pick operation by executing the “git cherry-pick –abort” command. This write-up illustrated about Git cherry-pick operation and how to resolve the Git cherry-pick conflict.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.