วันที่ 25: การใช้ GitHub Remote Repository - แนวคิด
ต่อยอดจากพื้นฐานในวันที่ 24 บทความนี้จะสำรวจแนวคิดและ Workflow ที่สำคัญเมื่อทำงานกับ GitHub Remote Repository
ทำความเข้าใจ Remote Tracking Branch
Remote Tracking Branch คือการอ้างอิงไปยังสถานะของ Remote Branch โดยใช้รูปแบบการตั้งชื่อ <remote>/<branch>:
# List all branches including remote tracking branches
git branch -a
# Example output:
# * main
# feature
# remotes/origin/main
# remotes/origin/feature
Fetch เทียบกับ Pull
Fetch
ดาวน์โหลด Object และ Ref จาก Repository อื่น แต่ไม่ทำการ Merge:
git fetch origin
สิ่งนี้จะอัปเดต Remote Tracking Branch ของคุณ แต่ไม่เปลี่ยน Working Directory ของคุณ
Pull
Fetch + Merge ในคำสั่งเดียว:
git pull origin main
# Equivalent to:
git fetch origin
git merge origin/main
หรือใช้ร่วมกับ Rebase:
git pull --rebase origin main
Upstream Branch
ตั้งค่า Upstream Tracking เพื่อให้ Push/Pull ง่ายขึ้น:
# Set upstream when pushing
git push -u origin feature-branch
# Now you can just use:
git push
git pull
ตรวจสอบการกำหนดค่า Upstream:
git branch -vv
การติดตาม Remote Branch
สร้าง Local Branch จาก Remote
# Option 1: Automatic tracking
git checkout feature-branch
# If feature-branch exists on remote, Git auto-creates and tracks it
# Option 2: Explicit tracking
git checkout -b feature-branch origin/feature-branch
# Option 3: Using track flag
git checkout --track origin/feature-branch
ยกเลิกการติดตาม Remote Branch
git branch --unset-upstream
Push ด้วยชื่อ Branch ที่ต่างกัน
Push Local Branch ไปยัง Remote ด้วยชื่อที่ต่างกัน:
git push origin local-branch:remote-branch
การลบ Remote Branch
ลบ Remote Branch:
# Method 1
git push origin --delete branch-name
# Method 2
git push origin :branch-name
การทำงานกับหลาย Remote
คุณสามารถมี Remote ได้หลายตัว (เช่น origin และ upstream):
# Add upstream remote
git remote add upstream https://github.com/original/repo.git
# Fetch from upstream
git fetch upstream
# Merge upstream changes
git merge upstream/main
# Push to your origin
git push origin main
ทำความเข้าใจพฤติกรรมของ git push
โหมด Push แบบต่าง ๆ:
# Push current branch to matching branch on remote
git config push.default simple # Default in Git 2.0+
# Push all matching branches
git config push.default matching
# Push current branch to branch with same name
git config push.default current
# No push without explicit ref spec
git config push.default nothing
Force Push และอันตรายของมัน
เมื่อใดควร Force Push
- คุณได้ Rebase Feature Branch ของคุณแล้ว
- คุณเป็นคนเดียวที่ทำงานบน Branch นั้น
- คุณจำเป็นต้องแก้ไขข้อผิดพลาดใน Commit ที่ Push ไปแล้ว
วิธี Force Push
# Standard force push (dangerous!)
git push -f origin feature-branch
# Safer: force push with lease
git push --force-with-lease origin feature-branch
--force-with-lease จะสำเร็จก็ต่อเมื่อ Remote ไม่มีการเปลี่ยนแปลงตั้งแต่ครั้งล่าสุดที่คุณ Fetch
แนวปฏิบัติที่ดีที่สุดสำหรับ Force Push
- ห้าม Force Push ไปยัง main/master เด็ดขาด
- สื่อสาร ก่อน Force Push Branch ที่แชร์กัน
- ใช้ –force-with-lease แทน -f
- สร้าง Branch สำรอง ก่อน Force Push
- Force Push เฉพาะ Feature Branch ของคุณเองเท่านั้น
การจัดการ Branch ที่แยกออกจากกัน
เมื่อ Local และ Remote แยกออกจากกัน (Diverge):
# Check the situation
git status
# Option 1: Merge remote changes
git pull origin main
# Option 2: Rebase on remote changes
git pull --rebase origin main
# Option 3: Force push (if appropriate)
git push --force-with-lease origin feature-branch
การเก็บกวาดการอ้างอิง Remote Tracking ที่เก่า
ลบ Remote Tracking Branch ที่เก่าและไม่ใช้แล้ว:
# See stale branches
git remote prune origin --dry-run
# Remove stale branches
git remote prune origin
# Or during fetch
git fetch --prune
Repository แบบ Bare เทียบกับ Non-Bare
Non-Bare (Working Repository)
- มี Working Directory
- ใช้สำหรับการพัฒนา
- เป็นสิ่งที่คุณ Clone ตามปกติ
Bare Repository
- ไม่มี Working Directory
- มีเฉพาะข้อมูล Git
- ใช้บนเซิร์ฟเวอร์
# Create bare repository
git clone --bare project project.git
# Or init as bare
git init --bare project.git
รูปแบบ Git Workflow
Centralized Workflow
ทุกคนทำงานบน Main Branch:
git pull origin main
# Make changes
git add .
git commit -m "Changes"
git push origin main
Feature Branch Workflow
แต่ละ Feature มี Branch ของตัวเอง:
git checkout -b feature-x
# Work on feature
git push origin feature-x
# Create pull request
# After merge, delete branch
git branch -d feature-x
git push origin --delete feature-x
Gitflow Workflow
ใช้ Branch อายุยาวหลาย Branch:
- main: โค้ดที่พร้อมใช้งานจริง (Production-ready)
- develop: Branch สำหรับการรวม (Integration)
- feature/: การพัฒนา Feature
- release/: การเตรียม Release
- hotfix/: การแก้ไขเร่งด่วน
Pull Request Workflow
- Fork Repository (สำหรับ Open Source)
- Clone Fork ของคุณ
- สร้าง Feature Branch
git checkout -b feature-name - ทำการเปลี่ยนแปลงและ Commit
- Push ไปยัง Fork ของคุณ
git push origin feature-name - สร้าง Pull Request บน GitHub
- สนทนาและ Review โค้ด
- ทำการเปลี่ยนแปลงตามที่ขอ
- Merge เมื่อได้รับการอนุมัติ
การทำให้ Fork ทันสมัย
# Add upstream remote (once)
git remote add upstream https://github.com/original/repo.git
# Fetch upstream changes
git fetch upstream
# Merge upstream/main into your main
git checkout main
git merge upstream/main
# Push to your fork
git push origin main
การแก้ Conflict ใน Pull Request
# Update your branch with target branch
git fetch origin
git checkout feature-branch
git merge origin/main
# Resolve conflicts
# ... edit files ...
git add .
git commit -m "Resolve conflicts"
# Push updated branch
git push origin feature-branch
สรุปของวันนี้
การทำความเข้าใจแนวคิดของ Remote Repository เป็นสิ่งสำคัญสำหรับการทำงานร่วมกันอย่างมีประสิทธิภาพ ไม่ว่าคุณจะทำงานเป็นทีมหรือมีส่วนร่วมใน Open Source การรู้วิธีจัดการ Remote, ติดตาม Branch และจัดการ Conflict จะทำให้คุณเป็นผู้ใช้ Git ที่มีประสิทธิภาพมากขึ้น
ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:
- git fetch origin
- git pull origin main
- git pull –rebase
- git push -u origin branch
- git push origin –delete branch
- git push –force-with-lease
- git branch -vv
- git branch –unset-upstream
- git remote prune origin
- git fetch –prune
- git clone –bare
- กลับไปยังสารบัญ
- วันก่อนหน้า: การใช้ GitHub Remote Repository - เริ่มต้นใช้งาน
- วันถัดไป: หลายคนทำงานใน Remote Repository เดียวกัน