当我在本地的git hub 进行修改后,需要推送到远程github仓库。
当我运行了git add . git commit -m "ingress-controller image" 以后,运行git push origin main,发现由于网络原因无法连接到远程github仓库。
此时开始设置ssh连接。首先在本地生成密钥,使用ssh-keygen命令:
-t
是指定密钥类型的参数。在这里,-t rsa
表示生成 RSA 类型的密钥。- b表示输出的密钥长度为4096位,-C
是为密钥添加注释的参数。"example@163.com"
是我为这个密钥添加的注释。
PS E:\git_image_repository\agent_image> ssh-keygen -t rsa -b 4096 -C "example@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\hades/.ssh/id_rsa):
Created directory 'C:\\Users\\hades/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\hades/.ssh/id_rsa
Your public key has been saved in C:\Users\hades/.ssh/id_rsa.pub
The key fingerprint is:
fsfhlsfhslfsbxvbdvvasvavsalufw
The key's randomart image is:
xxxxxx
执行ssh-keygen命令后,系统会询问你将密钥存储在哪里,否则会存储在默认的位置,这里我点击了回车,直接存储在默认位置。随后问了一个passphrase,两次回车跳过。则rsa密钥被存储好了。
通过cat 命令来查看自己的rsa 密钥。
PS E:\git_image_repository\agent_image> cat ~/.ssh/id_rsa.pub
ssh-rsa XXXXXXX
登录github网页版,并点击自己的头像,找到settings选项。
点击ssh and GPG keys
点击新建ssh key,并将自己电脑上的rsa 密钥粘贴进去。第一步点击new SSH key进行创建,2通过创建成功后的ssh key。
此时就可以在git powershell进行操作了。可以执行ssh -T git@github.com进行验证连接:
PS E:\git_image_repository\agent_image> ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/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 'github.com' (ED25519) to the list of known hosts.
Hi 5703840! You've successfully authenticated, but GitHub does not provide shell access.
出现这个提示表示连接成功,其中这条信息表示 GitHub 的主机密钥已经成功添加到你的 ~/.ssh/known_hosts 文件中。之后再连接到 github.com 时,SSH 会自动验证主机密钥,无需再次手动确认。
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
切换url和ssh连接方式
以下语句分别是设置用ssh的方式连接(git@github.....)和url的连接方式(https://github.com...)
git remote set-url origin git@github.com:fejlfj/agent_image.git
git remote set-url origin https://github.com/fejlfj/agent_image.git