How to Cherry Pick Multiple Commits in Git


By squashlabs, Last Updated: October 27, 2023

How to Cherry Pick Multiple Commits in Git

Cherry picking is a useful feature in Git that allows you to select specific commits from one branch and apply them to another branch. This can be helpful when you want to selectively merge changes or apply bug fixes from one branch to another. In this guide, I will walk you through the process of cherry picking multiple commits in Git.

Step 1: Identify the Commits to Cherry Pick

Before you can cherry pick multiple commits, you need to identify the commits you want to pick. You can do this by using the git log command to view the commit history of the source branch. Note down the commit hashes or any other identifier that uniquely identifies the commits you want to pick.

Step 2: Create a New Branch

To avoid making changes directly on your main branch, it is a good practice to create a new branch to cherry pick the commits onto. You can create a new branch using the following command:

git checkout -b <new-branch-name>

Replace <new-branch-name> with the desired name for your new branch.

Step 3: Cherry Pick the Commits

Once you have identified the commits and created a new branch, you can start cherry picking the commits one by one. Use the following command to cherry pick a single commit:

git cherry-pick <commit-hash>

Replace <commit-hash> with the hash of the commit you want to cherry pick. Repeat this command for each commit you want to pick.

A better way to build and deploy Web Apps

  Cloud Dev Environments
  Test/QA enviroments

One-click preview environments for each branch of code.

Step 4: Resolve Conflicts (if any)

During the cherry picking process, conflicts may arise if the changes being cherry picked conflict with the existing code in the target branch. Git will pause the cherry picking process and prompt you to resolve the conflicts manually.

Use the command git status to check the files with conflicts. Open the conflicting files in a text editor and resolve the conflicts by editing the files to your desired state. Once you have resolved the conflicts, save the files and use the following command to continue cherry picking:

git cherry-pick --continue

If you decide to abort the cherry picking process due to conflicts, you can use the following command:

git cherry-pick --abort

Step 5: Push the Changes

After successfully cherry picking the desired commits, you can push the changes to the remote repository using the command:

git push origin <new-branch-name>

Replace <new-branch-name> with the name of the branch you created earlier.

Alternative Method: Cherry Pick a Range of Commits

In addition to cherry picking individual commits, Git also allows you to cherry pick a range of commits. This can be useful when you want to pick a series of consecutive commits. To cherry pick a range of commits, use the following command:

git cherry-pick <start-commit-hash>..<end-commit-hash>

Replace <start-commit-hash> with the hash of the first commit in the range, and <end-commit-hash> with the hash of the last commit in the range. Git will pick all the commits in the specified range and apply them to the current branch.

Best Practices and Considerations

When cherry picking multiple commits, it is important to keep the following best practices and considerations in mind:

– Always create a new branch for cherry picking to avoid making changes directly on your main branch.
– Cherry pick the commits in the order they were originally committed to maintain the logical flow of changes.
– Be cautious of conflicts that may arise during cherry picking. Take the time to carefully resolve conflicts to ensure the integrity of the codebase.
– Test the cherry picked changes thoroughly to ensure they function as expected in the target branch.
– Remember that cherry picking creates new commits with different commit hashes, so avoid cherry picking the same commits multiple times to prevent duplication.

For more information on cherry picking in Git, you can refer to the official Git documentation:

More Articles from the Git Tutorial: From Basics to Advanced Concepts series: