เชี่ยวชาญ Git ใน 30 วัน

วันที่ 23: แก้ไขประวัติเวอร์ชันที่ Commit แล้ว ตอนที่ 5 (rebase 2)

ในบทความก่อนหน้านี้ เราได้พูดคุยกันว่า Rebase ใช้สำหรับ “กำหนดเวอร์ชันฐานใหม่” ให้กับ Branch ที่มีอยู่สอง Branch หลังจากดำเนินการ Rebase แล้ว มันยังเปลี่ยนจุดเริ่มต้นของ Branch เดิมด้วย (จุดแยกของ Branch ถูกย้ายไป) ทำให้กราฟเวอร์ชันเปลี่ยนแปลงไป อย่างไรก็ตาม Rebase ทำได้มากกว่านั้น มันยังสามารถใช้แก้ไขข้อมูลเวอร์ชันของเวอร์ชันใด ๆ บนเส้น Branch เส้นหนึ่งได้อีกด้วย

บทความนี้จะสำรวจ git rebase กันต่อ โดยเจาะลึกเทคนิคขั้นสูงและ Workflow ที่ใช้งานได้จริงสำหรับการใช้ Rebase อย่างมีประสิทธิภาพ รวมถึงการแก้ไขข้อมูล Commit บน Branch

เทคนิค Interactive Rebase ขั้นสูง

การแยก Commit

ในการแยก Commit หนึ่งรายการออกเป็นหลายรายการ:

git rebase -i HEAD~3

# Mark the commit to split with "edit"
# When Git stops at that commit:
git reset HEAD^
git add file1
git commit -m "First part"
git add file2
git commit -m "Second part"
git rebase --continue

การรวมและการจัดลำดับใหม่

คุณสามารถทั้งจัดลำดับใหม่และรวม Commit ได้ในการดำเนินการครั้งเดียว:

git rebase -i HEAD~5

# In the editor:
pick abc123 Feature A
squash def456 Fix for A
pick jkl012 Feature C  # Moved before B
pick ghi789 Feature B  # Moved after C
fixup mno345 Typo fix for B

Rebase ด้วย Exec

รันคำสั่งหลังจากแต่ละ Commit ระหว่าง Rebase:

git rebase -i --exec "npm test" HEAD~5

สิ่งนี้จะรัน npm test หลังจากนำแต่ละ Commit ไปใช้ ช่วยให้คุณค้นพบว่า Commit ใดทำให้ Test เสียหาย

Rebase ไปยัง Commit ที่ระบุ

แทนที่จะใช้ชื่อ Branch คุณสามารถ Rebase ไปยัง Commit ที่ระบุได้:

git rebase --onto <new-base-commit> <old-base-commit> <branch>

ตัวอย่างกรณีการใช้งาน: การลบ Commit ออกจากกลางประวัติ:

# Remove commits between B and D
#     A---B---C---D---E feature
# Becomes:
#     A---B---D'---E' feature

git rebase --onto B D feature

Rebase ด้วยตัวเลือก Strategy

ใช้ Merge Strategy ระหว่าง Rebase:

# Always prefer our version in conflicts
git rebase -X ours main

# Always prefer their version
git rebase -X theirs main

การรักษา Merge Commit ไว้

โดยค่าเริ่มต้น Rebase จะทำให้ Merge Commit แบนราบ (Flatten) ในการรักษา Merge Commit ไว้:

git rebase -p main
# or
git rebase --preserve-merges main

หมายเหตุ: -p เลิกใช้แล้ว ให้ใช้ --rebase-merges แทน:

git rebase --rebase-merges main

Rebase ด้วย Fork-Point

ค้นหาจุดแยก (Fork Point) โดยอัตโนมัติ:

git rebase --fork-point main

สิ่งนี้มีประโยชน์เมื่อ Remote Branch ถูก Force Push

Workflow: การทำให้ Feature Branch ทันสมัยอยู่เสมอ

Workflow ที่พบบ่อยในการทำให้ Feature Branch ของคุณทันสมัยกับ main:

# On feature branch
git fetch origin
git rebase origin/main

# Or in one command
git pull --rebase origin main

Workflow: การเตรียมตัวสำหรับ Pull Request

เก็บกวาด Commit ของคุณก่อนสร้าง Pull Request:

# 1. Interactive rebase to clean history
git rebase -i main

# 2. Squash fixup commits, reword messages
# 3. Force push to update your branch
git push -f origin feature-branch

Workflow: การอัปเดตหลังการ Review โค้ด

หลังจากได้รับ Feedback จากการ Review โค้ด:

# Make changes
git add .
git commit --fixup <original-commit>

# Rebase with autosquash
git rebase -i --autosquash main

# Force push the updated branch
git push -f origin feature-branch

การรับมือกับ Force Push

เมื่อมีคน Force Push Branch ที่คุณกำลังทำงานอยู่:

# Fetch the updated branch
git fetch origin

# Rebase your work onto the new history
git rebase --onto origin/feature-branch old-commit feature-branch

หรือเพียงแค่:

git pull --rebase origin feature-branch

Rebase เทียบกับ Reset สำหรับการเก็บกวาด

สำหรับการเก็บกวาด Commit ในเครื่องก่อน Push:

Rebase (แนะนำ):

  • รักษาลำดับเวลาตามเดิม
  • ควบคุมแบบ Interactive ได้
  • Cherry-Pick Commit ได้

Reset (ง่ายกว่าแต่ยืดหยุ่นน้อยกว่า):

  • ลบ Commit ออกอย่างสมบูรณ์
  • เริ่มใหม่จากสถานะที่สะอาด
  • ควบคุม Commit แต่ละรายการได้น้อยกว่า

รูปแบบ Rebase ที่พบบ่อย

รูปแบบที่ 1: Squash ทุก Commit

git rebase -i --root
# Mark all but first commit as "squash"

รูปแบบที่ 2: Reword ข้อความ Commit ทั้งหมด

git rebase -i HEAD~10
# Mark all commits as "reword"

รูปแบบที่ 3: ลบ Commit ที่ระบุ

git rebase -i HEAD~10
# Mark unwanted commits as "drop"

รายการตรวจสอบความปลอดภัยของ Rebase

ก่อนทำการ Rebase:

  • Commit นี้ถูก Push ไปยัง Branch ที่แชร์กันแล้วหรือไม่?
  • มีผู้อื่นทำงานต่อยอดจาก Commit นี้หรือไม่?
  • ผมมีข้อมูลสำรอง (Branch หรือ Tag) หรือไม่?
  • ผมได้สื่อสารกับทีมแล้วหรือไม่?
  • ผมพร้อมที่จะ Force Push หรือไม่?

การกู้คืนจากหายนะของ Rebase

หากเกิดข้อผิดพลาดขึ้น:

# Step 1: Stay calm
# Step 2: Find the old state
git reflog

# Step 3: Reset to before the rebase
git reset --hard HEAD@{5}

# Step 4: Try again more carefully

Rebase กับ Continuous Integration

เมื่อใช้ CI/CD ร่วมกับ Rebasing:

  1. Rebase ในเครื่อง ก่อน Push
  2. รัน Test หลังจาก Rebase
  3. Force Push เฉพาะ Branch ของคุณ
  4. รอ CI ก่อนทำการ Merge
  5. ห้าม Rebase หลังจาก Merge แล้ว

สรุปแนวปฏิบัติที่ดีที่สุด

  1. เฉพาะในเครื่องเท่านั้น: Rebase เฉพาะ Commit ที่ยังไม่ได้ Push
  2. Rebase ขนาดเล็ก: Rebase บ่อย ๆ เพื่อหลีกเลี่ยง Conflict ขนาดใหญ่
  3. ทดสอบอย่างละเอียด: รัน Test หลังจาก Rebase
  4. สื่อสาร: เตือนทีมก่อน Force Push
  5. สำรองข้อมูล: สร้าง Branch สำรองก่อน Rebase ครั้งใหญ่
  6. ระวัง -f: ตรวจสอบซ้ำก่อน Force Push

สรุปของวันนี้

เทคนิค Rebase ขั้นสูงมอบเครื่องมือที่ทรงพลังให้คุณในการรักษาประวัติ Commit ที่สะอาดและเข้าใจได้ แม้คุณสมบัติเหล่านี้จะช่วยปรับปรุง Workflow ของคุณได้อย่างมาก แต่ก็ต้องอาศัยการฝึกฝนและการใช้งานอย่างระมัดระวัง หัวใจสำคัญคือการรู้ว่าเมื่อใดควรใช้ Rebase และเมื่อใดควรยึดกับทางเลือกที่ปลอดภัยกว่าอย่าง Merge

ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:

  • git rebase –exec
  • git rebase –onto
  • git rebase -X
  • git rebase –preserve-merges
  • git rebase –rebase-merges
  • git rebase –fork-point
  • git rebase –root
  • git pull –rebase
  • git push -f