Changing a remote’s URL 修改远程 URL

git remote set-url 命令是修改存在的远程库的 URL

提示:了解 HTTPS 和 SSH URL 的不同之处,请查看 “我应该用哪种远程 URL

这个命令带两个参数

  • 已经存在远程名字,如 origin
  • 新的远程的 URL ,如:
    • https://github.com/USERNAME/REPOSITORY_2.git 如果你用https 更新代码
    • git@github.com:USER/REPOSITORY_2.git 如果你用 SSH 更新代码

Switching remote URLs from SSH to HTTPS 从 SSH 切换 远程 URL 到 HTTPS

1.打开终端( Mac 和 Linux 用户)或者 命令行 (Windows 用户).

2.更改当前工作路径到你的本地项目

3.列出你已经存在的远程库,为了获取到你想要修改的远程的名字

  1. $ git remote -v
  2. # origin git@github.com:USERNAME/REPOSITORY.git (fetch)
  3. # origin git@github.com:USERNAME/REPOSITORY.git (push)

4.使用 remote set-url 修改从 SSH 切换 远程 URL 到 HTTPS

  1. $ git remote set-url origin https://github.com/USERNAME/REPOSITORY_2.git

5.验证远程URL已改变

  1. $ git remote -v
  2. # Verify new remote URL
  3. # origin https://github.com/USERNAME/REPOSITORY2.git (fetch)
  4. # origin https://github.com/USERNAME/REPOSITORY2.git (push)

下次,如果你使用 git fetch, git pull, 或 git push 操作库,则被要求提供账号密码

Switching remote URLs from HTTPS to SSH 从 HTTPS 切换 远程 URL 到 SSH

1.打开终端( Mac 和 Linux 用户)或者 命令行 (Windows 用户).

2.更改当前工作路径到你的本地项目

3.列出你已经存在的远程库,为了获取到你想要修改的远程的名字

  1. $ git remote -v
  2. # origin https://github.com/USERNAME/REPOSITORY.git (fetch)
  3. # origin https://github.com/USERNAME/REPOSITORY.git (push)

4.使用 remote set-url 修改从 SSH 切换 远程 URL 到 HTTPS

  1. $ git remote set-url origin git@github.com:USERNAME/REPOSITORY2.git

5.验证远程URL已改变

  1. $ git remote -v
  2. # Verify new remote URL
  3. # origin git@github.com:USERNAME/REPOSITORY2.git (fetch)
  4. # origin git@github.com:USERNAME/REPOSITORY2.git (push)

Troubleshooting 故障排除

你可能会遇到这些错误时,当试图改变一个远程。

No such remote ‘[name]’

这意思是说你要修改的远程不存在

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

检查你是否正确地输入远程名称。

Further reading 扩展阅读

参考:https://help.github.com/articles/changing-a-remote-s-url/