Git 完全指南 / 05 - 远程仓库:remote、push、pull、fetch、clone
第五章:远程仓库
远程仓库是团队协作的桥梁,掌握远程操作是 Git 日常工作的核心。
5.1 远程仓库概念
远程仓库(Remote Repository)是托管在网络上的项目副本,通常位于 GitHub、GitLab、Bitbucket 等平台。默认远程仓库名为 origin。
┌─────────────────┐ ┌─────────────────┐
│ 本地仓库 │ push │ 远程仓库 │
│ (Local) │────────►│ (origin) │
│ │◄────────│ │
│ 工作区/暂存区 │ pull │ GitHub/GitLab │
│ 本地提交历史 │ │ 远程提交历史 │
└─────────────────┘ └─────────────────┘
5.2 管理远程仓库
5.2.1 查看远程
# 查看远程仓库名
$ git remote
origin
# 查看远程仓库详细信息(URL 和跟踪信息)
$ git remote -v
origin git@github.com:user/repo.git (fetch)
origin git@github.com:user/repo.git (push)
# 查看远程仓库详细信息
$ git remote show origin
* remote origin
Fetch URL: git@github.com:user/repo.git
Push URL: git@github.com:user/repo.git
HEAD branch: main
Remote branches:
main tracked
develop tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (up to date)
5.2.2 添加远程
# 添加远程仓库
$ git remote add origin git@github.com:user/repo.git
# 添加多个远程(如同时推送到 GitHub 和 GitLab)
$ git remote add github git@github.com:user/repo.git
$ git remote add gitlab git@gitlab.com:user/repo.git
# 添加只读远程(如上游仓库)
$ git remote add upstream git@github.com:original/repo.git
5.2.3 修改远程
# 修改远程 URL
$ git remote set-url origin git@github.com:user/new-repo.git
# 修改远程名称
$ git remote rename origin upstream
# 设置 push URL(fetch 和 push 使用不同地址)
$ git remote set-url --push origin git@github.com:user/repo.git
5.2.4 删除远程
# 删除远程仓库配置
$ git remote remove origin
5.3 获取远程更新
5.3.1 git fetch
git fetch 下载远程仓库的最新数据,但不自动合并到本地分支。
# 获取默认远程(origin)的更新
$ git fetch
# 获取指定远程
$ git fetch origin
# 获取所有远程
$ git fetch --all
# 获取并清理已删除的远程分支
$ git fetch --prune
$ git fetch -p
# 获取特定分支
$ git fetch origin main
# 获取特定标签
$ git fetch origin tag v1.0.0
# 获取所有标签
$ git fetch --tags
# 浅获取(只获取最近 N 个提交)
$ git fetch --depth=10
5.3.2 git pull
git pull = git fetch + git merge(或 git rebase)
# 拉取并合并(默认方式)
$ git pull
# 等价于
$ git fetch origin
$ git merge origin/main
# 使用 rebase 方式拉取(推荐,保持线性历史)
$ git pull --rebase
# 等价于
$ git fetch origin
$ git rebase origin/main
# 设置默认使用 rebase
$ git config --global pull.rebase true
# 拉取指定远程分支
$ git pull origin develop
# 拉取并允许不相关历史
$ git pull origin main --allow-unrelated-histories
fetch vs pull 对比
| 特性 | git fetch | git pull |
|---|---|---|
| 下载远程数据 | ✅ | ✅ |
| 自动合并 | ❌ | ✅ |
| 安全性 | 高(可先审查) | 较低(自动合并可能冲突) |
| 适用场景 | 审查后再合并 | 确认无冲突时快速更新 |
| 工作流 | fetch → diff → merge | pull(一步到位) |
💡 推荐:在重要分支上使用
git fetch+ 手动合并,避免意外冲突。
5.4 推送本地更改
5.4.1 git push
# 推送到默认远程分支
$ git push
# 推送到指定远程和分支
$ git push origin main
# 推送并设置上游跟踪
$ git push -u origin feature
# 之后可以直接 git push
# 强制推送(危险!覆盖远程历史)
$ git push --force
$ git push -f
# 安全强制推送(如果远程有新提交则失败)
$ git push --force-with-lease
# 推送所有分支
$ git push --all
# 推送标签
$ git push --tags
# 推送单个标签
$ git push origin v1.0.0
# 删除远程分支
$ git push origin --delete feature-branch
$ git push origin :feature-branch
5.4.2 上游跟踪(Upstream Tracking)
# 查看跟踪关系
$ git branch -vv
* main abc1234 [origin/main] Latest commit
feature def5678 [origin/feature: ahead 2, behind 1] Feature work
# 设置上游跟踪
$ git branch --set-upstream-to=origin/main main
$ git branch -u origin/main main
# 推送时自动设置跟踪
$ git push -u origin feature
push 相关配置
# 默认推送当前分支名
$ git config --global push.default current
# 推送当前分支(必须同名)
$ git config --global push.default simple
# 推送所有同名分支
$ git config --global push.default matching
# 自动设置远程跟踪
$ git config --global push.autoSetupRemote true
5.5 多远程仓库管理
5.5.1 Fork 工作流
上游仓库 (upstream)
│
├── Fork 到你的 GitHub (origin)
│ │
│ └── 本地克隆 (local)
│
└── 保持同步
# 1. Fork 后克隆自己的仓库
$ git clone git@github.com:your-user/repo.git
$ cd repo
# 2. 添加上游仓库
$ git remote add upstream git@github.com:original/repo.git
# 3. 验证远程配置
$ git remote -v
origin git@github.com:your-user/repo.git (fetch)
origin git@github.com:your-user/repo.git (push)
upstream git@github.com:original/repo.git (fetch)
upstream git@github.com:original/repo.git (push)
# 4. 同步上游更新
$ git fetch upstream
$ git switch main
$ git merge upstream/main
$ git push origin main
# 5. 创建功能分支
$ git switch -c feature/my-feature
# 6. 开发完成后推送到自己的 Fork
$ git push origin feature/my-feature
# 7. 在 GitHub 上创建 Pull Request
5.5.2 多平台推送
# 同时推送到 GitHub 和 GitLab
$ git remote add github git@github.com:user/repo.git
$ git remote add gitlab git@gitlab.com:user/repo.git
# 设置一个 URL 推送到多个地址
$ git remote set-url --add --push origin git@github.com:user/repo.git
$ git remote set-url --add --push origin git@gitlab.com:user/repo.git
# 现在 git push origin 会同时推送到两个平台
5.6 引用规格(Refspec)
Refspec 定义了本地引用和远程引用之间的映射关系。
# 格式:+<src>:<dst>
# + 表示强制更新
# 查看 fetch refspec
$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
# 查看 push refspec
$ git config remote.origin.push
refs/heads/*:refs/heads/*
# 推送本地分支到远程不同名分支
$ git push origin main:production
# 拉取远程分支到本地不同名分支
$ git fetch origin main:local-main
# 删除远程分支(推空引用)
$ git push origin :feature-branch
5.7 孤立分支(Orphan Branch)
# 创建孤儿分支(无历史记录)
$ git switch --orphan gh-pages
# 添加内容
$ echo "<h1>GitHub Pages</h1>" > index.html
$ git add index.html
$ git commit -m "Initial gh-pages"
# 推送到远程
$ git push origin gh-pages
常见用途:
- GitHub Pages 部署
- 项目文档站点
- 独立的配置分支
5.8 远程仓库配置详解
# .git/config 中的远程配置
[remote "origin"]
url = git@github.com:user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = git@github.com:user/repo.git
push = refs/heads/main:refs/heads/main
push = refs/heads/develop:refs/heads/develop
[branch "main"]
remote = origin
merge = refs/heads/main
[branch "develop"]
remote = origin
merge = refs/heads/develop
业务场景
| 场景 | 推荐方案 |
|---|---|
| 个人项目 | 单个 origin,HTTPS 或 SSH |
| 贡献开源 | Fork + upstream 工作流 |
| 多平台备份 | 多 remote 推送 |
| 团队协作 | origin + 保护分支规则 |
| 部署 | 孤立分支(如 gh-pages) |
| CI/CD | Deploy Key + shallow clone |