How to Cherry Pick Multiple Commits in Git Easily
To cherry pick multiple commits in Git, use
git cherry-pick followed by the commit hashes separated by spaces or specify a range with git cherry-pick A^..B. This applies the changes from those commits onto your current branch.Syntax
The basic syntax to cherry pick multiple commits is:
git cherry-pick- picks specific commits by their hashes.... git cherry-pick- picks a range of commits from start (exclusive) to end (inclusive).^..
Each commit hash identifies a commit. The range syntax picks all commits after start_commit up to end_commit.
bash
git cherry-pick <commit1> <commit2> <commit3> git cherry-pick <start_commit>^..<end_commit>
Example
This example shows how to cherry pick three commits by their hashes and then how to cherry pick a range of commits.
bash
# Cherry pick specific commits by hash $ git cherry-pick a1b2c3d e4f5g6h 7i8j9k0 # Cherry pick a range of commits from commit a1b2c3d (exclusive) to 7i8j9k0 (inclusive) $ git cherry-pick a1b2c3d^..7i8j9k0
Output
First, Git applies commit a1b2c3d changes.
Then applies e4f5g6h changes.
Finally applies 7i8j9k0 changes.
For the range, Git applies all commits after a1b2c3d up to 7i8j9k0 in order.
Common Pitfalls
- Trying to cherry pick commits that conflict with your current branch can cause merge conflicts.
- Using the wrong commit range syntax can pick unintended commits.
- Cherry picking commits that depend on earlier commits not included may cause errors.
Always check commit dependencies and resolve conflicts carefully.
bash
# Wrong: Using range without caret picks commits including start commit $ git cherry-pick a1b2c3d..7i8j9k0 # Right: Use caret to exclude start commit $ git cherry-pick a1b2c3d^..7i8j9k0
Quick Reference
| Command | Description |
|---|---|
| git cherry-pick | Apply multiple specific commits by hash |
| git cherry-pick | Apply a range of commits from after |
| git cherry-pick --continue | Continue after resolving conflicts |
| git cherry-pick --abort | Cancel cherry-pick and revert changes |
Key Takeaways
Use
git cherry-pick with multiple commit hashes or a commit range to pick multiple commits.Use the caret (^) in ranges to exclude the start commit and avoid picking extra commits.
Resolve conflicts carefully when cherry picking multiple commits.
Check commit dependencies to avoid errors during cherry pick.
Use
git cherry-pick --continue or --abort to manage conflicts.