添加远程存储库
要添加新的远程存储库,请在终端中使用 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 可能如下所示:
[email protected]:OWNER/REPOSITORY.git
将远程 URL 从 SSH 切换到 HTTPS
-
打开 终端终端Git Bash。
-
将当前工作目录更改为您的本地项目。
-
列出您现有的远程存储库,以便获取要更改的远程存储库的名称。
$ git remote -v > origin [email protected]:OWNER/REPOSITORY.git (fetch) > origin [email protected]: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 [email protected]:OWNER/REPOSITORY.git
-
验证远程 URL 是否已更改。
$ git remote -v # Verify new remote URL > origin [email protected]:OWNER/REPOSITORY.git (fetch) > origin [email protected]: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'
检查您是否正确输入了远程名称。