添加远程仓库
要添加新的远程仓库,请在终端中位于仓库所在目录时使用 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 命令接受两个参数
-
已有的远程名称。例如,
origin或upstream是常见选择。 -
远程的新 URL。例如
- 如果要改为使用 HTTPS,您的 URL 可能是这样的
https://github.com/OWNER/REPOSITORY.git- 如果要改为使用 SSH,您的 URL 可能是这样的
git@github.com:OWNER/REPOSITORY.git
将远程 URL 从 SSH 切换为 HTTPS
-
打开 终端终端Git Bash。
-
将当前工作目录更改为本地项目所在目录。
-
列出已有的远程,以获取要更改的远程名称。
$ git remote -v > origin git@github.com:OWNER/REPOSITORY.git (fetch) > origin git@github.com:OWNER/REPOSITORY.git (push) -
使用
git remote set-url命令将远程的 URL 从 SSH 切换为 HTTPS。git remote set-url origin https://github.com/OWNER/REPOSITORY.git -
验证远程 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 fetch、git pull 或 git push 时,系统会要求输入 GitHub 的用户名和密码。Git 提示输入密码时,请输入您的个人访问令牌。或者,您可以使用诸如 Git Credential Manager 之类的凭据帮助程序。Git 已不再支持基于密码的认证,转而使用更安全的认证方式。更多信息,请参阅 管理您的个人访问令牌。
您可以 使用凭据帮助程序,让 Git 在每次与 GitHub 交互时记住您的 GitHub 用户名和个人访问令牌。
将远程 URL 从 HTTPS 切换为 SSH
-
打开 终端终端Git Bash。
-
将当前工作目录更改为本地项目所在目录。
-
列出已有的远程,以获取要更改的远程名称。
$ git remote -v > origin https://github.com/OWNER/REPOSITORY.git (fetch) > origin https://github.com/OWNER/REPOSITORY.git (push) -
使用
git remote set-url命令将远程的 URL 从 HTTPS 切换为 SSH。git remote set-url origin git@github.com:OWNER/REPOSITORY.git -
验证远程 URL 已更改。
$ git remote -v # Verify new remote URL > origin git@github.com:OWNER/REPOSITORY.git (fetch) > origin git@github.com:OWNER/REPOSITORY.git (push)
故障排除:不存在名为 '[name]' 的远程
此错误表示您尝试更改的远程不存在
$ 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.[old name]' 重命名为 'remote.[new name]'
此错误表示您输入的旧远程名称不存在。
您可以使用 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)
故障排除:远程 [new name] 已经存在
此错误表示您想使用的远程名称已存在。解决方法是使用其他名称,或重命名原有远程。
删除远程仓库
使用 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.[name]'
此错误表示您尝试删除的远程不存在
$ git remote rm sofake
> error: Could not remove config section 'remote.sofake'
检查您是否正确输入了远程名称。