跳到主要内容

管理远程仓库

学习如何在你的电脑上操作本地仓库以及托管在 GitHub 上的远程仓库。

平台导航

添加远程仓库

要添加新的远程仓库,请在终端中,在你的仓库所在目录下使用git remote add命令。

git remote add命令接受两个参数

  • 远程名称,例如,origin
  • 远程 URL,例如,https://github.com/OWNER/REPOSITORY.git

例如

$ git remote add origin https://github.com/OWNER/REPOSITORY.git
# Set a new remote

$ git remote -v
# Verify new remote
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

有关使用哪个 URL 的更多信息,请参阅“关于远程仓库”。

故障排除:远程 origin 已存在

此错误意味着你尝试添加一个与本地仓库中已存在的远程仓库同名的远程仓库。

$ git remote add origin https://github.com/octocat/Spoon-Knife.git
> fatal: remote origin already exists.

要解决此问题,你可以:

  • 为新的远程仓库使用不同的名称。
  • 在添加新的远程仓库之前重命名现有的远程仓库。更多信息,请参阅下面的“重命名远程仓库”。
  • 在添加新的远程仓库之前删除现有的远程仓库。更多信息,请参阅下面的“移除远程仓库”。

更改远程仓库的 URL

git remote set-url命令更改现有的远程仓库 URL。

提示

有关 HTTPS 和 SSH URL 之间区别的信息,请参阅“关于远程仓库”。

git remote set-url命令接受两个参数

  • 现有的远程名称。例如,originupstream是两个常见的选项。

  • 远程仓库的新 URL。例如

    • 如果要更新为使用 HTTPS,你的 URL 可能如下所示:
    https://github.com/OWNER/REPOSITORY.git
    
    • 如果要更新为使用 SSH,你的 URL 可能如下所示:
    git@github.com:OWNER/REPOSITORY.git
    

将远程 URL 从 SSH 切换到 HTTPS

  1. 打开终端终端Git Bash

  2. 将当前工作目录更改为你的本地项目。

  3. 列出你现有的远程仓库以获取要更改的远程仓库的名称。

    $ git remote -v
    > origin  git@github.com:OWNER/REPOSITORY.git (fetch)
    > origin  git@github.com:OWNER/REPOSITORY.git (push)
    
  4. 使用git remote set-url命令将远程仓库的 URL 从 SSH 更改为 HTTPS。

    git remote set-url origin https://github.com/OWNER/REPOSITORY.git
    
  5. 验证远程 URL 是否已更改。

    $ git remote -v
    # Verify new remote URL
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)
    

下次你向远程仓库git fetchgit pullgit push时,系统会提示你输入 GitHub 用户名和密码。当 Git 提示你输入密码时,请输入你的个人访问令牌。或者,你可以使用像Git Credential Manager这样的凭据助手。基于密码的 Git 身份验证已被移除,取而代之的是更安全的身份验证方法。更多信息,请参阅“管理你的个人访问令牌”。

你可以使用凭据助手,这样 Git 每次与 GitHub 通信时都会记住你的 GitHub 用户名和个人访问令牌。

将远程 URL 从 HTTPS 切换到 SSH

  1. 打开终端终端Git Bash

  2. 将当前工作目录更改为你的本地项目。

  3. 列出你现有的远程仓库以获取要更改的远程仓库的名称。

    $ git remote -v
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)
    
  4. 使用git remote set-url命令将远程仓库的 URL 从 HTTPS 更改为 SSH。

    git remote set-url origin git@github.com:OWNER/REPOSITORY.git
    
  5. 验证远程 URL 是否已更改。

    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:OWNER/REPOSITORY.git (fetch)
    > origin  git@github.com:OWNER/REPOSITORY.git (push)
    

故障排除:没有这样的远程仓库“[名称]”

此错误意味着你尝试更改的远程仓库不存在。

$ git remote set-url sofake https://github.com/octocat/Spoon-Knife
> fatal: No such remote 'sofake'

检查你是否正确键入了远程仓库的名称。

重命名远程仓库

使用git remote rename命令重命名现有的远程仓库。

git remote rename命令接受两个参数

  • 现有的远程名称,例如,origin
  • 远程仓库的新名称,例如,destination

重命名远程仓库的示例

这些示例假设你正在使用 HTTPS 克隆,这是推荐的做法。

$ git remote -v
# View existing remotes
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

$ git remote rename origin destination
# Change remote name from 'origin' to 'destination'

$ git remote -v
# Verify remote's new name
> destination  https://github.com/OWNER/REPOSITORY.git (fetch)
> destination  https://github.com/OWNER/REPOSITORY.git (push)

故障排除:无法将配置部分“remote.[旧名称]”重命名为“remote.[新名称]”

此错误意味着你键入的旧远程名称不存在。

你可以使用git remote -v命令检查当前存在的远程仓库。

$ git remote -v
# View existing remotes
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

故障排除:远程仓库[新名称]已存在

此错误意味着你想要使用的远程仓库名称已存在。要解决此问题,请使用不同的远程仓库名称,或重命名原始远程仓库。

移除远程仓库

使用git remote rm命令从你的仓库中移除远程 URL。

git remote rm命令接受一个参数

  • 远程名称,例如,destination

从你的仓库中移除远程 URL 只会取消本地和远程仓库的链接。它不会删除远程仓库。

移除远程仓库的示例

这些示例假设你正在使用 HTTPS 克隆,这是推荐的做法。

$ git remote -v
# View current remotes
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)
> destination  https://github.com/FORKER/REPOSITORY.git (fetch)
> destination  https://github.com/FORKER/REPOSITORY.git (push)

$ git remote rm destination
# Remove remote
$ git remote -v
# Verify it's gone
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

注意

git remote rm不会删除服务器上的远程仓库。它只是从你的本地仓库中移除远程仓库及其引用。

故障排除:无法移除配置部分“remote.[名称]”

此错误意味着你尝试删除的远程仓库不存在。

$ git remote rm sofake
> error: Could not remove config section 'remote.sofake'

检查你是否正确键入了远程仓库的名称。

进一步阅读