更改远程仓库的 URL
git remote set-url
命令可更改现有远程仓库的 URL。
提示: 有关 HTTPS 与 SSH URL 之间的差异,请参阅“我应使用哪种远程 URL?”
git remote set-url
命令使用两个参数:
- 现有远程仓库的名称。 例如,
源仓库
或上游仓库
是两种常见选择。 -
远程仓库的新 URL。 例如:
-
如果您要更新为使用 HTTPS,您的 URL 可能如下所示:
https://[hostname]/USERNAME/REPOSITORY.git
-
如果您要更新为使用 SSH,您的 URL 可能如下所示:
git@主机名:USERNAME/REPOSITORY.git
-
将远程 URL 从 SSH 切换到 HTTPS
-
Open TerminalTerminalGit Bashthe terminal.
-
将当前工作目录更改为您的本地仓库。
-
列出现有远程仓库以获取要更改的远程仓库的名称。
$ git remote -v > origin git@主机名:USERNAME/REPOSITORY.git (fetch) > origin git@主机名:USERNAME/REPOSITORY.git (push)
-
Change your remote's URL from SSH to HTTPS with the
git remote set-url
command.$ git remote set-url origin https://主机名/USERNAME/REPOSITORY.git
-
验证远程 URL 是否已更改。
$ git remote -v # Verify new remote URL > origin https://主机名/USERNAME/REPOSITORY.git (fetch) > origin https://主机名/USERNAME/REPOSITORY.git (push)
The next time you git fetch
, git pull
, or git push
to the remote repository, you'll be asked for your GitHub username and password.
- If you have two-factor authentication enabled, you must create a personal access token to use instead of your GitHub password.
- 您可以使用凭据小助手让 Git 在每次与 GitHub 会话时记住您的 GitHub 用户名和密码。
Switching remote URLs from HTTPS to SSH
-
Open TerminalTerminalGit Bashthe terminal.
-
将当前工作目录更改为您的本地仓库。
-
列出现有远程仓库以获取要更改的远程仓库的名称。
$ git remote -v > origin https://主机名/USERNAME/REPOSITORY.git (fetch) > origin https://主机名/USERNAME/REPOSITORY.git (push)
-
Change your remote's URL from HTTPS to SSH with the
git remote set-url
command.$ git remote set-url origin git@主机名:USERNAME/REPOSITORY.git
-
验证远程 URL 是否已更改。
$ git remote -v # Verify new remote URL > origin git@主机名:USERNAME/REPOSITORY.git (fetch) > origin git@主机名:USERNAME/REPOSITORY.git (push)
疑难解答
You may encounter these errors when trying to change a remote.
No such remote '[name]'
This error means that the remote you tried to change doesn't exist:
$ git remote set-url sofake https://主机名/octocat/Spoon-Knife
> fatal: No such remote 'sofake'
检查您是否正确键入了远程仓库的名称。