ทำงานแบบทีม: branch, commit และ pull request
ใช้ใน Challenge Day 3
ถ้า 4 คนแก้ main พร้อมกัน โค้ดจะชนกันตลอด ทีมจริงจึงใช้ branch: แต่ละคนแตกสาขาไปทำงานของตัวเอง แล้วขอรวมกลับผ่าน pull request (PR) ให้เพื่อนรีวิวก่อน main จึงใช้งานได้เสมอ
At a glance
| คำศัพท์ | ความหมาย |
|---|---|
| Branch | สาขาของโค้ด ทำงานแยกได้โดยไม่กระทบ main |
| Pull request (PR) | คำขอรวม branch เข้า main พร้อมให้คนอื่นรีวิว |
| Review | เพื่อนอ่านโค้ด คอมเมนต์ หรือกด Approve |
| Merge | รวม branch เข้า main |
| Conflict | 2 คนแก้บรรทัดเดียวกัน ต้องเลือกเองว่าจะเอาอันไหน |
How it works
main ●────────────●──────────────●────▶ (Vercel deploy อัตโนมัติ)
\ / /
feat/menu ●───●────● (PR #1) /
\ /
fix/price ●──────●────────● (PR #2)Set up the team
หัวหน้าทีมสร้าง repo
สร้าง repo บน GitHub แล้วไปที่ Settings → Collaborators เพิ่มเพื่อนทุกคน
ทุกคน clone
git clone https://github.com/<team>/shop.git cd shopป้องกัน main (แนะนำ)
Settings → Branches → Add rule ให้
mainต้องผ่าน pull request ก่อน merge
Flow ทุกครั้งที่ทำงาน
ดึงโค้ดล่าสุด
git switch main git pullสร้าง branch
git switch -c feat/order-apiเขียนโค้ดและ commit ทีละเรื่องเล็ก ๆ
git add . git commit -m "feat: add POST /api/orders"Push branch
git push -u origin feat/order-apiเปิด pull request
บน GitHub กด Compare & pull request ใส่คำอธิบายว่าทำอะไร แล้วขอ reviewer
Review และ merge
เพื่อนรีวิว → กด Merge → ลบ branch แล้วกลับไป
git switch mainและgit pull
คำสั่งหลัก
| คำสั่ง | ทำอะไร |
|---|---|
git branch | ดูว่ามี branch อะไร และอยู่ที่ไหน |
git switch -c <name> | สร้าง branch ใหม่แล้วย้ายไป |
git switch <name> | ย้ายไป branch ที่มีอยู่ |
git push -u origin <name> | push branch ขึ้น GitHub ครั้งแรก |
git merge main | เอาของใหม่จาก main มารวมใน branch ตัวเอง |
gh pr create | เปิด PR จาก Terminal (ต้องมี GitHub CLI) |
ตั้งชื่อ commit ให้ทีมอ่านรู้เรื่อง
| Prefix | ใช้เมื่อ | ตัวอย่าง |
|---|---|---|
feat: | เพิ่มฟีเจอร์ใหม่ | feat: add product list page |
fix: | แก้บั๊ก | fix: total price rounding |
docs: | แก้เอกสาร / README | docs: add setup steps |
style: | แก้หน้าตา ไม่เปลี่ยน logic | style: card spacing |
refactor: | จัดโค้ดใหม่ ผลลัพธ์เท่าเดิม | refactor: move db to db.js |
เมื่อเจอ merge conflict
Git จะใส่เครื่องหมายไว้ในไฟล์ให้เลือกเอง
<<<<<<< HEAD
const price = 60; ← ของคุณ
=======
const price = 65; ← ของที่เข้ามาใหม่
>>>>>>> mainเลือก
ลบเครื่องหมาย
<<<<<<<,=======,>>>>>>>ให้เหลือโค้ดที่ถูก (VS Code มีปุ่ม Accept Current / Incoming)บันทึก
git add . git commit -m "fix: resolve conflict"ส่งต่อ
git pushแล้ว PR จะอัปเดตเอง
ฝึกต่อ
เว็บสำหรับลงมือทำ
ลงมือทำ · learngitbranching.js.orgLearn Git Branching ฝึก branch และ merge แบบเห็นภาพลงมือทำ · skills.github.comGitHub Skills ฝึกเปิด pull request บน GitHub จริงเอกสาร · docs.github.comGitHub Docs: About pull requests เอกสารทางการเอกสาร · conventionalcommits.orgConventional Commits มาตรฐานการเขียน commit message
วิดีโอเรียนเอง
Next steps
- Challenge Day 3: ทำร้านค้าเป็นทีม
CS ChallengeLast updated Oct 2026
