บทที่ 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 ที่บันทึกสถานะทั้งหมดในขณะนั้น
📸 Git Commands พื้นฐาน
Section titled “📸 Git Commands พื้นฐาน”# เริ่มต้นโปรเจกต์git init
# บันทึก Snapshotgit add .git commit -m "เพิ่มหน้า Login"
# ดูประวัติgit log --oneline
# ย้อนกลับgit checkout abc123
# สร้าง Branch (ห้องทดลอง)git branch feature/new-designgit 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 |
| Actions | CI/CD อัตโนมัติ |
📊 GitHub Flow
Section titled “📊 GitHub Flow”1. สร้าง Branch ใหม่2. เขียนโค้ด + Commit3. เปิด Pull Request4. Code Review5. Merge เข้า main6. Deploy อัตโนมัติ!Architect’s Note (บันทึกสถาปนิก)
Section titled “Architect’s Note (บันทึกสถาปนิก)”⚠️ ข้อควรระวัง
Section titled “⚠️ ข้อควรระวัง”-
เขียน Commit Message ให้ดี
- ❌ “fix” / “update” / “asdf”
- ✅ “แก้บัค Login ไม่ได้เมื่อใช้ email ไทย”
-
อย่า Commit ของลับ
- ❌ API Keys, Passwords
- ✅ ใช้
.gitignore+ Environment Variables
-
Push บ่อยๆ — Local crash = งานหาย
💡 Best Practices
Section titled “💡 Best Practices”# .gitignore ที่ต้องมีnode_modules/.env.env.local*.log.DS_Store- ใช้ Conventional Commits —
feat:,fix:,docs: - สร้าง Branch Protection — ห้าม push ตรงเข้า main
- ทำ Code Review ทุก PR
📝 สรุป
Section titled “📝 สรุป”| หัวข้อ | สรุป |
|---|---|
| Git | เครื่องมือบันทึกประวัติการเปลี่ยนแปลง |
| Commit | Snapshot ของโปรเจกต์ ณ เวลานั้น |
| Branch | ห้องทดลอง แยกจากโค้ดหลัก |
| GitHub | Cloud storage + Collaboration tools |