ข้ามไปเนื้อหาหลัก

git cherry-pick — เอา commit เดียวข้าม branch โดยไม่ merge ทั้งหมด

cherry-pick เอา commit hash ที่ต้องการมาใส่ branch ปัจจุบัน — ใช้เมื่อ hotfix อยู่ใน feature branch แต่ต้องการ deploy ก่อน

scenario ที่เจอบ่อย: fix bug อยู่ใน feature branch แต่ต้องการเอา fix นั้นขึ้น production ก่อนที่ feature จะเสร็จ

# หา commit hash ที่ต้องการ
git log feature-branch --oneline
# abc1234 fix: แก้ null pointer ใน checkout

# ไป branch ที่ต้องการ apply
git checkout main

# cherry-pick commit นั้น
git cherry-pick abc1234

commit ใหม่จะถูกสร้างใน main พร้อม content เหมือน abc1234 แต่เป็น commit ใหม่ที่มี hash ต่างกัน

cherry-pick หลาย commit:

git cherry-pick abc1234 def5678        # เลือก 2 commit
git cherry-pick abc1234..def5678       # range (ไม่รวม abc1234)
git cherry-pick abc1234^..def5678      # range (รวม abc1234)

ถ้ามี conflict ให้แก้แล้วรัน git cherry-pick --continue หรือ --abort เพื่อยกเลิก