git rebase -i — จัดเรียง รวม และแก้ commit ก่อน push
git rebase -i HEAD~N เปิด editor ให้เลือก squash, reword, drop หรือ reorder commit ก่อน push — ทำให้ history อ่านแล้วเข้าใจได้
git rebase -i HEAD~4 # เปิด interactive rebase 4 commit ล่าสุด
editor จะแสดง:
pick a1b2c3 feat: add login page
pick d4e5f6 fix typo
pick g7h8i9 wip: cleanup
pick j0k1l2 fix: remove console.log
เปลี่ยน command หน้า commit:
| command | ทำอะไร |
|---|---|
pick | เก็บ commit ไว้ |
squash / s | รวมกับ commit ก่อนหน้า |
reword / r | แก้ commit message |
drop / d | ลบ commit ออก |
edit / e | หยุดเพื่อแก้ไข |
ตัวอย่าง squash WIP commits:
pick a1b2c3 feat: add login page
squash d4e5f6 fix typo
drop g7h8i9 wip: cleanup
squash j0k1l2 fix: remove console.log
บันทึก → git จะ prompt ให้เขียน commit message รวม
⚠️ ใช้ได้ปลอดภัยก่อน push เท่านั้น — ถ้า push แล้ว history เปลี่ยนจะต้อง force push ซึ่งกระทบคนอื่น