วันที่ 27: การควบคุมเวอร์ชันด้วย Branch ใน Remote Repository เดียวกัน
แต่ละทีมใช้กลยุทธ์ Branch ที่แตกต่างกัน บทความนี้จะสำรวจโมเดล Branch ที่พบบ่อยและเวลาที่ควรใช้แต่ละโมเดล
ทำไมกลยุทธ์ Branch จึงสำคัญ
หากไม่มีกลยุทธ์ที่ชัดเจน:
- โค้ด Conflict เพิ่มขึ้น
- Release กลายเป็นเรื่องวุ่นวาย
- Bug หลุดเข้าไปในระบบ Production
- การประสานงานในทีมได้รับผลกระทบ
GitHub Flow
เหมาะที่สุดสำหรับ: เว็บแอปพลิเคชัน, Continuous Deployment
กลยุทธ์
- Main Branch เดียว:
main - Feature Branch สำหรับการเปลี่ยนแปลงทั้งหมด
- Deploy จาก
main
Workflow
1. Create branch from main
git checkout -b feature-name
2. Work and commit
git commit -am "Add feature"
3. Push and create PR
git push origin feature-name
4. Review and discuss
5. Deploy from branch (staging)
6. Merge to main
# On GitHub: Merge PR
7. Deploy from main (production)
ข้อดี
- เรียบง่ายและตรงไปตรงมา
- พัฒนาได้รวดเร็ว
- main พร้อม Deploy เสมอ
- ยอดเยี่ยมสำหรับ Continuous Deployment
ข้อเสีย
- ไม่รองรับ Release ระยะยาว
- ต้องมี CI/CD ที่ดี
- ไม่เหมาะสำหรับหลายเวอร์ชัน
Git Flow
เหมาะที่สุดสำหรับ: ซอฟต์แวร์ที่มี Release, หลายเวอร์ชัน
ประเภทของ Branch
- main: โค้ดที่พร้อมใช้งานจริง (Production-ready)
- develop: Branch สำหรับการรวม (Integration)
- feature/: Feature ใหม่
- release/: การเตรียม Release
- hotfix/: การแก้ไขเร่งด่วน
Workflow
# Start new feature
git checkout -b feature/login develop
# Work on feature
git commit -am "Implement login"
# Finish feature
git checkout develop
git merge --no-ff feature/login
git branch -d feature/login
# Start release
git checkout -b release/1.0 develop
# Prepare release (bug fixes only)
git commit -am "Bump version to 1.0"
# Finish release
git checkout main
git merge --no-ff release/1.0
git tag -a 1.0
git checkout develop
git merge --no-ff release/1.0
git branch -d release/1.0
# Hotfix
git checkout -b hotfix/1.0.1 main
git commit -am "Fix critical bug"
git checkout main
git merge --no-ff hotfix/1.0.1
git tag -a 1.0.1
git checkout develop
git merge --no-ff hotfix/1.0.1
git branch -d hotfix/1.0.1
ข้อดี
- แยกความรับผิดชอบชัดเจน
- รองรับหลายเวอร์ชัน
- กระบวนการ Release มีโครงสร้าง
ข้อเสีย
- ซับซ้อนกว่า
- วงจรการพัฒนาช้ากว่า
- มีภาระเกินจำเป็นสำหรับโปรเจกต์ขนาดเล็ก
GitLab Flow
เหมาะที่สุดสำหรับ: องค์กรที่มีสภาพแวดล้อม Staging และ Production
กลยุทธ์
- Environment Branch:
main,staging,production - Feature Branch Merge เข้าสู่
main - เลื่อนขั้นผ่านสภาพแวดล้อมต่าง ๆ
Workflow
# Feature development
git checkout -b feature-name main
# Work and commit
git push origin feature-name
# Create MR to main
# After merge to main
# Deploy to staging
git checkout staging
git merge main
git push origin staging
# After testing in staging
# Deploy to production
git checkout production
git merge staging
git push origin production
ข้อดี
- Deploy ตามสภาพแวดล้อม
- เส้นทางการเลื่อนขั้นชัดเจน
- เหมาะสำหรับอุตสาหกรรมที่มีกฎระเบียบควบคุม
ข้อเสีย
- มี Branch ให้จัดการมากกว่า
- ต้องใช้ระบบอัตโนมัติ
- อาจช้ากว่า
Trunk-Based Development
เหมาะที่สุดสำหรับ: ทีมที่มีประสิทธิภาพสูง, Continuous Deployment
กลยุทธ์
- Trunk เดียว:
main - Feature Branch อายุสั้นมาก (< 1 วัน)
- Feature Flag สำหรับ Feature ที่ยังไม่เสร็จ
Workflow
# Create short-lived branch
git checkout -b quick-fix main
# Make small change
git commit -am "Fix button color"
# Merge same day
git checkout main
git merge quick-fix
git push origin main
# Or commit directly to main
# (with CI and feature flags)
ข้อดี
- รวมโค้ดได้เร็วที่สุด
- ลด Merge Conflict
- ส่งเสริม CI/CD
- คุณภาพโค้ดสูง
ข้อเสีย
- ต้องมีวินัย
- ต้องมี CI/CD ที่ดี
- Feature Flag มีความซับซ้อน
Release Flow (Microsoft)
เหมาะที่สุดสำหรับ: ซอฟต์แวร์ระดับองค์กร, Release ที่วางแผนไว้
กลยุทธ์
main: พร้อม Release เสมอrelease/*: Branch สำหรับการทำให้เสถียร (Stabilization)- Topic Branch: Feature อายุสั้น
Workflow
# Create topic branch
git checkout -b users/name/feature main
# Work and create PR
git push origin users/name/feature
# Create release branch
git checkout -b release/v2.0 main
# Cherry-pick into release
git cherry-pick <commit-hash>
# Release
git checkout main
git merge release/v2.0
git tag v2.0
ข้อดี
- ตาราง Release ยืดหยุ่น
- พัฒนาและทำให้เสถียรไปพร้อมกันได้
- เหมาะสำหรับองค์กร
ข้อเสีย
- อาจซับซ้อน
- ต้องใช้ Cherry-Pick
- ต้องดูแลรักษามากกว่า
การเลือกกลยุทธ์
ใช้ GitHub Flow เมื่อ:
- Continuous Deployment
- เว็บแอปพลิเคชัน
- ทีมขนาดเล็ก
- ทำซ้ำอย่างรวดเร็ว
ใช้ Git Flow เมื่อ:
- Release ตามกำหนดการ
- แอป Desktop/Mobile
- จำเป็นต้องรองรับหลายเวอร์ชัน
- วงจรชีวิตซอฟต์แวร์แบบดั้งเดิม
ใช้ GitLab Flow เมื่อ:
- หลายสภาพแวดล้อม
- Deploy เป็นขั้นตอน
- ข้อกำหนดด้านกฎระเบียบ
ใช้ Trunk-Based เมื่อ:
- ทีมที่มีประสบการณ์
- CI/CD ยอดเยี่ยม
- Continuous Deployment
- ความถี่ในการ Release สูง
หลักการตั้งชื่อ Branch
Feature Branch
feature/user-authentication
feature/dark-mode
feat/issue-123
Bug Fix Branch
fix/login-error
bugfix/issue-456
hotfix/critical-security-patch
Release Branch
release/1.0
release/2.0.0
release/v1.5
Branch อื่น ๆ
docs/update-readme
test/integration-tests
refactor/database-layer
chore/update-dependencies
คำสั่งจัดการ Branch
แสดงรายการ Branch
# Local branches
git branch
# All branches (including remote)
git branch -a
# With last commit
git branch -v
# Merged branches
git branch --merged
# Unmerged branches
git branch --no-merged
ลบ Branch
# Delete local branch
git branch -d feature-name
# Force delete
git branch -D feature-name
# Delete remote branch
git push origin --delete feature-name
เปลี่ยนชื่อ Branch
# Rename current branch
git branch -m new-name
# Rename other branch
git branch -m old-name new-name
สรุปของวันนี้
การเลือกกลยุทธ์ Branch ที่เหมาะสมขึ้นอยู่กับทีม โปรเจกต์ และโมเดลการ Deploy ของคุณ ไม่มีโซลูชันเดียวที่ใช้ได้กับทุกกรณี เริ่มต้นด้วยกลยุทธ์ที่เรียบง่ายอย่าง GitHub Flow และพัฒนาไปตามความจำเป็น หัวใจสำคัญคือความสม่ำเสมอและการสื่อสาร
ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:
- git checkout -b branch-name
- git merge –no-ff
- git cherry-pick
- git branch -m
- git branch –merged
- git branch –no-merged
- git push origin –delete
- กลับไปยังสารบัญ
- วันก่อนหน้า: หลายคนทำงานใน Remote Repository เดียวกัน
- วันถัดไป: ทำความเข้าใจขั้นตอน Fork และ Pull Request บน GitHub