วันที่ 28: ทำความเข้าใจขั้นตอน Fork และ Pull Request บน GitHub
Fork และ Pull Request เป็นพื้นฐานของการทำงานร่วมกันใน Open Source บน GitHub บทความนี้จะอธิบายวิธีมีส่วนร่วมในโปรเจกต์ที่คุณไม่มีสิทธิ์เขียน
ทำความเข้าใจ Fork
Fork คือสำเนาที่สมบูรณ์ของ Repository ภายใต้บัญชี GitHub ของคุณ มันช่วยให้คุณ:
- ทดลองได้อย่างอิสระโดยไม่กระทบโปรเจกต์ต้นฉบับ
- เสนอการเปลี่ยนแปลงผ่าน Pull Request
- ปรับแต่งโปรเจกต์ให้ตรงกับความต้องการของคุณ
Fork เทียบกับ Clone เทียบกับ Branch
- Fork: คัดลอก Repository บน GitHub (บัญชีของคุณ)
- Clone: คัดลอก Repository มายังเครื่อง Local
- Branch: สร้างเส้นทางการพัฒนาที่แยกออกไป
การ Fork Repository
บน GitHub
- ไปยัง Repository นั้น
- คลิกปุ่ม “Fork” (ขวาบน)
- เลือกปลายทางที่จะ Fork ไป (บัญชีของคุณหรือองค์กร)
- รอให้ GitHub สร้าง Fork
การ Clone Fork ของคุณ
# Clone your fork
git clone https://github.com/your-username/repo-name.git
# Navigate to directory
cd repo-name
# Add upstream remote
git remote add upstream https://github.com/original-owner/repo-name.git
# Verify remotes
git remote -v
# origin https://github.com/your-username/repo-name.git (fetch)
# origin https://github.com/your-username/repo-name.git (push)
# upstream https://github.com/original-owner/repo-name.git (fetch)
# upstream https://github.com/original-owner/repo-name.git (push)
Workflow Pull Request แบบครบวงจร
ขั้นตอนที่ 1: Sync Fork ของคุณ
# Fetch upstream changes
git fetch upstream
# Checkout your main branch
git checkout main
# Merge upstream changes
git merge upstream/main
# Push to your fork
git push origin main
ขั้นตอนที่ 2: สร้าง Feature Branch
# Create and checkout feature branch
git checkout -b fix-typo-in-readme
# Make your changes
# Edit files...
# Stage changes
git add README.md
# Commit changes
git commit -m "Fix typo in installation instructions"
ขั้นตอนที่ 3: Push ไปยัง Fork ของคุณ
# Push branch to your fork
git push origin fix-typo-in-readme
ขั้นตอนที่ 4: สร้าง Pull Request
- ไปที่ Fork ของคุณบน GitHub
- คลิกปุ่ม “Compare & pull request”
- กรอกรายละเอียดของ PR:
- ชื่อเรื่อง: คำอธิบายที่ชัดเจนและกระชับ
- คำอธิบาย: อะไร ทำไม และอย่างไร
- อ้างอิง Issue หากเกี่ยวข้อง
- คลิก “Create pull request”
ขั้นตอนที่ 5: ตอบสนองต่อ Feedback
# Make requested changes
# Edit files...
# Commit changes
git add .
git commit -m "Address review feedback"
# Push updates
git push origin fix-typo-in-readme
# PR automatically updates!
ขั้นตอนที่ 6: หลังจาก Merge
# Switch to main
git checkout main
# Pull merged changes
git pull upstream main
# Push to your fork
git push origin main
# Delete feature branch
git branch -d fix-typo-in-readme
git push origin --delete fix-typo-in-readme
การทำให้ Fork ทันสมัย
การ Sync เป็นประจำ
# Fetch and merge in one command
git checkout main
git pull upstream main
git push origin main
การ Sync อัตโนมัติ
ใช้ปุ่ม “Fetch upstream” บนหน้า Fork ของคุณบน GitHub หรือตั้งค่า GitHub Actions:
# .github/workflows/sync-upstream.yml
name: Sync Fork
on:
schedule:
- cron: '0 0 * * *' # Daily
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: TobKed/github-forks-sync-action@v1
with:
upstream: original-owner/repo-name
target: your-username/repo-name
หลาย Commit ใน PR เดียว
วิธีที่ 1: Interactive Rebase
# Squash commits
git rebase -i HEAD~3
# In editor, change 'pick' to 'squash' for commits to combine
# Save and edit commit message
# Force push (since you're rewriting history)
git push -f origin feature-branch
วิธีที่ 2: Soft Reset
# Reset to before your commits
git reset --soft HEAD~3
# Create single commit
git commit -m "Complete feature implementation"
# Force push
git push -f origin feature-branch
การรับมือกับ Merge Conflict ใน PR
# Sync with upstream
git fetch upstream
# Rebase on upstream/main
git rebase upstream/main
# Resolve conflicts
# Edit conflicting files
git add resolved-files
git rebase --continue
# Force push
git push -f origin feature-branch
Draft Pull Request
สร้าง Draft PR เพื่อรับ Feedback ตั้งแต่เนิ่น ๆ:
- เมื่อสร้าง PR ให้คลิกลูกศรลงบน “Create pull request”
- เลือก “Create draft pull request”
- ทำเครื่องหมายว่าพร้อมเมื่อเสร็จสมบูรณ์
ประโยชน์:
- ได้รับ Feedback ตั้งแต่เนิ่น ๆ
- แสดงงานที่กำลังดำเนินการอยู่
- ป้องกันการ Merge โดยไม่ได้ตั้งใจ
แนวปฏิบัติที่ดีที่สุดสำหรับ Pull Request
ก่อนสร้าง PR
- โค้ดทำงานได้และผ่านการทดสอบ
- ทำตามแนวทางรูปแบบของโปรเจกต์
- เอกสารได้รับการอัปเดต
- Commit สะอาดและมีเหตุผล
- Branch ทันสมัยกับ main
การเขียน PR ที่ดี
ชื่อเรื่อง:
❌ Fix stuff
✅ Fix null pointer exception in user login
คำอธิบาย:
## What
Fixes a null pointer exception that occurs when users login with empty passwords.
## Why
Bug #123 reported crashes on invalid login attempts.
## How
Added null check before password validation.
## Testing
- Tested with empty password
- Tested with valid password
- Tested with invalid password
Closes #123
ระหว่างการ Review
- ตอบทุกความคิดเห็น
- ถามคำถามหากไม่ชัดเจน
- ทำการเปลี่ยนแปลงตามที่ขอโดยเร็ว
- เปิดรับข้อเสนอแนะ
- ขอบคุณผู้ Review
การมีส่วนร่วมใน Open Source
ค้นหาโปรเจกต์
- GitHub Explore: https://github.com/explore
- First Timers Only: https://www.firsttimersonly.com/
- Good First Issue: ค้นหาบน GitHub
- Up For Grabs: https://up-for-grabs.net/
ก่อนเริ่มมีส่วนร่วม
- อ่าน
CONTRIBUTING.md - ตรวจสอบ Issue ที่เปิดอยู่
- ดู PR ที่มีอยู่
- ทำความเข้าใจหลักปฏิบัติ (Code of Conduct)
- ตั้งค่าสภาพแวดล้อมสำหรับการพัฒนา
PR แรกของคุณ
เริ่มจากสิ่งเล็ก ๆ:
- แก้คำสะกดผิด
- ปรับปรุงเอกสาร
- เพิ่ม Test
- แก้ Bug ที่เห็นได้ชัดเจน
การสร้างชื่อเสียง
- เริ่มจากเอกสาร
- รายงาน Bug ที่ดี
- ช่วยเหลือผู้อื่นใน Issue
- ส่ง PR ที่มีคุณภาพ
- อดทนและให้เกียรติผู้อื่น
ข้อผิดพลาดทั่วไปที่ควรหลีกเลี่ยง
- ไม่ Sync Fork: อัปเดตก่อนสร้าง PR เสมอ
- เปลี่ยนแปลงมากเกินไป: ทำให้ PR มีจุดเน้น
- ไม่มีคำอธิบาย: อธิบายว่าอะไรและทำไม
- Force Push ไปยัง main: ห้ามทำเด็ดขาด
- โต้เถียงกับผู้ดูแล: ให้เกียรติผู้อื่น
- ไม่ทดสอบ: ทดสอบการเปลี่ยนแปลงของคุณเสมอ
สรุปของวันนี้
Fork และ Pull Request ช่วยให้การทำงานร่วมกันใน Open Source เกิดขึ้นได้ในวงกว้าง การทำความเข้าใจ Workflow นี้จะเปิดประตูสู่โปรเจกต์นับพันที่คุณสามารถมีส่วนร่วมได้ เริ่มจากสิ่งเล็ก ๆ อดทน และจำไว้ว่าผู้มีส่วนร่วมที่มีประสบการณ์ทุกคนก็เคยเป็นมือใหม่มาก่อนเช่นกัน
ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:
- git clone
- git remote add upstream
- git fetch upstream
- git merge upstream/main
- git push origin branch-name
- git push -f origin branch-name
- git rebase -i HEAD~n
- git reset –soft HEAD~n
- กลับไปยังสารบัญ
- วันก่อนหน้า: การควบคุมเวอร์ชันด้วย Branch ใน Remote Repository เดียวกัน
- วันถัดไป: วิธีนำเข้าโปรเจกต์ Subversion เข้าสู่ Git Repository