windows11 git命令访问github

本文详细介绍了在Windows环境下,由于DNS解析问题和SSH密钥配置导致无法正常连接GitHub的解决步骤,包括修改DNS设置、生成SSH密钥、添加到SSH agent以及配置.gitconfig的过程。
摘要由CSDN通过智能技术生成

注意事项:
1)以下所有命令都是在git bash中执行;
2)由于默认.ssh目录路径中包含中文,执行ssh-keygen时不使用默认路径,指定另外的密钥文件路径,比如:/c/.ssh/id_rsa ; 
3)需要提前在c盘生成.ssh目录,并生成一个空白的/c/.ssh/id_rsa文件;
4)"xxxxxx@xxx.com"需要修改为你个人email;

首先执行ssh看看域名解析有没有问题,如果有问题,如下:
$ ssh -T git@github.com
ssh: Could not resolve hostname github.com: No address associated with hostname

这时手动设置dns ip地址为8.8.8.8及8.8.4.4
参考下面的文章:
https://zhongguo.eskere.club/%E5%A6%82%E4%BD%95%E5%9C%A8-windows-11-%E4%B8%AD%E6%9B%B4%E6%94%B9-dns-%E8%AE%BE%E7%BD%AE/2021-10-13/

设置完后,再次执行ssh,提示permission denied,如下:
$ 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.
git@github.com: Permission denied (publickey).


然后执行ssh-keygen并指定key文件为/c/.ssh/id_rsa,如下:
$ ssh-keygen -t rsa -b 4096 -C "xxxxxx@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxxxx/.ssh/id_rsa): /c/.ssh/id_rsa
/c/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/.ssh/id_rsa
Your public key has been saved in /c/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:RKP9snwKcQ+nqBnS+I6tMt8LOXcUXXrnZd51bN6HeKk xxxxxx@xxx.com
The key's randomart image is:
+---[RSA 4096]----+
|        o .      |
|       = +     . |
|      o = . . o =|
|       o o o = *+|
|      o S o o = =|
|   + . = B   o  .|
|  * + + + o E    |
|o  X = . o       |
| ++oO.  .        |
+----[SHA256]-----+

然后,拷贝id_rsa.pub文件内容并粘贴到github profile上去:
Github profile -> Settings -> SSH and GPG Keys -> Add new SSH key

接着执行ssh-add,出现如下提示:
$ ssh-add /c/.ssh/id_rsa
Could not open a connection to your authentication agent.

然后执行:
$ eval $(ssh-agent) 或者 eval $(ssh-agent -s)
Agent pid 1118

OK后,接着又执行ssh-add,这次OK了:
$ ssh-add /c/.ssh/id_rsa
Identity added: /c/.ssh/id_rsa (xxxxxx@xxx.com)

继续执行:
$ ssh -T git@github.com
Hi xxxxxx! You've successfully authenticated, but GitHub does not provide shell access.

对了,如果没有配置.gitconfig的话,要配置:
git config --global user.name "xxxxxx"
git config --global user.email "xxxxxx@xxx.com"

然后就可以git clone啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值