How to cherry-pick multiple commits
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Cherry-picking multiple commits in Git means applying selected commits from one branch onto another without merging the entire branch. It is useful for backports, hotfix propagation, and pulling over a small subset of changes when a full merge would bring unrelated work.
Cherry-Pick Several Explicit Commits
If you already know the commit hashes, list them in the order you want Git to apply them.
Git applies them one after another. The order matters because later commits may depend on earlier ones.
Cherry-Pick a Commit Range
When the commits are contiguous in history, you can cherry-pick a range.
Using start^..end includes both start and end. This is a common point of confusion because plain start..end excludes the left boundary commit.
Review Before You Cherry-Pick
Before applying multiple commits, inspect what is actually in the range.
That quick check helps catch the common mistake of pulling in one extra commit or missing the first one in the sequence.
Handle Conflicts Carefully
If a cherry-pick conflicts, Git pauses and lets you resolve the conflict before continuing.
If you decide the cherry-pick should not proceed:
This is especially important when applying several commits, because one bad conflict in the middle can leave the branch in an in-between state until you either continue or abort.
Use -n When You Want One Combined Result
Sometimes you want the changes from several commits but do not want separate cherry-picked commits in the target branch. Use --no-commit.
That stages the changes without committing each one immediately.
Be Careful with Merge Commits
Cherry-picking normal commits is straightforward. Cherry-picking merge commits is more complicated because Git needs a mainline parent choice. If you hit that case, stop and make sure you understand the history before continuing.
For ordinary multi-commit cherry-picks, it is usually better to pick the underlying non-merge commits whenever possible.
Keep an Eye on Duplicate History
Cherry-picking copies changes, not commit identity. If the same logical change later arrives through a merge, you may see duplicate-looking history or conflict noise. That is why cherry-pick is best for surgical cases, not as a general branch synchronization strategy.
Prefer Reviewing the Target Branch First
Before cherry-picking, update and inspect the target branch so you understand what is already there. That reduces the chance of replaying fixes that were already applied under different commit hashes.
Cherry-Pick Is About Intentional Selection
If you are bringing over most of a branch, a merge or rebase is usually the more honest operation. Cherry-pick is strongest when the selection is small, deliberate, and easy to explain in code review.
Common Pitfalls
- Cherry-picking commits in the wrong order.
- Using
start..endwhen you meant to include the starting commit. - Forgetting to inspect the range before applying it.
- Leaving a conflicted cherry-pick half-finished instead of continuing or aborting.
- Using cherry-pick repeatedly where a merge or rebase would better express the real intent.
Summary
- List explicit hashes when the commits are scattered.
- Use
start^..endwhen you want an inclusive contiguous range. - Check the range with
git log --onelinebefore applying it. - Resolve conflicts with
--continueor cancel with--abort. - Use
--no-commitwhen you want one combined commit instead of many separate cherry-picks.
Related reading
- How to cherry-pick multiple commits
- How to cherry-pick only changes to certain files?
- How to cleanup garbage in remote git repo
- How to clone a private git repository into a kubernetes pod using ssh keys in secrets?
- How to clone a specific Git tag
- How to clone git repository with specific revision/changeset?
- How to close git commit editor?
- How to code a simple versioning system?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.