วันที่ 24: การใช้ GitHub Remote Repository - เริ่มต้นใช้งาน
ปัจจุบัน GitHub เป็นแพลตฟอร์มจัดการ Git ออนไลน์ที่ได้รับการยอมรับอย่างกว้างขวางที่สุดในโลก ประกอบด้วยระบบ Git Remote Repository ที่สมบูรณ์ กลไกและรายงานการติดตาม Issue ที่ครอบคลุม และโปรเจกต์ Open Source นับพันที่ใช้ GitHub สำหรับการควบคุมเวอร์ชันด้วย Git แม้แต่ในหลายบริษัท GitHub ก็ถูกนำมาใช้เป็นแพลตฟอร์ม Git หลัก บทความนี้จะแนะนำการใช้งาน GitHub ขั้นพื้นฐานที่สุดให้กับคุณ
GitHub คืออะไร?
GitHub เป็นแพลตฟอร์มบนเว็บที่:
- โฮสต์ Git Repository บนคลาวด์
- มีเครื่องมือสำหรับการทำงานร่วมกัน (Issue, Pull Request, Project Board)
- มี CI/CD ผ่าน GitHub Actions
- ช่วยให้ Review โค้ดและสนทนากันได้
- ให้โฮสติ้งฟรีสำหรับโปรเจกต์ Open Source
- ทำหน้าที่เป็นแพลตฟอร์มสังคมสำหรับนักพัฒนาและโปรเจกต์ Open Source
การสร้างบัญชี GitHub
- ไปที่ https://github.com
- คลิก “Sign up”
- ทำตามขั้นตอนการลงทะเบียน
- ยืนยันที่อยู่อีเมลของคุณ
การสร้าง Repository ใหม่บน GitHub
มีแนวทางหลักสองแบบเมื่อสร้าง GitHub Repository:
- Repository เปล่า (ไม่มี Commit เริ่มต้น): เหมาะที่สุดเมื่อคุณมี Local Repository ที่มีอยู่แล้วเพื่อ Push ขึ้นไป
- Repository ที่ Initialize แล้ว (มี Commit เริ่มต้น): GitHub จะสร้างไฟล์ README, .gitignore และ/หรือ License ให้คุณ
การสร้าง Repository เปล่า
- คลิกไอคอน “+” ที่มุมขวาบน
- เลือก “New repository”
- กรอกข้อมูล:
- ชื่อ Repository
- คำอธิบาย (ไม่บังคับ)
- Public หรือ Private
- อย่าเลือก “Initialize this repository with a README”
- ปล่อย .gitignore และ License ไว้เป็น “None”
- คลิก “Create repository”
หลังจากสร้างเสร็จ GitHub จะแสดงคำแนะนำสำหรับการ Push Repository ที่มีอยู่ขึ้นไป
การสร้าง Repository ที่ Initialize แล้ว
- ทำตามขั้นตอนที่ 1-3 ข้างต้น
- เลือก “Initialize this repository with a README”
- เลือกเพิ่มเติมได้:
- เทมเพลต .gitignore สำหรับภาษาของคุณ
- License สำหรับโปรเจกต์ของคุณ
- คลิก “Create repository”
GitHub จะสร้าง Commit เริ่มต้นพร้อมไฟล์ที่เลือกไว้
การเชื่อมต่อ Local Repository กับ GitHub
ตัวเลือกที่ 1: Push Repository ที่มีอยู่
# Add remote
git remote add origin https://github.com/username/repo-name.git
# Push to GitHub
git push -u origin main
ตัวเลือกที่ 2: Clone จาก GitHub
# Clone the repository
git clone https://github.com/username/repo-name.git
# Navigate to the directory
cd repo-name
ทำความเข้าใจ Remote
Remote คือการอ้างอิงไปยัง Repository ที่โฮสต์อยู่ที่อื่น คำสั่งที่พบบ่อย:
# List remotes
git remote -v
# Add a remote
git remote add <name> <url>
# Remove a remote
git remote remove <name>
# Rename a remote
git remote rename <old> <new>
# Change remote URL
git remote set-url origin <new-url>
การ Push ไปยัง GitHub
Push Commit ของคุณไปยัง GitHub:
# Push to the default branch
git push origin main
# Push all branches
git push --all
# Push tags
git push --tags
แฟล็ก -u ใช้ตั้งค่า Upstream Tracking:
git push -u origin main
# After this, you can just use:
git push
การ Pull จาก GitHub
รับการอัปเดตจาก GitHub:
# Fetch changes
git fetch origin
# Fetch and merge
git pull origin main
# If upstream is set
git pull
วิธีการยืนยันตัวตน
HTTPS
ต้องใช้ชื่อผู้ใช้และรหัสผ่าน/Token:
https://github.com/username/repo-name.git
SSH
ต้องตั้งค่า SSH Key:
git@github.com:username/repo-name.git
การตั้งค่า SSH Key:
# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# Add to ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Copy public key
cat ~/.ssh/id_ed25519.pub
# Add this key to GitHub settings
Personal Access Token
สำหรับการยืนยันตัวตนผ่าน HTTPS:
- ไปที่ GitHub Settings > Developer settings > Personal access tokens
- สร้าง Token ใหม่
- ใช้ Token เป็นรหัสผ่านเมื่อทำการ Push
README.md
README ที่ดีควรประกอบด้วย:
- คำอธิบายโปรเจกต์
- คำแนะนำการติดตั้ง
- ตัวอย่างการใช้งาน
- แนวทางการมีส่วนร่วม
- ข้อมูล License
ตัวอย่าง:
# Project Name
Brief description of the project.
## Installation
\`\`\`bash
npm install
\`\`\`
## Usage
\`\`\`javascript
const example = require('example');
\`\`\`
## Contributing
Pull requests are welcome!
## License
MIT
.gitignore
ไฟล์สำคัญที่ควรละเว้น:
# Dependencies
node_modules/
vendor/
# Environment
.env
.env.local
# Build outputs
dist/
build/
*.log
# IDE
.vscode/
.idea/
*.swp
คุณสมบัติของ GitHub
Issues
ติดตาม Bug, การปรับปรุง และงานต่าง ๆ:
- สร้างชื่อเรื่องที่สื่อความหมาย
- ระบุขั้นตอนการทำซ้ำสำหรับ Bug
- ใช้ Label เพื่อจัดระเบียบ
- อ้างอิง Commit และ Pull Request
Wiki
เอกสารสำหรับโปรเจกต์ของคุณ:
- คู่มือเริ่มต้นใช้งาน
- เอกสาร API
- คำถามที่พบบ่อย (FAQ)
- แผนภาพสถาปัตยกรรม
Projects
จัดระเบียบงานด้วยบอร์ด Kanban:
- To Do
- In Progress
- Done
Releases
ทำเครื่องหมายเหตุการณ์สำคัญ:
- ไปที่ “Releases”
- คลิก “Draft a new release”
- เลือก Tag
- เพิ่ม Release Notes
- แนบไฟล์ Binary หากจำเป็น
แนวปฏิบัติที่ดีที่สุดสำหรับ Workflow บน GitHub
- Commit บ่อย ๆ: ทำ Commit ขนาดเล็กและมีเหตุผล
- เขียนข้อความที่ดี: อธิบายว่าทำไม ไม่ใช่แค่ทำอะไร
- Pull ก่อน Push: รับการเปลี่ยนแปลงล่าสุดก่อนเสมอ
- ใช้ Branch: อย่า Commit ลง main โดยตรง
- Review โค้ด: ใช้ Pull Request สำหรับการเปลี่ยนแปลง
งานที่พบบ่อย
การ Fork Repository
- คลิก “Fork” บนหน้า Repository
- Clone Fork ของคุณ:
git clone https://github.com/your-username/repo-name.git - เพิ่ม Upstream Remote:
git remote add upstream https://github.com/original-owner/repo-name.git
การ Sync Fork
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
สรุปของวันนี้
GitHub มอบแพลตฟอร์มที่ทรงพลังสำหรับการโฮสต์ Git Repository และการทำงานร่วมกับผู้อื่น การทำความเข้าใจวิธีเชื่อมต่อ Local Repository ของคุณกับ GitHub, การ Push และ Pull การเปลี่ยนแปลง และการใช้คุณสมบัติต่าง ๆ ของ GitHub เป็นสิ่งสำคัญสำหรับการพัฒนาซอฟต์แวร์สมัยใหม่
ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:
- git remote add
- git remote -v
- git remote remove
- git remote rename
- git push -u origin main
- git push –all
- git push –tags
- git pull origin main
- git fetch origin
- git clone
- กลับไปยังสารบัญ
- วันก่อนหน้า: แก้ไขประวัติเวอร์ชันที่ Commit แล้ว ตอนที่ 5 (rebase 2)
- วันถัดไป: การใช้ GitHub Remote Repository - แนวคิด