Git ข้างใน
commit คือรูปถ่ายที่ชี้กลับไปหาตัวก่อนหน้า ส่วน branch เป็นแค่ป้ายที่แปะอยู่บน commit มาดู merge, rebase กับ squash เอา branch เดียวกันเข้า main สามแบบ แล้วลองย้อน commit ด้วย revert กับ reset
สิ่งที่จะได้
- อธิบายได้ว่า commit กับ branch จริง ๆ คืออะไร: รูปถ่ายของโปรเจกต์ กับป้ายที่แปะอยู่บนมัน
- เลือกใช้ merge, rebase หรือ squash ได้ และรู้ว่าตัวไหนสร้าง commit ใหม่แทนของเดิม
- ย้อน commit ที่ push ไปแล้วด้วย
git revertและใช้reset --hardกับงานของตัวเองเท่านั้น - เอา commit ที่หายไปหลัง reset หรือ rebase กลับมาจาก
git reflogได้
สรุป 1 นาที
กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู
ลอง merge แล้วเริ่มใหม่ ลอง rebase แล้ว merge ดูว่าถนนตรงขึ้นแต่มี commit ถูกเขียนใหม่ ป้าย branch เลื่อนตามคำสั่ง ส่วน commit เดิมไม่เคยถูกแก้ เมืองมีที่ให้ 8 commit
branch feature push ขึ้นไปแล้วและเพื่อนดึงไปทำต่อ คุณอยาก rebase มันบน main ควรทำไหม
ในภาพ ตึกแต่ละหลังคือ commit หนึ่งตัว เป็นรูปถ่ายของทั้งโปรเจกต์ ชื่ออยู่หน้าตึก ส่วนสิ่งที่มันเปลี่ยนเป็นแผ่นบนหลังคา (สีฟ้าคือทำบน main สีม่วงคือทำบน feature) ถนนแต่ละเส้นวิ่งจาก commit กลับไปหาพ่อแม่ของมัน ถนนของ main อยู่ด้านหลัง ของ feature อยู่ด้านหน้า ป้ายคือ branch และวงบนพื้นบอกว่าป้ายนั้นชี้ที่ commit ไหน ตึกจาง ๆ เส้นประคือ commit ที่ไม่มี branch ไหนชี้แล้ว git ยังเก็บไว้อีกสักพัก
เทียบทุกแบบ: Git ข้างใน
กราฟเดียวกันทุกช่อง: main มี A B C ส่วน feature แยกจาก B มี D E แล้วเอา feature เข้า main สามแบบ ดูว่าประวัติออกมาหน้าตายังไง มี commit ไหนถูกเขียนใหม่ และ main มีกี่ commit
ใช้เมื่อ รวม branch ที่คนอื่นใช้อยู่ด้วย: ไม่แตะ commit เดิมเลย ประวัติจริงอยู่ครบว่าแยกตอนไหนรวมตอนไหน
ไม่ควรใช้เมื่อ อยากได้ประวัติเส้นตรงอ่านง่าย: merge commit เยอะ ๆ ทำให้ graph ยุ่ง
ชื่อ commit ในภาพเป็นป้าย ไม่ใช่ hash จริง D′ คือ commit ใหม่ที่มีการเปลี่ยนแปลงเดียวกับ D git.sh ในโค้ดข้างล่างแสดง hash จริง · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน
แต่ละคำสั่งเก็บอะไรไว้
| คำสั่ง | ประวัติ | เขียน commit ใหม่ | บน branch ที่แชร์กัน |
|---|---|---|---|
| merge | อยู่ครบ มี merge commit | ไม่ | ปลอดภัย |
| rebase | เป็นเส้นตรง | ใช่: hash ใหม่ | อย่าทำ |
| squash merge | commit เดียวบน main | ขั้นย่อยของ branch ไม่ถึง main | ปลอดภัย |
| revert | อยู่ครบ บวก commit ที่ย้อนกลับ | ไม่ | ปลอดภัย |
| reset --hard | branch ถอยกลับ | ทิ้ง commit ไว้ข้างหลัง | อย่าทำ |
ไม่มีอะไรถูกแก้ มีแต่เพิ่ม
commit ไม่เคยเปลี่ยน hash ของมันคำนวณจากเนื้อหา พ่อแม่ ผู้เขียน และเวลา ที่เรียกว่า "เขียนประวัติใหม่" จริง ๆ คือสร้าง commit ใหม่แล้วย้ายป้ายไปชี้ มันถึงได้เจ็บเวลา rebase branch ที่แชร์กัน เพราะคนอื่นยังถือของเก่าอยู่
ของหาย? ลอง reflog
git reflog บอกว่า HEAD เคยชี้ไปที่ไหนบ้าง commit ที่โดน reset หรือ rebase ทิ้งไว้มักยังอยู่ในนั้นอีกพักใหญ่ สร้าง branch จาก hash ของมันแล้วก็ได้คืนมา
ในโค้ดจริง
#!/bin/sh
# Merge, rebase and squash on the same two branches, then revert and reset, in throwaway repositories.
# Fixed names and dates, so the hashes come out the same every run (a commit's hash covers its content, parents,
# author and time). Run: sh git.sh (needs git 2.28+)
set -eu
export GIT_AUTHOR_NAME=Mai GIT_AUTHOR_EMAIL=mai@example.com GIT_COMMITTER_NAME=Mai GIT_COMMITTER_EMAIL=mai@example.com
export GIT_AUTHOR_DATE="2026-11-25T10:00:00+0700" GIT_COMMITTER_DATE="2026-11-25T10:00:00+0700"
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
start() { # main: A B C; feature, from B: D E
git init -q -b main "$tmp/$1" && cd "$tmp/$1"
for c in A B; do echo "$c" > "$c.txt" && git add . && git commit -qm "$c"; done
git switch -qc feature
for c in D E; do echo "$c" > "$c.txt" && git add . && git commit -qm "$c"; done
git switch -q main && echo C > C.txt && git add . && git commit -qm C
}
start merge
git merge -q --no-edit feature
echo "merge: $(git rev-list --count main) commits on main, $(git rev-list --merges --count main) merge commit"
start rebase
d=$(git rev-parse --short feature~1)
git switch -q feature && git rebase -q main && git switch -q main && git merge -q --ff-only feature
echo "rebase: $(git rev-list --count main) commits on main, $(git rev-list --merges --count main) merge commits, D was $d and is now $(git rev-parse --short main~1)"
start squash
git merge -q --squash feature > /dev/null 2>&1 && git commit -qm "feature: D and E"
echo "squash: $(git rev-list --count main) commits on main, D and E as one"
start undo
git revert --no-edit HEAD > /dev/null
echo "revert: $(git rev-list --count main) commits, C kept in the history: $(git log --format=%s | grep -cx C)"
git reset -q --hard HEAD~2
echo "reset --hard HEAD~2: main at $(git log -1 --format=%s), $(git rev-list --count main) commits, C still in the reflog: $(git reflog --format=%gs | grep -c 'commit: C')"
sh git.sh (ต้องมี Docker)ผลที่พิมพ์ออกมา:merge: 6 commits on main, 1 merge commit rebase: 5 commits on main, 0 merge commits, D was 9277dfc and is now 9344968 squash: 4 commits on main, D and E as one revert: 4 commits, C kept in the history: 1 reset --hard HEAD~2: main at B, 2 commits, C still in the reflog: 1
ทุกคำสั่งในบท รันใน repo ที่สร้างแล้วทิ้ง: merge, rebase (D ได้ hash ใหม่), squash แล้ว revert กับ reset --hard (C ยังอยู่ใน reflog) ตั้งชื่อกับเวลาไว้ตายตัว hash เลยออกมาเหมือนเดิมทุกครั้ง รันจริงด้วย git ข้างล่างคือที่พิมพ์ออกมา
คำถามที่เจอบ่อย
branch ใน git จริง ๆ คืออะไร
เป็นป้ายชื่อเล็ก ๆ (ไฟล์ที่เก็บ hash ของ commit ตัวเดียว) ที่ขยับตามไปเองทุกครั้งที่ commit ต่อบนมัน สร้างหรือลบ branch เลยแทบไม่ใช้เวลา ของจริงคือ commit แต่ละตัวที่ชี้กลับไปหาพ่อแม่ของมัน
merge กับ rebase ต่างกันยังไง
merge สร้าง commit ใหม่ที่มีพ่อแม่สองตัว ไม่แตะ commit เดิมเลย ประวัติบอกตามจริงว่าแยกตอนไหนรวมตอนไหน rebase ยก commit ของเราไปต่อท้ายอีก branch ได้เส้นตรงอ่านง่าย แต่ทุกตัวที่ยกไปเป็น commit ใหม่ hash ใหม่
ทำไมไม่ควร rebase branch ที่คนอื่นดึงไปแล้ว
rebase ไม่ได้แก้ commit เดิม แต่สร้างตัวใหม่แทน (D กลายเป็น D′) ต้อง force push และเพื่อนที่ถือตัวเก่าอยู่จะได้ประวัติที่ไม่ตรงกับเรา กฎง่าย ๆ คือ rebase ได้เฉพาะ branch ของตัวเองที่ยังไม่มีใครดึงไป
squash merge ต่างจาก merge ธรรมดายังไง
git merge --squash เอาการเปลี่ยนแปลงทั้งหมดของ feature มาเป็น commit เดียวบน main ประวัติสะอาด เหมาะกับ PR ที่มี commit ย่อย ๆ เยอะ แต่ขั้นย่อยไม่ไปถึง main และ git ไม่รู้ว่า feature ถูกรวมไปแล้ว
commit ที่ push ขึ้น main แล้วทำระบบพัง ย้อนยังไง
ใช้ git revert <commit> ได้ commit ใหม่ที่ทำกลับด้าน ไม่ลบอะไร ทุกคน pull ต่อได้ปกติ อย่าไป reset --hard แล้ว force push บน branch ที่ใช้ร่วมกัน เพราะเครื่องคนอื่นยังมี commit ที่เราทิ้งไปอยู่ ประวัติจะไม่ตรงกัน
reset --soft, --mixed, --hard ต่างกันยังไง
ทั้งสามย้าย branch ถอยไปที่ commit ที่บอกเหมือนกัน --soft เก็บการเปลี่ยนแปลงไว้ใน staging --mixed (ค่าเริ่มต้น) เก็บไว้ในไฟล์แต่ไม่ stage ส่วน --hard ทิ้งการเปลี่ยนแปลงในไฟล์ไปด้วย
เผลอ reset --hard ไปแล้ว กู้คืนได้ไหม
ส่วนใหญ่ได้ถ้ายังไม่นานเกินไป git reflog จดไว้ว่า HEAD เคยชี้ที่ไหนบ้าง หา hash เก่าแล้วสร้าง branch จากมันก็กลับมา แต่ของที่ไม่เคย commit เลย reflog ไม่ได้จดไว้ให้
ทำไม commit ที่ rebase แล้วถึงได้ hash ใหม่
hash ของ commit คิดจากเนื้อหา พ่อแม่ ผู้เขียน และเวลา พอยกไปต่อท้ายที่ใหม่ พ่อแม่ก็เปลี่ยน hash เลยเปลี่ยนตาม commit ไม่เคยถูกแก้ การ "เขียนประวัติใหม่" คือสร้าง commit ใหม่แล้วย้ายป้ายไปชี้
บทต่อไป
Git securityระดับ: กลาง
history เก็บทุกอย่าง รวมถึง token ที่คุณลบไปแล้ว บทต่อไปคือยกเลิก secret ที่หลุดก่อน และป้องกัน branch ที่ทุกคนใช้ร่วมกัน