git cherry-pick <commit> copy 1 commit từ branch khác vào branch hiện tại — tạo commit mới với cùng changes nhưng hash khác.
Khi cherry-pick là đúng:
1. Hotfix cần apply vào nhiều release branches:
git checkout release/2.1
git cherry-pick abc123 # apply hotfix commit từ main
git checkout release/2.0
git cherry-pick abc1232. Lấy 1 commit cụ thể từ colleague's branch mà bạn cần ngay:
git cherry-pick def456 # chỉ cần 1 commit, không cần cả branch3. Recover commit từ deleted branch:
git cherry-pick abc123 # commit vẫn tồn tại dù branch bị xóaKhi cherry-pick là dấu hiệu vấn đề:
- Cherry-pick từ develop vào feature hàng ngày → nên rebase thay thế
- "Chúng ta cherry-pick hotfix vào 5 branches" → cần reconsider branching strategy, quá nhiều long-lived branches
- Cherry-pick để share code giữa features → nên extract thành shared module
Lưu ý:
git cherry-pick abc123..def456 # range of commits
git cherry-pick -n abc123 # apply changes nhưng không commit (staging chỉ)Conflict cherry-pick: resolve, git add, git cherry-pick --continue.
git cherry-pick <commit> copies a commit from another branch into the current branch — creating a new commit with the same changes but a different hash.
When cherry-pick is correct:
1. Hotfix needs to be applied to multiple release branches:
git checkout release/2.1
git cherry-pick abc123 # apply hotfix commit from main
git checkout release/2.0
git cherry-pick abc1232. Getting one specific commit from a colleague's branch that you need right now:
git cherry-pick def456 # need just this one commit, not the whole branch3. Recovering a commit from a deleted branch:
git cherry-pick abc123 # commit still exists even if the branch was deletedWhen cherry-pick signals a problem:
- Cherry-picking from develop into a feature daily → use rebase instead
- "We cherry-pick hotfixes into 5 branches" → reconsider branching strategy, too many long-lived branches
- Cherry-picking to share code between features → extract a shared module instead
Useful flags:
git cherry-pick abc123..def456 # range of commits
git cherry-pick -n abc123 # apply changes but do not commit (staging only)Cherry-pick conflict: resolve, git add, git cherry-pick --continue.