Git Rebase เป็นเครื่องมือที่ทำให้ Git History สะอาด และเข้าใจง่ายขึ้น โดยแปลงประวัติการ Commit ให้เป็นลำดับเชิงเส้น ซึ่งแตกต่างจาก Merge ที่สร้าง Merge Commit ใหม่ Rebase เป็นวิธีที่ได้รับความนิยมในการรวมการเปลี่ยนแปลงจาก Branch อื่น
Git Rebase vs Merge
Merge และ Rebase ทั้งคู่ใช้สำหรับรวม Branches แต่ทำหน้าที่แตกต่างกัน:
Merge:
- สร้าง Merge Commit เพื่อรวม 2 Branches
- เก็บรักษา History ทั้งหมด
- History อาจจะซับซ้อนกับ Multiple Parents
- ปลอดภัยสำหรับ Public Branches
Rebase:
- เลื่อน Commits ของ Branch ปัจจุบันไปอยู่บนตำแหน่ง Base Branch ใหม่
- สร้าง Linear History ที่เข้าใจง่าย
- History สะอาดกว่า Merge
- อันตรายสำหรับ Public Branches (Rewrite History)
Rebase ขั้นมูลฐาน
# Rebase Branch ปัจจุบันบน main
git rebase main
# Rebase โดยการแก้ไข Commits ขณะทำ Rebase
git rebase -i main
# Rebase Branch อื่น
git rebase main feature/new-feature
# ยกเลิก Rebase ที่กำลังทำ
git rebase --abort
Interactive Rebase (Squash, Reword, etc)
Interactive Rebase ช่วยให้คุณจัดการ Commits ในรูปแบบต่างๆ
# Interactive Rebase สำหรับ 3 Commits ล่าสุด
git rebase -i HEAD~3
# ตัวเลือกใน Interactive Mode:
# p (pick) - ใช้ Commit นี้
# r (reword) - ใช้ Commit แต่แก้ไข Message
# s (squash) - ใช้ Commit นี้ แต่ Merge เข้ากับ Commit ก่อนหน้า
# f (fixup) - เหมือน squash แต่ไม่มี Message
# d (drop) - ลบ Commit นี้
# x (exec) - รัน Shell Command
Squashing Commits
Squash ใช้ในการรวม Commits หลายตัวเป็นหนึ่งเดียว เป็นวิธีที่ใช้ได้บ่อยสำหรับ Pull Requests ขนาดเล็ก
# Squash 3 Commits ล่าสุดเป็น 1
git reset --soft HEAD~3
git commit -m "Add new feature"
# หรือใช้ Interactive Rebase
git rebase -i HEAD~3
# เปลี่ยน 'pick' เป็น 's' สำหรับ Commits ที่ต้องการ Squash
Reword Commits
# เปลี่ยน Commit Message ของ Commits สุดท้าย
git commit --amend
# เปลี่ยน Commit Message ของ Commits เก่า
git rebase -i HEAD~5
# เปลี่ยน 'pick' เป็น 'r' สำหรับ Commit ที่ต้องการแก้
Rebase Conflicts
เมื่อ Rebase พบความขัดแย้ง ต้องแก้ไขก่อน
# Rebase พบ Conflict
# Git จะหยุด และแจ้งให้แก้ไข
# แก้ไข Conflicts ในไฟล์
# ...
# เพิ่มไฟล์ที่แก้ไขแล้ว
git add .
# ทำการ Rebase ต่อ
git rebase --continue
# หากต้องการยกเลิก
git rebase --abort
ตัวอย่าง Rebase Workflow
# 1. สร้าง Feature Branch
git checkout -b feature/new-feature
# 2. ทำงาน Feature
# ... edit files ...
# ... edit more files ...
# ... edit again ...
# 3. Commit หลาย ๆ ครั้ง (บางทีไม่ Clean)
git add .
git commit -m "Add user authentication"
git commit -m "Fix typo"
git commit -m "Add validation"
git commit -m "Improve error handling"
# 4. main Branch มี Update จาก Team อื่น
git fetch origin
# 5. Rebase ลงใหม่บน main ล่าสุด
git rebase origin/main
# 6. Interactive Rebase เพื่อ Squash Commits
git rebase -i origin/main
# Pick commit แรก, Squash ส่วนที่เหลือ
# 7. Push ไป Feature Branch
git push origin feature/new-feature -f
# 8. สร้าง Pull Request
# History จะสะอาดมาก
Rebase Safety Tips
- ไม่ Rebase บน Branches ที่ Public (shared branches)
- Rebase เฉพาะ Local Branches ก่อน Push
- บอก Team ให้ทราบถ้า Rebase บน Shared Branches
- ใช้
git reflogเพื่อกู้คืน Commits ที่หายไป - ให้แน่ใจว่า Rebase ผ่านแล้วก่อน Push ด้วย -f

