Ngày 25: Sử dụng GitHub Remote Repository - Khái niệm
Dựa trên những kiến thức cơ bản từ Ngày 24, bài viết này khám phá các khái niệm và quy trình làm việc quan trọng khi làm việc với GitHub remote repository.
Hiểu Remote Tracking Branch
Remote tracking branch là các tham chiếu tới trạng thái của các remote branch. Chúng tuân theo mẫu đặt tên <remote>/<branch>:
# List all branches including remote tracking branches
git branch -a
# Example output:
# * main
# feature
# remotes/origin/main
# remotes/origin/feature
Fetch so với Pull
Fetch
Tải xuống các object và ref từ một repository khác nhưng không merge:
git fetch origin
Thao tác này cập nhật các remote tracking branch của bạn nhưng không thay đổi working directory của bạn.
Pull
Fetch + Merge trong một lệnh:
git pull origin main
# Equivalent to:
git fetch origin
git merge origin/main
Hoặc với rebase:
git pull --rebase origin main
Upstream Branch
Thiết lập theo dõi upstream để đơn giản hóa push/pull:
# Set upstream when pushing
git push -u origin feature-branch
# Now you can just use:
git push
git pull
Kiểm tra cấu hình upstream:
git branch -vv
Theo dõi Remote Branch
Tạo Local Branch từ 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
Hủy theo dõi một Remote Branch
git branch --unset-upstream
Push với tên Branch khác nhau
Push local branch lên remote với tên khác:
git push origin local-branch:remote-branch
Xóa Remote Branch
Xóa một remote branch:
# Method 1
git push origin --delete branch-name
# Method 2
git push origin :branch-name
Làm việc với nhiều Remote
Bạn có thể có nhiều remote (ví dụ: origin và 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
Hiểu hành vi của git push
Các chế độ push khác nhau:
# 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 và sự nguy hiểm của nó
Khi nào nên Force Push
- Bạn đã rebase feature branch của mình
- Bạn là người duy nhất làm việc trên branch
- Bạn cần sửa một lỗi trong các commit đã push
Cách 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 chỉ thành công nếu remote không thay đổi kể từ lần fetch cuối cùng của bạn.
Các thực hành tốt nhất khi Force Push
- Không bao giờ force push lên main/master
- Thông báo trước khi force push các branch dùng chung
- Dùng –force-with-lease thay vì -f
- Tạo branch sao lưu trước khi force push
- Chỉ force push các feature branch của chính bạn
Xử lý các Branch bị phân kỳ
Khi local và remote đã phân kỳ:
# 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
Dọn dẹp các tham chiếu Remote Tracking
Loại bỏ các remote-tracking branch đã lỗi thời:
# See stale branches
git remote prune origin --dry-run
# Remove stale branches
git remote prune origin
# Or during fetch
git fetch --prune
Bare so với Non-Bare Repository
Non-Bare (Working Repository)
- Có working directory
- Dùng cho phát triển
- Là thứ bạn clone thông thường
Bare Repository
- Không có working directory
- Chỉ có dữ liệu Git
- Dùng trên máy chủ
# Create bare repository
git clone --bare project project.git
# Or init as bare
git init --bare project.git
Các mẫu quy trình làm việc Git
Quy trình tập trung (Centralized Workflow)
Mọi người làm việc trên branch main:
git pull origin main
# Make changes
git add .
git commit -m "Changes"
git push origin main
Quy trình Feature Branch
Mỗi tính năng có branch riêng:
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
Quy trình Gitflow
Sử dụng nhiều branch dài hạn:
- main: Mã sẵn sàng cho production
- develop: Branch tích hợp
- feature/: Phát triển tính năng
- release/: Chuẩn bị phát hành
- hotfix/: Sửa lỗi khẩn cấp
Quy trình Pull Request
- Fork repository (đối với mã nguồn mở)
- Clone fork của bạn
- Tạo feature branch
git checkout -b feature-name - Thực hiện thay đổi và commit
- Push lên fork của bạn
git push origin feature-name - Tạo pull request trên GitHub
- Thảo luận và review mã
- Thực hiện các thay đổi được yêu cầu
- Merge khi được phê duyệt
Giữ Fork luôn cập nhật
# 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
Giải quyết xung đột trong 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
Tổng kết hôm nay
Hiểu các khái niệm remote repository là điều then chốt cho sự cộng tác hiệu quả. Dù làm việc trong một nhóm hay đóng góp cho mã nguồn mở, việc biết cách quản lý remote, theo dõi branch và xử lý xung đột sẽ giúp bạn trở thành một người dùng Git hiệu quả hơn.
Hãy để tôi sắp xếp lại các lệnh và tham số Git đã học hôm nay:
- 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
- Quay lại Mục lục
- Ngày trước: Sử dụng GitHub Remote Repository - Bắt đầu
- Ngày tiếp theo: Nhiều người cùng làm việc trên một Remote Repository