Git ขั้นสูง
Amanda Crawford-Adamo
Software and Data Engineer
คำสั่ง Git Rebase:
git rebase <branch_name>

data-cleanupgit checkout data-cleanup
main ลงบน branch data-cleanupgit rebase main
data-cleanup จะถูกสร้างใหม่ต่อจาก commit ล่าสุดของ maindata-cleanup ที่ถูก rebase จะได้รับ hash ใหม่หมายเหตุ: หากมี conflict ใดๆ จะต้องแก้ไขด้วยตนเอง

git rebase -i <commit_hash>
การทำงาน
คำเตือน!
main$$
นี่คือ ประวัติของ branch data-validation ก่อนที่จะแก้ไข commit history
$ git log --oneline data-validation
abc1234 Optimize validation performance
def5678 Fix validation bug
ghi9101 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit
วิธีแก้ไข commit history โดยใช้คำสั่ง pick และ fixup ใน editor ที่เปิดขึ้น
$ git rebase -i HEAD~3
pick ghi9101 Add data validation function
fixup def5678 Fix validation bug
fixup abc1234 Optimize validation performance
# Rebase xyz1234..abc1234 onto HEAD~3(xyz1234) (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# f, fixup <commit> = like "squash", but discard this commit's log message
...
ก่อน
$ git log --oneline data-validation
abc1234 Optimize validation performance
def5678 Fix validation bug
ghi9101 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit
หลัง
$ git log --oneline data-validatoin
mno6789 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit
commit ต่อไปนี้ถูกรวมเป็นหนึ่งเดียว

Merge
Rebase
Merge
ใช้สำหรับรวม feature ที่เสร็จแล้ว และเมื่อต้องการเก็บบริบทการพัฒนาไว้
Rebase
ใช้สำหรับอัปเดต feature branch ให้ทันกับ main หรือจัดระเบียบก่อน merge
จำไว้
Git ขั้นสูง