跳转到内容

远程仓库的使用

到目前为止,我们所有的操作都是在本地计算机上进行的。Git 的真正强大之处在于其分布式特性——你可以与他人协作,将代码备份到服务器,或者在多台设备间同步进度。要实现这些,你需要了解远程仓库 (Remote Repositories)

远程仓库是托管在网络或其他位置的你的项目版本。它可以是你同事的电脑,也可以是像 GitHub、GitLab 或 Gitee 这样的托管服务。

在 Git 中,“远程” (Remote) 是一个命名配置,它存储了远程仓库的 URL 以及推拉规则。例如 origin 就是一个远程的名称。

graph LR
Local["本地仓库 (Local Repo)"]
Remote["远程仓库 (Remote Repo)"]
Local -- "push (推送)" --> Remote
Remote -- "pull (拉取)" --> Local
classDef local fill:#4a5568,stroke:#2d3748,color:#fff,stroke-width:2px
classDef remote fill:#3182ce,stroke:#2c5282,color:#fff,stroke-width:2px
class Local local
class Remote remote

如果你是克隆 (clone) 了一个现有的仓库,Git 会自动为你添加一个远程仓库。你可以使用 git remote 命令查看当前配置的远程仓库。

Terminal window
# 列出远程仓库的简写名称
git remote
# 列出远程仓库的详细信息 (名称 + URL)
git remote -v

输出示例:

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

如果你是在本地初始化的仓库 (git init),你想把它连接到 GitHub 上新建的空仓库,你需要手动添加远程连接。

语法: git remote add <简写> <url>

Terminal window
git remote add origin https://github.com/your-username/your-project.git

执行后,再次运行 git remote -v,你应该能看到新添加的 origin

随着项目的发展,你可能需要管理你的远程连接。

例如,将 origin 重命名为 destination(虽然不推荐改默认名,但了解一下无妨):

Terminal window
git remote rename origin destination

如果某个远程仓库不再使用(例如对应的服务器关闭了),你可以解除绑定:

Terminal window
git remote remove destination
  • 远程仓库是代码的异地备份或协作中心。
  • 使用 git remote -v 查看当前连接。
  • 使用 git remote add <name> <url> 添加新的连接。
  • origin 是默认的远程仓库名称。