今天在尝试提交第二个Github账号下的项目内容时提交失败,下面记录一下解决过程。
> git push origin main
kex_exchange_identification: Connection closed by remote host
Connection closed by 192.30.255.112 port 22
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.错误中提到无法从远程仓库中读取内容,需要确认是有正确权限以及远程仓库项目是否建立。
1.首先确保远程仓库已经建立。
2.然后通过添加SSH Key给第二个Github账户建立安全连接。多个Git平台是可以共用同一对密钥的,比如Github,Bitbucket,Gitee平台三个平台之间用同一对是没问题的。但是同一平台内不同账户必须用不同的密钥对,比如Github平台的账户A和账户B,必须是不同的密钥。
之前在为BitBucket已经创建过id_rsa密钥对,用在此前的Bitbucket账号A和Github账号B上都没有问题,现在遇到问题的项目是Github账号C上的,那么需要为他创建新的密钥对。
注意这里需要命名新的文件名。使用默认文件名可能会覆盖原有文件,那么原有文件的密钥便失效了,用到了这个密钥的平台则都需要更改。
> ssh-keygen -t rsa -C "comment"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lily/.ssh/id_rsa): /Users/lily/.ssh/id_rsa_accountC
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/lily/.ssh/id_rsa_accountC
Your public key has been saved in /Users/lily/.ssh/id_rsa_accountC.pub
The key fingerprint is:
SHA256:RTKN6c9D2BqrmS3EVfo+FjSlCRQD4aJyZpe6IWehUGI comment
The key's randomart image is:
+---[RSA 3072]----+
| o+*=. |
| . +=o . |
|.E. . .. *.+ |
|.o . o *.B |
|o * o. .SX . |
|.* + o o * |
|o = . = . o |
| + o = . + |
| . . . . |
+----[SHA256]-----+3.接下来需要在.ssh文件夹内添加config文件并加上配置,如果已有config则打开编辑即可。默认情况下GIt会寻找默认文件id_rsa的密钥进行授权匹配,有了config文件,系统会根据其中配置读取指定密钥。
Host accountB.github
HostName github.com
user git
IdentityFile "~/.ssh/id_rsa"
IdentitiesOnly yes
Host accountC.github
HostName github.com
user git
IdentityFile "~/.ssh/id_rsa_accountC"
IdentitiesOnly yes其中Host是自己命名的域名别名,HostName是GIt平台域名,user一般使用系统用户git即可,IdentityFile则指定对应的私钥文件地址。
测试一下
> ssh -T git@github.com
kex_exchange_identification: Connection closed by remote host
Connection closed by 192.30.255.113 port 22显示Git远程连接关闭,上网查了一下类似问题,可能是电脑设置的梯子封禁了 Github 端口 22 的连接,将 Github 的连接端口从 22 改为 443 即可。继续修改config,将HostName改成ssh.githu.com,Port改为443。
Host accountB.github
HostName ssh.github.com
user git
Port 443
IdentityFile "~/.ssh/id_rsa"
IdentitiesOnly yes
Host accountC.github
HostName ssh.github.com
user git
Port 443
IdentityFile "~/.ssh/id_rsa_accountC"
IdentitiesOnly yes测试一下
> ssh -T git@accountC.github
The authenticity of host '[ssh.github.com]:443 ([192.30.255.122]:443)' can't be established.
ED25219 key fingerprint is SHA256:+DiY3wxxV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25219) to the list of known hosts.
Hi accountC! You've successfully authenticated, but GitHub does not provide shell access.建立ssh连接成功。相应的也可以再执行一下ssh -T git@accountB.github。
4.重新回到项目中,修改远程仓库的连接。
git remote set-url origin git@accountC.github/accountC/someproject.git再执行一下提交,提交成功!
参考链接: