Skip to content

远程仓库管理

远程仓库是托管在网络上的 Git 仓库,用于团队协作和代码备份。Git 支持同时管理多个远程仓库。

git remote add 添加远程仓库

bash
# 语法:git remote add <name> <url>

# 添加 origin(默认远程仓库名)
git remote add origin https://github.com/user/repo.git
git remote add origin git@github.com:user/repo.git  # SSH 方式

# 添加第二个远程仓库(upstream 通常指原始仓库)
git remote add upstream https://github.com/original/repo.git

# 添加自建服务器
git remote add production git@myserver.com:repo.git

git remote -v 查看远程仓库

bash
# 查看所有远程仓库的 URL
git remote -v
# origin    https://github.com/user/repo.git (fetch)
# origin    https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

# 查看远程仓库名称列表
git remote
# origin
# upstream

git remote rename / remove

bash
# 重命名远程仓库
git remote rename origin github
git remote rename upstream original

# 删除远程仓库引用(不影响远程服务器)
git remote remove upstream
git remote rm upstream    # 简写

多远程仓库管理(origin / upstream)

典型场景:Fork 开源项目

Fork 开源项目后,通常需要同时维护两个远程仓库:

bash
# origin:你 Fork 出来的仓库(你有写权限)
git remote add origin git@github.com:your-username/repo.git

# upstream:原始项目仓库(只有读权限)
git remote add upstream git@github.com:original-owner/repo.git

# 查看配置
git remote -v
# origin    git@github.com:your-username/repo.git (fetch)
# origin    git@github.com:your-username/repo.git (push)
# upstream  git@github.com:original-owner/repo.git (fetch)
# upstream  git@github.com:original-owner/repo.git (push)

同步上游仓库

bash
# 获取上游最新代码
git fetch upstream

# 将上游 main 合并到本地 main
git switch main
git merge upstream/main

# 或者使用 rebase 保持线性历史
git rebase upstream/main

# 推送更新到自己的 Fork
git push origin main

推送到多个远程仓库

bash
# 推送到所有远程仓库(需要分别推送)
git push origin main
git push backup main

# 配置一个别名同时推送到多个地方
git config alias.pushall '!git push origin && git push backup'

为一个 remote 配置多个 push URL

bash
# 为 origin 添加额外的 push URL
git remote set-url --add --push origin git@gitlab.com:user/repo.git
git remote set-url --add --push origin git@github.com:user/repo.git

# 一次 push 同时推送到 GitHub 和 GitLab
git push origin main

git remote show 查看详情

bash
git remote show origin

# 输出:
# * remote origin
#   Fetch URL: https://github.com/user/repo.git
#   Push  URL: https://github.com/user/repo.git
#   HEAD branch: main
#   Remote branches:
#     develop tracked
#     main    tracked
#     feature/login tracked
#   Local branches configured for 'git pull':
#     develop merges with remote develop
#     main    merges with remote main
#   Local refs configured for 'git push':
#     develop pushes to develop (up to date)
#     main    pushes to main    (local out of date)

修改远程仓库 URL

bash
# 修改 fetch/push URL(用于仓库迁移)
git remote set-url origin https://new-url/repo.git

# 从 HTTPS 切换到 SSH
git remote set-url origin git@github.com:user/repo.git

# 从 SSH 切换到 HTTPS
git remote set-url origin https://github.com/user/repo.git

# 单独修改 push URL(fetch 和 push 用不同 URL)
git remote set-url --push origin git@github.com:user/repo.git

远程仓库的本地引用

每个远程仓库的分支在本地都有对应的远程追踪分支

bash
# 查看本地存储的远程分支状态
git branch -r
# origin/main
# origin/develop
# upstream/main

# 这些引用存储在 .git/refs/remotes/ 目录下
ls .git/refs/remotes/origin/
# HEAD  develop  main

实用技巧

克隆时指定远程名

bash
# 默认是 origin,可以自定义
git clone -o github https://github.com/user/repo.git

验证远程连接

bash
# 测试是否能连接到远程仓库
git ls-remote origin

# 只获取 HEAD 引用(轻量测试)
git ls-remote --heads origin

管理远程仓库的钩子(服务器端)

某些 Git 服务(如 Gitea、GitLab)支持查看服务端钩子,但通常通过 Web 界面管理。

总结

操作命令
添加远程git remote add <name> <url>
查看远程git remote -v
查看详情git remote show <name>
重命名git remote rename <old> <new>
删除git remote remove <name>
修改 URLgit remote set-url <name> <url>

管理好远程仓库是团队协作的基础,理解 origin 和 upstream 的关系对于参与开源项目尤为重要。