我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

此版本的 GitHub Enterprise 已停止服务 2021-03-02. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

重命名远程

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

本文内容

git remote rename 命令使用两个参数:

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

示例

以下示例假设您使用 HTTPS 克隆,即推荐使用的方法。

$ git remote -v
# 查看现有远程
> origin  https://hostname/OWNER/REPOSITORY.git (fetch)
> origin  https://hostname/OWNER/REPOSITORY.git (push)

$ git remote rename origin destination
# 将远程名称从 'origin' 更改为 'destination'

$ git remote -v
# 验证远程的新名称
> destination  https://hostname/OWNER/REPOSITORY.git (fetch)
> destination  https://hostname/OWNER/REPOSITORY.git (push)

疑难解答

当您尝试重命名远程时可能会遇到以下错误。

无法将配置部分 'remote.[old name]' 重命名为 'remote.[new name]'

此错误表示您输入旧远程名称尝试的远程不存在。

您可以使用 git remote -v 命令检查当前存在哪些远程:

$ git remote -v
# 查看现有远程
> origin  https://hostname/OWNER/REPOSITORY.git (fetch)
> origin  https://hostname/OWNER/REPOSITORY.git (push)

远程 [new name] 已存在。

此错误表示您要使用的远程名称已经存在。 要解决此问题,使用不同的远程名称,或重命名原始远程。

延伸阅读