Git และ GitHub Workflow สำหรับโปรเจกต์เว็บ Git and GitHub Workflow for Web Projects

จัด branch, commit, pull request, conflict และ merge ให้เป็นระบบ เพื่อให้ main เป็นเวอร์ชันที่ deploy ได้เสมอ Organize branches, commits, PRs, and merges systematically to keep your production code deploy-ready at all times.

Pratchayawut Thansri
Pratchayawut Thansri
· อ่าน 6 นาที 6 min read
ภาพรวม Overview ขั้นตอนเวิร์กโฟลว์ Git Workflow คำสั่งสำคัญ Git Commands
Git Workflow Tech Illustration
ภาพประกอบแสดงแผนภูมิกิ่งก้านและการทำงานร่วมกันผ่าน Git Branch, Commits และ GitHub Pull Request Workflow illustration demonstrating branching, committing, and GitHub Pull Request paths

ในการพัฒนาซอฟต์แวร์ไม่ว่าจะทำคนเดียวหรือทำเป็นทีม ปัญหาที่พบบ่อยคือโค้ดพังโดยไม่รู้ตัวเมื่อมีการเขียนฟีเจอร์ใหม่ การนำ Git & GitHub Workflow มาประยุกต์ใช้จะช่วยปกป้องโค้ดสายหลัก (main branch) ให้ทำงานได้อย่างปลอดภัยและพร้อมใช้งาน (Deploy) ตลอดเวลา

หากคุณต้องการมีระบบจัดเก็บและรวมโค้ดที่มีประสิทธิภาพ ไร้กังวลเรื่องการทำโค้ดทับซ้อนหรือโค้ดหาย นี่คือคู่มือปฏิบัติการ Git Workflow ขั้นตอนการทำงานจริงที่คัดเลือกมาอย่างพรีเมียม

ทำไมเราไม่ควรดึงและแก้ไขโค้ดที่กิ่ง main โดยตรง?

กิ่ง main เปรียบเสมือนตึกหน้างานจริงที่เปิดให้บริการแก่ลูกค้า หากมีข้อผิดพลาดแม้เพียงบรรทัดเดียวเกิดขึ้น ทุกคนที่ใช้งานเว็บไซต์จะเห็นเว็บพังทันที ดังนั้น หลักการของ Git Workflow สากลกำหนดให้กิ่ง main ต้องเป็น "กิ่งที่ผ่านการทดสอบแล้วและพร้อมเปิดตัวเสมอ"


ขั้นตอนที่ 1: การสร้างกิ่งแยก (Branching)

ทุกครั้งที่คุณได้รับโจทย์ให้พัฒนาฟีเจอร์ใหม่ หรือแม้กระทั่งการแก้บั๊กย่อย ให้เปิด Terminal และสร้างกิ่งใหม่แยกจากกิ่งหลักเสมอ:

Git Bash / Terminal
# 1. ย้ายสายงานมาที่กิ่งหลัก main และดึงโค้ดล่าสุดจากคลาวด์ลงมาก่อน
git checkout main
git pull origin main

# 2. สร้างและสลับไปยังกิ่งงานใหม่ตามประเภทฟีเจอร์
git checkout -b feat/add-darkmode

ขั้นตอนที่ 2: การพัฒนาและบันทึกงาน (Commit)

เมื่อคุณเริ่มเขียนหรือแก้ไขโค้ด เมื่อเขียนเสร็จสิ้นแต่ละขั้นตอนย่อย ให้บันทึกการทำงานอย่างมีหลักการผ่าน Commit Messages ที่มีโครงสร้างชัดเจน:

Git Commit Format
# ตรวจสอบสถานะการแก้ไขไฟล์
git status

# เตรียมย้ายไฟล์เตรียมบันทึก
git add .

# บันทึกด้วย Semantic Commit Message
git commit -m "feat: implement theme toggling system and dark mode styles"
💡 รูปแบบการเขียน Commit สไตล์โปรแกรมเมอร์มืออาชีพ:
  • feat: เมื่อเพิ่มฟังก์ชันหรือลูกเล่นใหม่ในระบบ
  • fix: สำหรับการแก้บั๊กหรือซ่อมหน้าเว็บพัง
  • docs: การเขียนหรือแก้ไขคู่มือการทำงาน (README.md)
  • chore: ปรับแต่งการตั้งค่าระบบภายนอกหรืออัปเดต dependencies

ขั้นตอนที่ 3: ส่งงานขึ้นคลาวด์และสร้าง Pull Request (PR)

เมื่อพัฒนาฟีเจอร์สำเร็จและรันดูผลงานปกติในเครื่องแล้ว ให้ส่งกิ่งใหม่นี้ขึ้นสู่ GitHub เพื่อเข้าสู่กระบวนการตรวจรับความถูกต้อง:

Git Push Command
# ส่งกิ่งงานขึ้นเซิร์ฟเวอร์ GitHub
git push -u origin feat/add-darkmode

จากนั้นให้เปิดหน้าเว็บ GitHub Repository ของคุณ จะพบปุ่มแจ้งเตือนสีเหลือง Compare & pull request ให้คลิกเข้าไปเขียนคำอธิบายว่าคุณได้ทำอะไรไปบ้าง แล้วคลิกสร้าง Pull Request (PR)

ขั้นตอนที่ 4: การแก้ไข Merge Conflict (เมื่อไฟล์ชนกัน)

ปัญหายอดฮิตเกิดขึ้นเมื่อนักพัฒนา 2 คนแก้ไขไฟล์เดียวกันและบรรทัดเดียวกันพร้อมๆ กัน เมื่อเราพยายามกดรวมโค้ด (Merge) ระบบ GitHub จะฟ้องว่าเกิด Merge Conflict

วิธีการแก้ไขแบบถูกต้องและง่ายที่สุดคือแก้ไขผ่านโปรแกรม VS Code:

  1. ย้ายสายงานหลักกลับมารับโค้ดปัจจุบัน: git checkout main && git pull origin main
  2. ย้ายเข้ากิ่งฟีเจอร์ของเราและรวมกิ่ง main เข้ามา: git checkout feat/add-darkmode && git merge main
  3. VS Code จะไฮไลท์บรรทัดที่เป็นปัญหาเป็นสีเขียวและน้ำเงิน ให้เลือกทางออกที่เหมาะสม:
    • Accept Current Change: เลือกเฉพาะโค้ดที่เราเขียน
    • Accept Incoming Change: เลือกเฉพาะโค้ดของเพื่อนร่วมทีม
    • Accept Both Changes: เอาโค้ดของทั้งสองฝ่ายมาทำงานร่วมกัน
  4. ลบสัญลักษณ์ <<<<<<<, =======, >>>>>>> ออกให้หมด จากนั้นทำการ git add . และ git commit -m "chore: resolve merge conflicts with main" แล้วรัน git push อีกครั้ง

ขั้นตอนที่ 5: การกดรวมโค้ด (Merge) และดึงโค้ดกลับ

เมื่อตรวจสอบผ่านหน้าจอ PR บน GitHub แล้วไม่มีข้อขัดแย้งของโค้ด ให้คลิกปุ่ม Merge pull request และทำการลบกิ่งฟีเจอร์นั้นออกจากเซิร์ฟเวอร์ เพื่อให้หน้าจอสะอาด จากนั้นย้อนกลับมาที่เครื่องคอมพิวเตอร์ของคุณและเคลียร์กิ่งด้วยการรัน:

Git Sync Terminal
git checkout main
git pull origin main
git branch -d feat/add-darkmode

เพียงเท่านี้คุณก็ได้พัฒนาฟีเจอร์เสร็จสิ้นอย่างสมบูรณ์ และมีกิ่ง main ที่ทันสมัยและใช้งานได้เสมอครับ! 🌟

Whether working solo or in teams, code integrity is easily compromised when developers work directly on production lines. Adopting a structured Git & GitHub Workflow guarantees that your main branch remains stable, tested, and always deployable.

The Golden Rule: Never Commit to Main Directly

The main branch represents the live environment. An untested commit pushed directly to main can cause immediate downtime. Developers should always branch out, write code, run audits, and merge via Pull Requests.


Step 1: Create a Feature Branch

Before starting on any new features or bug fixes, fetch the latest code from remote and branch out:

Git Bash / Terminal
# 1. Update main branch
git checkout main
git pull origin main

# 2. Spawn feature branch
git checkout -b feat/add-darkmode

Step 2: Commit Semantically

Commit your work in small, logical increments. Use semantic naming prefixes to structure your project history:

Git Commit Format
# Check updated files
git status

# Stage files
git add .

# Semantic commit message
git commit -m "feat: implement theme toggling system and dark mode styles"
💡 Popular Semantic Prefixes:
  • feat: Adding a new feature or interactive element.
  • fix: Patching script exceptions or rendering bugs.
  • docs: Modifying manuals, READMEs, or documentation.
  • chore: Changing configuration settings or updating npm packages.

Step 3: Push and Open a Pull Request (PR)

Send your branch to the remote repository and initiate code review:

Git Push Command
# Push branch to GitHub
git push -u origin feat/add-darkmode

Open your repository on GitHub, click Compare & pull request, outline your changes, and submit the PR for code inspection.

Step 4: Handling Merge Conflicts

Merge conflicts occur when separate developers edit the same lines of a file concurrently. To resolve a conflict using VS Code:

  1. Fetch latest master changes: git checkout main && git pull origin main
  2. Merge main into feature branch: git checkout feat/add-darkmode && git merge main
  3. Resolve highlighted conflicting regions using VS Code's quick actions (e.g. Accept Current vs Accept Incoming).
  4. Save file, run git add ., and commit changes: git commit -m "chore: resolve conflicts" && git push.

Step 5: Merge and Cleanup

Once checked and approved, click Merge pull request on GitHub, then clean up your local branch stack:

Git Sync Terminal
git checkout main
git pull origin main
git branch -d feat/add-darkmode

ความคิดเห็น (1) Responses (1)

J
Jenny_Dev 1 day ago

เมื่อก่อนเจอปัญหา conflict แล้วกลัวมากค่ะ พออ่านสเต็ปการทำใน VS Code หน้านี้แล้ว เข้าใจวิธีการดึง main มาช่วย merge ในกิ่งเราเลย สภาพจิตใจดีขึ้นเยอะเลยค่ะ Merge conflicts used to scare me. Reading this guide makes merging main into the feature branch so logical and easy. Thank you!

ยินดีต้อนรับสู่หน้านิยายความรู้ครับ! 🐉
Cute Dragon Mascot