git
version control
cherry-pick
commits
software development

How to cherry-pick multiple commits

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Cherry-picking in Git allows developers to apply specific changes from one branch into another. This flexibility is essential for integrating patches, bug fixes, or features without merging entire branches. While cherry-picking a single commit is straightforward, managing multiple commits requires careful consideration. This guide describes how to cherry-pick multiple commits, including technical explanations and examples.

Understanding Cherry-Picking

Cherry-picking is the process of selecting a commit from one branch and applying it onto another. It is different from a typical merge, which involves combining the full timeline of one branch into another. Cherry-picking is ideal when you need specific changes that are encapsulated within certain commits.

Overview of the Cherry-Pick Command

The basic syntax for cherry-picking a commit is:

bash
git cherry-pick <commit-hash>

For multiple commits, a range or specific list of commits can be cherry-picked.

Cherry-Picking Multiple Commits

Using Commit Ranges

When the commits are consecutive in the branch history, you can cherry-pick a range:

bash
git cherry-pick <commit-hash-A>^..<commit-hash-B>
  • <commit-hash-A> is the first commit in the series.
  • <commit-hash-B> is the last commit you want to include.

This command applies all commits from just after <commit-hash-A> until <commit-hash-B>.

Cherry-Picking Non-Consecutive Commits

If the commits are not sequential or you only need select changes, you can cherry-pick multiple commits in sequence:

bash
git cherry-pick <commit-hash-1> <commit-hash-2> <commit-hash-3>

You will need to list each commit hash that you intend to apply.

Practical Example

Imagine a scenario where a feature branch has several commits, but only a few are relevant for a hotfix on the main branch. Here’s how you can proceed:

  1. Identify the Commits to Cherry-pick
    Use git log or git log --oneline to see the history and obtain commit hashes.
bash
   git log --oneline feature-branch

Example output:

 
1   f2e44bc Fix typo in documentation
2   4c5d9b3 Add logging to payment process
3   7d8bf9a Update payment gateway API
4   d5e8c3a Correct tax calculation
  1. Cherry-Pick the Selected Commits
    If you want to apply the commit 7d8bf9a and 4c5d9b3 to the main branch:
bash
   git checkout main
   git cherry-pick 7d8bf9a 4c5d9b3

Handling Conflicts

While cherry-picking, conflicts may arise if the changes contradict existing code on the target branch. When encountering a conflict:

  1. Git pauses the cherry-pick process.
  2. You'll see both conflicting files and CONFLICT messages.
  3. Manually edit the files to resolve conflicts.
  4. After resolving, continue with:
bash
   git add <resolved-file>
   git cherry-pick --continue

If you wish to abandon the cherry-pick during a conflict:

  1. Execute:
bash
   git cherry-pick --abort

This reverts the working directory to the state it was in before the attempt to cherry-pick.

Advantages and Considerations

Advantages:

  • Isolates specific changes without merging the full history.
  • Allows for rapid application of necessary bug fixes or features.

Considerations:

  • Overuse may lead to a fragmented commit history.
  • Requires careful conflict resolution.

Summary Table

Here's a summary of key points about cherry-picking multiple commits:

FeatureDescription
Single Commitgit cherry-pick <commit-hash>
Range of Commitsgit cherry-pick <commit-hash-A>^..<commit-hash-B>
Specific Non-Consecutive Commitsgit cherry-pick <commit-hash-1> <commit-hash-2>
Conflict HandlingManual resolution required Use --continue afterward
Abort Cherry-Pickgit cherry-pick --abort

Additional Tips

  • Before starting, ensure your branches are up to date: git fetch and git pull.
  • Test thoroughly after cherry-picking to verify functionality.
  • Consider using tags or branch names instead of hashes for readability.

Conclusion

Cherry-picking multiple commits is a powerful tool that, when used wisely, can streamline your development workflow without cluttering your main branch's history. By mastering the use of commit ranges or manually selecting commits, developers can ensure the precise integration of desired changes. Always handle conflicts with care and check your work thoroughly post-application.


Course illustration
Course illustration

All Rights Reserved.