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.

Related Article: How to Undo a Git Rebase: A Tutorial

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.

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

Related Article: How to Fix a Git Detached Head

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:

How to Merge Multiple Commits as Single Squashed Commit

Combining multiple commits into a single squashed commit can help streamline your Git workflow. This article provides a step-by-step guide on using Git's git merge... read more

How to Remove a File From the Latest Git Commit

Learn how to remove a file from the latest Git commit using a simple process. Discover two approaches: interactive rebase and amending the commit. Explore best practices... read more

How To Cherry Pick A Commit With Git

Cherry picking a commit with Git allows you to selectively apply changes to your codebase. In this article, you will learn the meaning and process of cherry picking, as... read more

How To Combine My Last N Commits In Git

Learn how to combine multiple commits into a single one using Git's squash feature. Discover the potential reasons for combining commits and explore different methods to... read more

How To Change the Git Remote URL

Changing the Git remote URL for a repository is a simple process that can be done with a few easy steps. This article will guide you through the process, from checking... read more

How To Uncommit Last Git Commit

Learn how to uncommit your last commit in Git with simple steps and avoid unnecessary changes in your codebase. Find out two methods to uncommit, understand the reasons... read more