Git Handbook

04 · Remote & Collaboration

Remote, origin, fetch, pull, push, upstream tracking và cách suy nghĩ khi nhiều developer cùng làm việc.

1. Remote chỉ là tên + URL/path

git-remote

Quản lý remote repository definitions.
Official docs ↗
Cú pháp
git remote [-v] | git remote add <name> <url>
Hiểu đơn giản
origin là convention phổ biến, không phải từ khóa đặc biệt.
git remote -v
git remote add origin git@github.com:org/project.git
git remote set-url origin NEW_URL
git remote remove origin

2. Fetch trước, integrate sau

git-fetch

Tải refs/objects mới từ remote mà không tự merge vào branch hiện tại.
Official docs ↗
Cú pháp
git fetch [remote] [refspec]
Hiểu đơn giản
Sau fetch, bạn có thể review origin/main trước khi merge/rebase.
git fetch origin
git fetch --prune origin
git log --oneline main..origin/main
--prune dọn remote-tracking refs đã bị xóa trên remote.

git-pull

Fetch rồi integrate vào branch hiện tại.
Official docs ↗
Cú pháp
git pull [--rebase] [remote] [branch]
Hiểu đơn giản
Convenience command. Hãy hiểu fetch + merge/rebase trước khi dùng pull theo phản xạ.
git pull origin main
git pull --rebase origin main

3. Push

git-push

Cập nhật refs trên remote bằng commit local.
Official docs ↗
Cú pháp
git push [-u] <remote> <branch>
Hiểu đơn giản
Push có thể bị reject nếu remote chứa lịch sử mà local chưa tích hợp.
git push -u origin feature/login
git push
git push origin --delete feature/old
Không dùng git push --force như thói quen. Nếu thật sự rewrite feature branch, ưu tiên --force-with-lease.

4. Upstream / tracking branch

Khi chạy:

git push -u origin feature/login

Git thiết lập upstream để lần sau có thể dùng ngắn:

git push
git pull

Kiểm tra:

git branch -vv
git status -sb

5. Mô hình collaboration

Developer A local ----                                               central remote
                       /
Developer B local ----/

A: commit -> push
B: fetch -> inspect -> integrate -> push

Bạn sẽ thực hành mô hình này hoàn toàn local trong Lab 5.