ข้ามไปยังเนื้อหา
Tayakorn
← Git Handbook

← Git Handbook

LEVEL 4 · ระดับเทพ

Repo ใหญ่ & บทสรุป

15.1 เมื่อ repo เริ่มอืด

repo ที่โตมาก (ประวัติยาว ไฟล์เยอะ) จะเริ่มช้า เครื่องมือที่ช่วย:

  • shallow clone — git clone --depth 1 ดึงแค่ commit ล่าสุด ไม่เอาประวัติทั้งหมด เหมาะกับ CI ที่ต้องการแค่โค้ดล่าสุด
  • Git LFS — ไฟล์ใหญ่ที่ไม่ใช่โค้ด (วิดีโอ, โมเดล, รูปความละเอียดสูง) ไม่เหมาะเก็บใน Git ตรง ๆ เพราะทุกเวอร์ชันถูกเก็บครบ LFS เก็บไฟล์จริงแยกที่อื่น เหลือไว้ใน repo แค่ตัวชี้
  • git gc — เก็บกวาด object ที่ลอยและบีบอัดเป็น packfile (ปกติ Git ทำเองเป็นระยะ)

15.2 บทสรุปทั้งเล่ม — แก่นที่ถือกลับไป

ถ้าจำได้แค่ไม่กี่อย่างจากทั้งเล่ม ขอให้เป็นสิ่งเหล่านี้ เพราะทุกคำสั่งงอกออกมาจากมัน:

  • commit คือ ภาพถ่ายทั้งโปรเจกต์ ที่มี SHA ผูกกับเนื้อหา — จึงแก้ไม่ได้ ปลอมไม่ได้ และเป็นรากของความน่าเชื่อถือทั้งหมด
  • ไฟล์เดินทางผ่าน สามพื้นที่ (working → staging → repository) — เข้าใจสิ่งนี้แล้ว add/commit/restore/reset เป็นเรื่องเดียวกันหมด
  • branch เป็นแค่ ป้ายชี้ commit — merge, rebase, reset ล้วนคือการขยับป้ายและผูกสายประวัติ
  • เกือบทุกอย่าง กู้คืนได้ ถ้าเคย commit — reflog คือตาข่ายนิรภัย กล้าทดลองได้เต็มที่
  • เส้นแบ่งเดียวที่ต้องเคารพ: อย่าเขียนประวัติที่แชร์กับคนอื่นไปแล้ว

— จบหลักสูตร —

รายการอ้างอิง#

เรียงตามลำดับที่ปรากฏครั้งแรกในเล่ม เล่มนี้ไม่ปักรุ่น Git — หน้าเอกสารบน git-scm.com ทุกรายการอ่านจากฉบับล่าสุดตอนตรวจ (Git 2.56.0 · 4 พฤศจิกายน 2026) ถ้าเครื่องคุณใช้รุ่นเก่ากว่า ดูหน้าเดียวกันของรุ่นตัวเองได้ด้วย git help <คำสั่ง> · ทุกรายการเปิดอ่านได้ฟรีทั้งหน้า

  1. Chacon S, Straub B. Getting Started — What is Git? Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 1.3 · git-scm.com/book — บทที่ 1, 2 · ภาพถ่ายไม่ใช่ส่วนต่าง · งานเกือบทุกอย่างทำในเครื่อง · checksum SHA-1 ยาว 40 ตัวอักษรฐานสิบหก
  2. Git project. git-init — Create an empty Git repository or reinitialize an existing one. เอกสารทางการ Git · git-scm.com/docs/git-init — บทที่ 1, 12 · โครง .git/ (objects · refs/heads · refs/tags) และ init.defaultBranch · ค่าเริ่มต้นยังเป็น master และจะเปลี่ยนเป็น main เมื่อ Git 3.0 ออก
  3. Git project. hash-function-transition (แผนย้ายฟังก์ชันแฮชจาก SHA-1 ไป SHA-256). เอกสารทางการ Git · git-scm.com/docs/hash-function-transition — บทที่ 2, 12 · ชื่อ object แบบ SHA-1 ยาว 40 ตัว แบบ SHA-256 ยาว 64 ตัว
  4. Git project. git-log — Show commit logs. เอกสารทางการ Git · git-scm.com/docs/git-log — บทที่ 3, 11 · --oneline · --graph · -S (pickaxe) · --grep · --author · ช่วง A..B
  5. Git project. gitignore — Specifies intentionally untracked files to ignore. เอกสารทางการ Git · git-scm.com/docs/gitignore — บทที่ 3 · ไฟล์ที่ติดตามอยู่แล้วไม่ถูกกระทบ และคำแนะนำ git rm --cached
  6. Chacon S, Straub B. Git Internals — Git References. Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 10.3 · git-scm.com/book — บทที่ 4, 12 · branch คือไฟล์ใน refs/heads ที่เก็บ SHA · HEAD เป็น symbolic reference · tag object คือวัตถุชนิดที่สี่
  7. Git project. git-switch — Switch branches. เอกสารทางการ Git · git-scm.com/docs/git-switch — บทที่ 4, 5 · -c · --detach · การเดาสร้าง branch ติดตาม remote เมื่อชื่อตรงกันใน remote เดียว (--guess เป็นค่าเริ่มต้น)
  8. Git project. git-checkout — Switch branches or restore working tree files. เอกสารทางการ Git · git-scm.com/docs/git-checkout — บทที่ 4 · หัวข้อ DETACHED HEAD และการเก็บ commit ที่ทำในสถานะนั้นด้วยการสร้าง branch
  9. Git project. git-clone — Clone a repository into a new directory. เอกสารทางการ Git · git-scm.com/docs/git-clone — บทที่ 5, 15 · ชื่อ remote เริ่มต้น origin และ --depth (shallow clone)
  10. GitHub. Token authentication requirements for Git operations. The GitHub Blog 15 ธันวาคม 2020 · github.blog — บทที่ 5 · เลิกรับรหัสผ่านบัญชีสำหรับงาน Git ตั้งแต่ 13 สิงหาคม 2021
  11. GitHub. Generating a new SSH key and adding it to the ssh-agent. GitHub Docs · docs.github.com — บทที่ 5
  12. Git project. git-pull — Fetch from and integrate with another repository or a local branch. เอกสารทางการ Git · git-scm.com/docs/git-pull — บทที่ 5 · ฉบับปัจจุบันระบุว่าค่าเริ่มต้นคือ --ff-only เมื่อไม่ได้ตั้ง pull.rebase หรือ pull.ff ถ้าสองฝั่งแยกทางกันแล้ว git pull จะหยุด ไม่ merge ให้เอง — ตั้งค่าหรือระบุ --no-rebase / --rebase ก่อน
  13. Git project. git-push — Update remote refs along with associated objects. เอกสารทางการ Git · git-scm.com/docs/git-push — บทที่ 5, 9, 14 · -u · การปฏิเสธ push ที่ไม่ใช่ fast-forward · --tags / --follow-tags · --force-with-lease เทียบ --force
  14. Git project. git-reset — Set HEAD or the index to a known state. เอกสารทางการ Git · git-scm.com/docs/git-reset — บทที่ 6 · โหมด --soft / --mixed (ค่าเริ่มต้น) / --hard
  15. Git project. git-revert — Revert some existing commits. เอกสารทางการ Git · git-scm.com/docs/git-revert — บทที่ 6, 7 · revert บันทึก commit ใหม่ที่กลับผลของ commit เดิม และหน้านี้เตือนว่าทั้ง reset --hard และ restore --source ทิ้งการแก้ที่ยังไม่ commit
  16. Git project. git-reflog — Manage reflog information. เอกสารทางการ Git · git-scm.com/docs/git-reflog — บทที่ 7, 14 · ไวยากรณ์ HEAD@{n} และการหมดอายุของ entry
  17. Git project. git-rebase — Reapply commits on top of another base tip. เอกสารทางการ Git · git-scm.com/docs/git-rebase — บทที่ 8 · คำสั่งใน interactive rebase · --continue / --skip / --abort · หัวข้อ RECOVERING FROM UPSTREAM REBASE
  18. Chacon S, Straub B. Git Branching — Rebasing. Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 3.6 · git-scm.com/book — บทที่ 8, 14 · ช่วง The Perils of Rebasing ที่มาของกฎทอง
  19. Driessen V. A successful Git branching model. nvie.com 5 มกราคม 2010 (เพิ่ม Note of reflection 5 มีนาคม 2020) · nvie.com — บทที่ 8, 10 · ต้นทางของ Git Flow · บันทึกปี 2020 ของผู้เขียนเองแนะนำ workflow ที่ง่ายกว่าสำหรับทีมที่ส่งมอบต่อเนื่อง
  20. Git project. git-rerere — Reuse recorded resolution of conflicted merges. เอกสารทางการ Git · git-scm.com/docs/git-rerere — บทที่ 8, 13 · ต้องเปิด rerere.enabled · rerere เขียนผลลงไฟล์ให้ แต่ไม่แตะ index จึงยังต้องตรวจแล้ว git add เอง
  21. Git project. git-tag — Create, list, delete or verify tags. เอกสารทางการ Git · git-scm.com/docs/git-tag — บทที่ 9 · annotated สำหรับ release · lightweight สำหรับป้ายส่วนตัวหรือชั่วคราว
  22. Hammant P, with contributions from friends. Trunk Based Development. trunkbaseddevelopment.com · trunkbaseddevelopment.com — บทที่ 10 · branch เดียวชื่อ trunk · feature branch อายุสั้น · feature flag
  23. Conventional Commits 1.0.0. conventionalcommits.org · conventionalcommits.org — บทที่ 10 · สเปกนิยามแค่ feat กับ fix ส่วน docs: refactor: เป็นชนิดที่สเปกอนุญาต (ตามแนว commitlint/Angular)
  24. Git project. git-bisect — Use binary search to find the commit that introduced a bug. เอกสารทางการ Git · git-scm.com/docs/git-bisect — บทที่ 11 · บรรทัดจริงของ Git คือ Bisecting: 675 revisions left to test after this (roughly 10 steps)
  25. Chacon S, Straub B. Git Internals — Git Objects. Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 10.2 · git-scm.com/book — บทที่ 12 · content-addressable filesystem · blob / tree / commit · ผล git cat-file -p ของ commit จริงมีทั้งบรรทัด author และ committer
  26. Chacon S, Straub B. Git Internals — Packfiles. Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 10.4 · git-scm.com/book — บทที่ 12 · pack เมื่อ loose object เยอะ ตอน git gc หรือตอน push · เก็บ delta ระหว่างไฟล์ที่คล้ายกัน
  27. Git project. git-worktree — Manage multiple working trees. เอกสารทางการ Git · git-scm.com/docs/git-worktree — บทที่ 13
  28. Git project. githooks — Hooks used by Git. เอกสารทางการ Git · git-scm.com/docs/githooks — บทที่ 13 · pre-commit รันก่อนขอข้อความ commit และ exit ไม่เป็น 0 = ยกเลิก · commit-msg · pre-push · core.hooksPath
  29. Chacon S, Straub B. Customizing Git — Git Hooks. Pro Git ฉบับที่ 2 (Apress 2014) หัวข้อ 8.3 · git-scm.com/book — บทที่ 13 · hook ฝั่ง client ไม่ถูกคัดลอกไปตอน clone
  30. Git project. git-sparse-checkout — Reduce your working tree to a subset of tracked files. เอกสารทางการ Git · git-scm.com/docs/git-sparse-checkout — บทที่ 13 · หน้านี้ยังติดป้ายว่าคำสั่งอยู่ในขั้นทดลอง (experimental)
  31. Git project. git-fsck — Verifies the connectivity and validity of the objects in the database. เอกสารทางการ Git · git-scm.com/docs/git-fsck — บทที่ 14 · --lost-found และความหมายของ dangling object
  32. Git project. git-gc — Cleanup unnecessary files and optimize the local repository. เอกสารทางการ Git · git-scm.com/docs/git-gc — บทที่ 7, 14, 15 · gc.reflogExpire ค่าเริ่มต้น 90 วัน · gc.reflogExpireUnreachable ค่าเริ่มต้น 30 วัน · gc --auto ที่คำสั่งทั่วไปเรียกเอง
  33. Git project. git-filter-branch — Rewrite branches. เอกสารทางการ Git · git-scm.com/docs/git-filter-branch — บทที่ 14 · คำเตือนต้นหน้าที่ไม่แนะนำให้ใช้ และชี้ไปที่ git filter-repo (ติดตั้งแยก ไม่ได้มากับ Git)
  34. GitHub. Removing sensitive data from a repository. GitHub Docs · docs.github.com — บทที่ 3, 14 · ขั้นแรกคือเพิกถอนหรือหมุนรหัสลับ แล้วค่อยเขียนประวัติใหม่ด้วย git-filter-repo
  35. Git LFS project. Git Large File Storage. git-lfs.com · git-lfs.com — บทที่ 15 · แทนไฟล์ใหญ่ด้วย text pointer ใน Git

เล่มต่อไป: git push แล้วเกิดอะไรต่อ?#

ตลอดเล่มนี้ทุกอย่างจบลงที่ git push — งานของคุณขึ้นไปอยู่บนเซิร์ฟเวอร์แล้ว แต่มีคำถามที่ค้างอยู่: ใครเป็นคนตรวจว่าโค้ดที่เพิ่งรวมเข้ามายัง build ได้ เทสต์ผ่าน และไม่ทำของเดิมพัง? ถ้าให้คนนั่งตรวจเองทุกครั้ง ทุกอย่างจะช้าและพลาดง่าย คำตอบคือ CI/CD — การให้เครื่องตรวจและส่งมอบโค้ดอัตโนมัติทุกครั้งที่คุณ push หรือเปิด Pull Request

มันคือก้าวต่อจากตรงนี้พอดี: เหตุการณ์ใน Git ที่คุณเรียนมาทั้งเล่ม (push, PR, tag) คือ "ทริกเกอร์" ที่ปลุกสายการตรวจสอบและ deploy ให้ทำงานเอง

อ่านต่อ: System Design#

เข้าใจกลไกของ Git แล้ว รู้วิธีจัดการโค้ดของทีมให้ปลอดภัยและย้อนกลับได้ — แต่โค้ดที่ push ไปแล้วต้องรันบนระบบที่รับมือกับผู้ใช้จริงได้ System Design พาไปจากคำถาม "จัดการโค้ดยังไง" สู่ "ระบบที่ deploy ไปแล้วต้องออกแบบยังไงให้ scale ได้จริง" ตั้งแต่เซิร์ฟเวอร์เครื่องเดียวไปจนถึง production พร้อม auth และความปลอดภัย · เปิดอ่าน System Design Handbook →


Git 0 → Hero — คู่มือฉบับเข้าใจถึงแก่น · เขียนเป็นภาษาไทย พร้อมไดอะแกรมทุกหัวข้อ

วิธีใช้ให้คุ้ม: อ่านไล่ระดับ อย่าข้าม Level 0–1 แม้จะใช้ Git เป็นแล้ว เพราะแบบจำลองความคิดในบทต้นคือกุญแจของบทท้าย · ลองทุกคำสั่งใน repo ทดลองที่พังได้

ถ้าเนื้อหานี้มีประโยชน์ —เลี้ยงกาแฟสักแก้วหรือพร้อมเพย์ · Lightning

อ่านแบบเต็มเล่ม