添加远程仓库
要添加新的远程仓库,请在终端中使用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)
故障排除:不存在这样的远程仓库“[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.[旧名称]”重命名为“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'
检查您是否正确输入了远程仓库名称。