How to cherry-pick a commit in Git

· Category: Git

Short answer

Use git cherry-pick <commit-hash> to apply a specific commit onto your current branch.

Steps

  1. Find the commit hash:
git log --oneline
  1. Cherry-pick it:
git cherry-pick abc1234
  1. Cherry-pick a range:
git cherry-pick abc1234^..def5678
  1. Abort if needed:
git cherry-pick --abort

Tips

  • Cherry-picking is useful for backporting bug fixes to release branches.
  • It creates a new commit with a different hash on the target branch.
  • Use -n to apply changes without committing immediately.

Common issues

  • Conflicts can occur if the commit changes lines modified in the target branch.
  • Cherry-picking too often may indicate the need for a better branching strategy.