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
- Find the commit hash:
git log --oneline
- Cherry-pick it:
git cherry-pick abc1234
- Cherry-pick a range:
git cherry-pick abc1234^..def5678
- 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
-nto 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.