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:

bash
git cherry-pick abc1234..def5678
# Commits applied in unexpected order
# Conflicts that should not occur
# Missing commits from the range

Symptoms

  • 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..B excludes 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. 1.Verify the commit order before cherry-picking:
  2. 2.```bash
  3. 3.git log --oneline abc1234..def5678
  4. 4.# Shows commits from newest to oldest (reverse order)
  5. 5.git log --oneline --reverse abc1234..def5678
  6. 6.# Shows from oldest to newest (the order cherry-pick will apply them)
  7. 7.`
  8. 8.Use the correct range notation. Remember that A..B excludes A:
  9. 9.```bash
  10. 10.# To include both endpoints
  11. 11.git cherry-pick abc1234^..def5678
  12. 12.# The ^ includes the commit abc1234 itself

# Or specify individual commits git cherry-pick abc1234 def5678 ghi9012 ```

  1. 1.Handle merge commits by using the -m flag:
  2. 2.```bash
  3. 3.git cherry-pick -m 1 <merge-commit-hash>
  4. 4.# -m 1 means use the first parent (usually the main branch)
  5. 5.`
  6. 6.Check for already-applied commits using git cherry:
  7. 7.```bash
  8. 8.git cherry -v main feature-branch
  9. 9.# + means not applied, - means already applied
  10. 10.`
  11. 11.Abort and restart if cherry-pick went wrong:
  12. 12.```bash
  13. 13.git cherry-pick --abort
  14. 14.# Then retry with the correct range
  15. 15.git cherry-pick --no-commit abc1234^..def5678
  16. 16.# Review with git diff, then commit
  17. 17.git commit -m "Cherry-pick feature commits"
  18. 18.`

Prevention

  • Always verify commit order with git log --reverse before cherry-picking ranges
  • Use --no-commit flag to review all cherry-picked changes before committing
  • Prefer git merge or git rebase over cherry-pick for moving multiple commits
  • Document the exact commit hashes to cherry-pick, not just ranges
  • Use git cherry to 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-patch and git am for more controlled patch application