解决拉取多个不同git项目下的ssh问题

发布于:2024-05-05 ⋅ 阅读:(34) ⋅ 点赞:(0)

目录

一、情况背景

二、私钥不匹配报错

三、密钥生成

        3.1、单git仓库生成

        3.2、多git仓库生成


一、情况背景

        简单来讲,就是不同的git仓库需要有对应不同的ssh私钥。公钥需要在git仓库的ssh页面配置后,才能正常的提交拉取仓库代码。否则会因为公钥不匹配而报错。一般来说,git仓库的ssh key的配置在:我的—settings—ssh key。

二、私钥不匹配报错

        如若私钥和git仓库配置的公钥不匹配,那么就无法进行拉取提交代码。使用idea等工具操作远程git仓库,那么git console会输出如下图的报错。

        一般出现这种情况的大概率是同时使用了两种及以上的git仓库,如github和gitlab。解决方法就是生成两份公钥和私钥即可,然后再去对应的git ssh页面进行配置。

git clone xxxx
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:0ga5tdL1I49aoKBp0qwakGwnO9/YAyiW9XPw4oCHYqk.
Please contact your system administrator.
Add correct host key in /c/Users/dell/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /c/Users/dell/.ssh/known_hosts:3
ECDSA host key for xxx.xx.x.xx has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

三、密钥生成

        3.1、单git仓库生成

1、cmd打开输入:ssh-keygen -t rsa -C "xxxx@qq.com" 后,一直回车直至看到如下内容。
The key's randomart image is:
+---[RSA 3072]----+
|          ...    |
|     .   . o     |
|    E o.o o      |
|     o.o+.o.     |
|    . ooSBo*     |
|   o .. o=O .    |
|    o. .o*.+     |
|     ..o=+* o .  |
|   .o.o=**=oo+   |
+----[SHA256]-----+
2、进入 C:\Users\dell\.ssh 查看对应的公钥私钥,其中id_rsa为私钥,id_rsa.pub为公钥。
3、复制公钥(id_rsa.pub)到git仓库的ssh页面进行配置。

        3.2、多git仓库生成

        如若需要使用多git仓库来管理代码,那么需要生成多个公钥私钥来对应。本文就拿两个(github、gitlab)仓库举例。

ssh-keygen -t rsa -C "xxx@qq.com"
ssh-keygen -t rsa -f c:\users\dell\.ssh\id_rsa.github -C "xxx@163.com"


c:\users\dell\.ssh 
    id_rsa
    id_rsa.pub
    id_rsa.github
    id_rsa.github.pub
拷贝对应的公钥(.pub文件)到git仓库的ssh配置页面中进行配置。


网站公告

今日签到

点亮在社区的每一天
去签到