How to Cherry-Pick Merge Commit in Git

Abdul Jabbar Feb 02, 2024
  1. the git cherry-pick Command in Git
  2. Cherry-Pick Merge Commit in Git
How to Cherry-Pick Merge Commit in Git

When multiple developers of the same team are working on a project, whether that project is big or small in complexity, it becomes very difficult to handle and manage changes between Git branches.

Occasionally, we need some particular commits to be combined into another branch rather than combining the whole branch, and for this purpose, we use an essential tool in Git that is popularly known as Cherry Picking.

Cherry Picking is a good tool to use when picking only one commit from a whole list of commits in a repository branch.

the git cherry-pick Command in Git

git cherry-pick is an influential command that allows random Git commits to be moved from one branch in a repository and adds to the repository’s current working HEAD. This command is also beneficial for undoing changes related to the past development done by the team.

For instance, if we accidentally made a commit to the wrong branch, we will shift to the correct branch and cherry-pick the commit to the right place where it should belong in a new branch of the repository.

All the commits from a branch are mixed when working with git merge or git rebase. While the cherry-pick command enables us to pick selected commits for assimilation.

Cherry-Pick Merge Commit in Git

Cherry-picking a merge commit is not as easy as it looks. When we execute the merge procedure, we work with two branches at a time of the same repository.

For instance, a branch parent-B is merged into the parent-A branch using the Git commands.

So when we are cherry-picking the specific commit that was a merge with two parents, we need to tell the cherry-pick command which one against which the diff should be used by using the -m option. We will mention the specific parent branch with the commit hash in the following command.

The syntax for the above situation is explained as follows.

git cherry-pick -m <commit hash>

Let’s explain the situation with the following example of the above command.

M - N - O - P - Q (branch A)
     \     /
      R - S - T (branch B)

As shown above, P is the merge commit. If we run the git cherry-pick command, Git will pass a warning about the command.

So now we have to mention the parent branch number with the commit id. It will be performed in the following command.

git cherry-pick -m 1 P

This cherry-pick command will take all commits from O- P from the above example.

We can also perform the above action by using the below command.

git cherry-pick -m 2 P

This will pick all the commits from R- S- P from the above example.

Note
Cherry-picking a commit in a branch with the merge commit command is too harmful, so while working with it, always proceed with caution. There is a high risk of losing the history of the commit done in the past.

It disrupts all the changes we made in the parent, and we didn’t define to -m into that one commit.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Cherry-Pick