Skip to content

บทที่ 3: Version Control

“ถ้าย้อนเวลากลับไปก่อนทุบกำแพงผิดได้ คุณจะทำไหม?”

The Blueprint (พิมพ์เขียว)

Section titled “The Blueprint (พิมพ์เขียว)”

เคยมีประสบการณ์แบบนี้ไหม?

  • แก้โค้ดไปเรื่อยๆ แล้วพังหมดเลย อยากกลับไปเวอร์ชันเมื่อวานแต่ทำไม่ได้
  • ทำงานคู่กับเพื่อน แก้ไฟล์เดียวกัน งานหายไปครึ่งหนึ่ง
  • final_v2_edit_final_FINAL.psd ไม่รู้อันไหนของจริง

Version Control (Git) คือ ไทม์แมชชีน ที่บันทึกทุกการเปลี่ยนแปลง ย้อนกลับได้ทุกเมื่อ

The Construction Site (หน้างานก่อสร้าง)

Section titled “The Construction Site (หน้างานก่อสร้าง)”

⏰ Git = เครื่องย้อนเวลา

Section titled “⏰ Git = เครื่องย้อนเวลา”

ลองนึกภาพการสร้างบ้าน:

วันที่ 1: วางฐานราก ← Commit #1
วันที่ 2: ก่อกำแพงชั้นล่าง ← Commit #2
วันที่ 3: ติดประตูหน้าต่าง ← Commit #3
วันที่ 4: ทาสีผนัง ← Commit #4
วันที่ 5: โอ๊ะ! สีไม่ถูกใจ ← ย้อนไป Commit #3!

ทุก Commit คือ Snapshot ที่บันทึกสถานะทั้งหมดในขณะนั้น

Terminal window
# เริ่มต้นโปรเจกต์
git init
# บันทึก Snapshot
git add .
git commit -m "เพิ่มหน้า Login"
# ดูประวัติ
git log --oneline
# ย้อนกลับ
git checkout abc123
# สร้าง Branch (ห้องทดลอง)
git branch feature/new-design
git checkout feature/new-design

🌿 Branches — ห้องทดลอง

Section titled “🌿 Branches — ห้องทดลอง”
main ─────○─────○─────○───────○───── (บ้านจริง)
│ │
│ merge │
▼ │
feature ──────○──○──○───────┘ (ห้องทดลอง)

Branch = ห้องทดลองที่ไม่กระทบบ้านจริง ทดลองจนพอใจแล้วค่อย merge กลับ

Material Selection (เลือกสเปกวัสดุ)

Section titled “Material Selection (เลือกสเปกวัสดุ)”

🐙 GitHub — สำนักงานใหญ่ของนักพัฒนา

Section titled “🐙 GitHub — สำนักงานใหญ่ของนักพัฒนา”
บริการหน้าที่
Repositoryเก็บโค้ดบน Cloud
Pull Requestขอรวมโค้ด + Code Review
Issuesติดตาม Bugs, Features
ActionsCI/CD อัตโนมัติ
1. สร้าง Branch ใหม่
2. เขียนโค้ด + Commit
3. เปิด Pull Request
4. Code Review
5. Merge เข้า main
6. Deploy อัตโนมัติ!

Architect’s Note (บันทึกสถาปนิก)

Section titled “Architect’s Note (บันทึกสถาปนิก)”
  1. เขียน Commit Message ให้ดี

    • ❌ “fix” / “update” / “asdf”
    • ✅ “แก้บัค Login ไม่ได้เมื่อใช้ email ไทย”
  2. อย่า Commit ของลับ

    • ❌ API Keys, Passwords
    • ✅ ใช้ .gitignore + Environment Variables
  3. Push บ่อยๆ — Local crash = งานหาย

Terminal window
# .gitignore ที่ต้องมี
node_modules/
.env
.env.local
*.log
.DS_Store
  1. ใช้ Conventional Commitsfeat:, fix:, docs:
  2. สร้าง Branch Protection — ห้าม push ตรงเข้า main
  3. ทำ Code Review ทุก PR

หัวข้อสรุป
Gitเครื่องมือบันทึกประวัติการเปลี่ยนแปลง
CommitSnapshot ของโปรเจกต์ ณ เวลานั้น
Branchห้องทดลอง แยกจากโค้ดหลัก
GitHubCloud storage + Collaboration tools