Workshop นี้จะจำลองการทำงานเป็นทีม 2 คน โดยใช้ Git Branch และ Merge เพื่อให้ความเข้าใจ ทำความหมายทั่วไป และปฏิบัติในสภาพจริง ก่อนนำไปใช้ในโครงการที่มีความซับซ้อนมากขึ้น การทำงานเป็นทีมด้วย Git Branch ทำให้แต่ละคนสามารถทำงานบน Feature ต่างๆ พร้อมกัน โดยไม่ขัดขวางกัน และเมื่อทำงานเสร็จสามารถ Merge กลับมาเป็น Main Branch ได้อย่างปลอดภัย สำหรับทีมที่พัฒนา Application บน de.co.th Cloud VPS นี่คือทักษะที่จำเป็นต้องมี
Workshop: จำลองการทำงานเป็นทีม 2 คน ด้วย Git Branch และ Merge
ขั้นตอนที่ 1: เตรียมสภาพแวดล้อมและ Repository
เรากำหนดให้ Person A เป็นผู้สร้าง Repository และ Person B เป็นผู้ Clone ไปใช้:
# Person A สร้าง Repository
mkdir -p ~/projects/demo-repo
cd ~/projects/demo-repo
git init
echo "# Demo Project" > README.md
git add README.md
git commit -m "Initial commit from Person A"
# Person B Clone Repository
cd ~/projects
git clone ~/projects/demo-repo demo-repo-b
cd demo-repo-b
ขั้นตอนที่ 2: Person A สร้าง Feature Branch สำหรับ User Authentication
Person A จะสร้าง Feature Branch เพื่อทำงานด้านการ Authentication:
# Person A สร้าง feature/user-auth branch
cd ~/projects/demo-repo
git checkout -b feature/user-auth
# สร้างไฟล์ auth.js
echo "
// Simple authentication module
function authenticate(username, password) {
if (username && password.length >= 8) {
return { success: true, user: username, token: 'abc123' };
}
return { success: false, error: 'Invalid credentials' };
}
module.exports = { authenticate };
" > auth.js
# Commit changes
git add auth.js
git commit -m "feat: add user authentication module"
# Push ไปยัง origin
git push origin feature/user-auth
ขั้นตอนที่ 3: Person B สร้าง Feature Branch สำหรับ API Endpoints
ในขณะเดียวกัน Person B จะสร้าง Feature Branch เพื่อทำงานด้าน API:
# Person B สร้าง feature/api-endpoint branch
cd ~/projects/demo-repo-b
git checkout -b feature/api-endpoint
# สร้างไฟล์ api.js
echo "
const express = require('express');
const app = express();
app.get('/api/users', (req, res) => {
res.json([
{ id: 1, name: 'User 1', email: '[email protected]' },
{ id: 2, name: 'User 2', email: '[email protected]' }
]);
});
app.post('/api/users', (req, res) => {
// Create new user
res.json({ success: true, message: 'User created' });
});
app.listen(3000, () => console.log('API running on port 3000'));
module.exports = app;
" > api.js
# Commit changes
git add api.js
git commit -m "feat: add REST API endpoints"
# Push ไปยัง origin
git push origin feature/api-endpoint
ขั้นตอนที่ 4: สร้าง Pull Request เพื่อ Code Review
ทั้ง Person A และ B ต้องสร้าง Pull Request เพื่อให้อีกฝ่ายตรวจสอบ Code:
- Person A: สร้าง PR จาก feature/user-auth ไปยัง main พร้อมอธิบาย “Add authentication system for user login”
- Person B: สร้าง PR จาก feature/api-endpoint ไปยัง main พร้อมอธิบาย “Add REST API endpoints for user management”
- ทั้งคู่ลง Comment เพื่ออธิบายข้อดี ข้อมูล การทดสอบ
ขั้นตอนที่ 5: Code Review และให้ Feedback
ทั้ง Person A และ B ควรตรวจสอบ Code ของกันและกันก่อน Merge:
- ตรวจสอบ Logic ของแต่ละ Function ว่าถูกต้อง
- มองหา Security Issues เช่น SQL Injection (ถ้ามี Database)
- ประเมิน Code Style และความอ่านง่าย
- เสนอการปรับปรุง หากมี Best Practices ที่ควรใช้
- ให้ Approval หรือ Request Changes
ขั้นตอนที่ 6: Merge Pull Request
เมื่อ Approval เสร็จสิ้น ให้ Merge ทั้ง 2 PR เข้า Main Branch:
# ผ่าน GitHub/GitLab Web Interface คลิก "Merge Pull Request"
# หรือทำ Manual Merge ด้วย Command Line:
cd ~/projects/demo-repo
git checkout main
git pull origin main
# Merge PR แรก
git merge --no-ff feature/user-auth
# Merge PR ที่สอง
git merge --no-ff feature/api-endpoint
# Push กลับไป origin
git push origin main
ขั้นตอนที่ 7: Sync ความเป็นปัจจุบันของ Local Repository
ทั้ง Person A และ B ต้อง Sync เพื่อให้ Local Repository มีข้อมูล Latest:
# Person A
cd ~/projects/demo-repo
git fetch origin
git checkout main
git pull origin main
ls -la # ดูไฟล์ที่ถูก Merge เข้ามา
# Person B
cd ~/projects/demo-repo-b
git fetch origin
git checkout main
git pull origin main
ls -la # ควรเห็นไฟล์ auth.js และ api.js
ขั้นตอนที่ 8: ดู Commit History และ Merge Graph
ตรวจสอบประวัติ Commit และ Merge เพื่อให้เห็นภาพรวมการทำงาน:
git log --oneline --all --graph
# ผลลัพธ์จะแสดง:
# * commit hash - Merge pull request #2 (feature/api-endpoint)
# |\\
# | * commit hash - feat: add REST API endpoints
# |
# * commit hash - Merge pull request #1 (feature/user-auth)
# |\\
# | * commit hash - feat: add user authentication module
# |/
# * commit hash - Initial commit
# หรือใช้ GUI Tools เช่น GitKraken, SourceTree, GitHub Desktop
# ที่จะแสดงภาพ Branch และ Merge แบบ Visual ที่ชัดเจน
ขั้นตอนที่ 9: ปฏิบัติการกับ Merge Conflict (ลองทำ)
หากต้องการจำลอง Merge Conflict ให้ลองให้ทั้งคนแก้ไขไฟล์เดียวกัน:
# Person A แก้ไข README.md
cd ~/projects/demo-repo
echo "# Demo Project - User Auth Module" > README.md
git add README.md
git commit -m "docs: update README with auth details"
git push origin feature/user-auth-v2
# Person B แก้ไข README.md ด้วย (conflict!)
cd ~/projects/demo-repo-b
echo "# Demo Project - API Endpoints Module" > README.md
git add README.md
git commit -m "docs: update README with API details"
git push origin feature/api-endpoint-v2
# เมื่อ Merge PR ตัวที่สอง จะเกิด Merge Conflict
# ปรากฏว่า README.md มีการแก้ไขจากทั้งสองฝ่าย
# แก้ไข Conflict ด้วยการเลือกเนื้อหาที่ต้องการ
git checkout --theirs README.md # เก็บเวอร์ชันจาก feature/api-endpoint
# หรือ
git checkout --ours README.md # เก็บเวอร์ชันจาก feature/user-auth
# หรือแก้ไขด้วยมือ แล้วลบ conflict markers
# Commit Merge Resolution
git add README.md
git commit -m "Resolve merge conflict: combine auth and API documentation"
git push origin main
สิ่งที่ได้เรียนรู้จาก Workshop นี้
- Branch Strategy – การใช้ Feature Branch ทำให้ทีมสามารถทำงานแยกกัน และลด Conflict
- Pull Request Workflow – Pull Request เป็นเครื่องมือสำคัญในการ Code Review และการควบคุมคุณภาพ
- Merge Techniques – การใช้ –no-ff flag เพื่อให้ History ชัดเจนว่า Merge มาจาก Branch ไหน
- Conflict Resolution – ทราบวิธีแก้ไข Merge Conflict อย่างปลอดภัย
- Collaboration – การสื่อสารและ Feedback ที่ดีช่วยให้ทีมทำงานได้มีประสิทธิภาพ
- Commit History – การเขียน Commit Message ที่ดี ช่วยให้ย้อนกลับประวัติและจำไว้ว่าทำอะไร
Tips และ Best Practices
- Keep Pull Requests Small – PR เล็ก ๆ จะ Review ได้ง่ายและเร็วกว่า
- Write Clear Commit Messages – Commit Message ที่ดี ช่วยเพื่อบ Blame และ ให้ Code Review ง่ายขึ้น
- Test Before Submitting PR – ยืนยันว่า Code ของคุณ Pass ทั้งหมดก่อนส่ง PR
- Use Descriptive Branch Names – ชื่อ Branch ที่มีความหมาย เช่น feature/user-auth, bugfix/login-issue, docs/readme-update
- Review Code Thoroughly – ไม่ควรลืมถึง Security, Performance, Readability เมื่อทำ Code Review
- Communicate with Team – บอกให้ทีมรู้ว่าคุณกำลัง Merge หรือ Branch ของคุณมีการเปลี่ยนแปลงครั้งใหญ่
- Keep Branches Updated – ทำ Fetch และ Pull เป็นประจำเพื่อแน่ใจว่า Branch ของคุณมี Latest Code
Integration กับ de.co.th Cloud VPS
สำหรับทีมที่พัฒนา Application บน de.co.th Cloud VPS สามารถตั้งค่า Git Hooks หรือ CI/CD Pipeline (เช่น GitHub Actions หรือ GitLab CI) ให้รันการ Test และ Deploy โดยอัตโนมัติเมื่อมี Pull Request หรือ Push ไปยัง Main Branch นี่จะช่วยให้แน่ใจว่าโค้ดที่ Merge มาเป็นคุณภาพและพร้อม Deploy ได้
สรุป
Workshop นี้ได้สอนขั้นตอนการทำงานเป็นทีม 2 คน ด้วย Git Branch, Pull Request, Code Review และ Merge ซึ่งเป็นทักษะที่จำเป็นในการพัฒนา Software แบบวิชาชีพ ด้วยการจำลองเหตุการณ์จริง ทีมจะเข้าใจวิธีการทำงานร่วมกัน ลด Conflict และลดข้อผิดพลาดในการ Merge ทำให้การพัฒนาโครงการเป็นไปด้วยความเรียบร้อยและมีประสิทธิภาพมากขึ้น
