Multiple Commits Using Cherry-Pick in Git

Abdul Jabbar Jan 03, 2023
  1. Difference Between Git merge and Git Cherry-Pick Command in Git
  2. the Cherry-picking Bug-Fix Commit in Git
  3. Multiple Cherry-picking Commit in Git
  4. Use Rebase Command to Pick Desired Changes on a Specific Branch in Git
Multiple Commits Using Cherry-Pick in Git

After completing our branching work, we usually do not need to merge into the existing files.

We often need to get a few particular commits from different branches rather than deal with all of them in a particular branch. That’s the reason why the git-cherry pick command is used in Git.

Difference Between Git merge and Git Cherry-Pick Command in Git

git merge is just as git cherry-pick command. Git cherry-pick command is used to assimilate changes from another branch to our desired branch.

While using the merge command, we are typically allowed to assimilate all the changes done in the other branch into our desired one.

But on the other hand, sometimes we only need a few changes in our desired branch. And for this purpose, the Git cherry-pick command is the safest option.

the Cherry-picking Bug-Fix Commit in Git

Cherry-picking can typically be explained as updating a bug fix to the former version of our software, which means that to fetch only some explicit bug fix commits from hundreds of bug fixes in a particular branch.

Then use that particular bug-fixed commit in the future for any reason. This statement or command is mostly used when the team wants to fix some important bug in the production or live web or desktop application.

Using this command, the team prepares the release and deploys it into the production or live environment.

Multiple Cherry-picking Commit in Git

For some cases, we want to apply to cherry-pick on multiple commits simultaneously in different branches. Then we have to identify both commits first and use the following commands on it.

After that, cherry-picking can be done on the multiple commits using the dot notation between both A and B branches as shown below in the following command:

$ git cherry-pick A..B

After applying this command, commit A will not be assimilated into the cherry-pick.

For assimilating commit A also, we can use the following syntax:

$ git cherry-pick A^..B
Note
Commit should be placed in the perfect order. Commit A should be older than commit B. If this won’t be in perfect order, the command used will fail, and we need to use it according to the condition stated above.

Use Rebase Command to Pick Desired Changes on a Specific Branch in Git

We have another way to pick the desired changes or commits in a specific branch using the rebase command in Git. We can rebase the command with the option onto in this method.

We can use git rebase --onto in two types of cases:

  • The First condition is that we have a branch that wants to replace its parent branch in a repository.
  • Secondly, we want to immediately remove some commits from our present branch in a repository.

Let’s suppose that our branch is the branch that finishes at D, and this is the desired branch that we want to move C-Y onto.

git reset --hard Y

git rebase --onto A B

It helps us rebase starting from the particular point or committing to the desired commit. We can manage completely what to rebase and where to rebase in a specific branch commits.

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 Commit