Skip to Content

Remote Repository

Remote là gì?

Remote là phiên bản repository được lưu trữ trên server (GitHub, GitLab, Bitbucket…). Cho phép:

  • Backup code trên cloud
  • Chia sẻ code với người khác
  • Collaboration trong team

Xem danh sách Remote

git remote -v

Kết quả:

origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)

Thêm Remote

git remote add <name> <url>

Ví dụ:

git remote add origin https://github.com/user/repo.git git remote add upstream https://github.com/original/repo.git

Đổi URL Remote

git remote set-url origin https://github.com/user/new-repo.git

Xóa Remote

git remote remove origin

Push - Đẩy code lên Remote

# Push lần đầu (thiết lập upstream) git push -u origin main # Các lần sau git push # Push branch cụ thể git push origin feature-login # Push tất cả branches git push --all origin

Pull - Kéo code về

# Pull từ remote mặc định git pull # Pull từ remote và branch cụ thể git pull origin main

💡 git pull = git fetch + git merge

Fetch - Lấy thông tin không merge

# Fetch từ tất cả remotes git fetch --all # Fetch từ remote cụ thể git fetch origin # Xem branches remote git branch -r

Làm việc với nhiều Remote

Ví dụ: Fork workflow

# origin = fork của bạn git remote add origin https://github.com/you/repo.git # upstream = repo gốc git remote add upstream https://github.com/original/repo.git # Sync với upstream git fetch upstream git checkout main git merge upstream/main

Xóa branch trên Remote

git push origin --delete feature-login

Xem thông tin Remote

git remote show origin

Tiếp theo

Học cách Hoàn tác thay đổi khi có lỗi xảy ra.

Last updated on