Introduction
Git cherry-pick with a commit range (A..B) can apply commits in an unexpected order or miss commits, especially when the range includes merge commits or the commits are not linearly connected:
git cherry-pick abc1234..def5678
# Commits applied in unexpected order
# Conflicts that should not occur
# Missing commits from the rangeSymptoms
- Cherry-picked commits appear in wrong order in the log
- Unexpected merge conflicts during cherry-pick
- Some commits from the range are silently skipped
- Cherry-pick stops midway with no clear error message
- Resulting code does not match the expected state
Common Causes
- Range notation
A..Bexcludes commit A (it means "after A up to and including B") - Commits in the range are not topologically ordered
- Range includes merge commits which cherry-pick cannot handle by default
- Commits have already been applied to the target branch
- Commit range specified in reverse order (newer..older instead of older..newer)
Step-by-Step Fix
- 1.Verify the commit order before cherry-picking:
- 2.```bash
- 3.git log --oneline abc1234..def5678
- 4.# Shows commits from newest to oldest (reverse order)
- 5.git log --oneline --reverse abc1234..def5678
- 6.# Shows from oldest to newest (the order cherry-pick will apply them)
- 7.
` - 8.Use the correct range notation. Remember that
A..Bexcludes A: - 9.```bash
- 10.# To include both endpoints
- 11.git cherry-pick abc1234^..def5678
- 12.# The ^ includes the commit abc1234 itself
# Or specify individual commits git cherry-pick abc1234 def5678 ghi9012 ```
- 1.Handle merge commits by using the
-mflag: - 2.```bash
- 3.git cherry-pick -m 1 <merge-commit-hash>
- 4.# -m 1 means use the first parent (usually the main branch)
- 5.
` - 6.Check for already-applied commits using
git cherry: - 7.```bash
- 8.git cherry -v main feature-branch
- 9.# + means not applied, - means already applied
- 10.
` - 11.Abort and restart if cherry-pick went wrong:
- 12.```bash
- 13.git cherry-pick --abort
- 14.# Then retry with the correct range
- 15.git cherry-pick --no-commit abc1234^..def5678
- 16.# Review with git diff, then commit
- 17.git commit -m "Cherry-pick feature commits"
- 18.
`
Prevention
- Always verify commit order with
git log --reversebefore cherry-picking ranges - Use
--no-commitflag to review all cherry-picked changes before committing - Prefer
git mergeorgit rebaseover cherry-pick for moving multiple commits - Document the exact commit hashes to cherry-pick, not just ranges
- Use
git cherryto check which commits have already been applied - For backporting, create a dedicated backport branch to isolate the cherry-picks
- Test cherry-picked code in the target branch before merging
- Consider using
git format-patchandgit amfor more controlled patch application