Windows系统下Git不同密钥不同账户不同邮箱的管理

首先来记录一下系统多公钥的管理,比如你在gitee上喝github上不同的邮箱不同的用户名,不同项目的管理时候,设置不同的公钥。

第一步:生成两套密钥对

IBM@IBM-PC MINGW64 ~
$ ssh-keygen -t rsa -C "88888888@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/IBM/.ssh/id_rsa): /c/Users/IBM/.ssh/id_rsa_gitee
Created directory '/c/Users/IBM/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/IBM/.ssh/id_rsa_gitee.
Your public key has been saved in /c/Users/IBM/.ssh/id_rsa_gitee.pub.
The key fingerprint is:
SHA256:xuDwSq*******51o7/lv+ta********57g 88888888@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .o .            |
|o+.+ .           |
|+o= + +          |
~
~
| . . o = = .     |
| E.  .*oo        |
+----[SHA256]-----+

这样就生成了一对密钥id_rsa_giteeid_rsa_gitee.pub这里面和单密钥不一样的是在第二步提示Enter file in which to save the key (/c/Users/IBM/.ssh/id_rsa):的时候,你需要为密钥取名字,为了区分不同的密钥对。
同样的方法生成另外一对密钥id_rsa_githubid_rsa_github.pub
这时候你的~/.ssh目录下应该有两对密钥对了。

id_rsa_gitee
id_rsa_gitee.pub
id_rsa_github
id_rsa_github.pub

第二步: 设置config

~/.ssh目录下创建一个config文件(windows下创建txt文件然后重命名为config不要后缀),内容:

# gitee
Host gitee.com
HostName gitee.com                      #这个是真实的域名地址
PreferredAuthentications publickey      #配置登录时用什么权限认证--可设置publickey,password publickey,keyboard-interactive等
IdentityFile ~/.ssh/id_rsa_gitee        #这里是id_rsa的地址
User myname                         #配置使用用户名

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User myname

说明一下配置文件:

  • 每个账号单独配置一个Host,每个Host要取一个别名,每个Host主要配置HostNameIdentityFile两个属性即可。
  • Host的别名可以取为自己喜欢的名字,不过这个会影响git相关命令;
    例如:
  • Host mygithub这样定义的话,命令如下,即git@后面紧跟的名字改为mygithub
git clone git@mygithub:username/myproject.git

就相当于你配置的HostName真正的域名,映射成了Host后面的配置的名字。在你与远程仓库建立连接的时候这个别名将直接取代HostName

第三步:使用ssh-add将密钥添加到ssh-agent保管

执行添加两对密钥:

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitee

在执行出现错误:

Could not open a connection to your authentication agent.

这个错误基本上都会出现。

解决办法

$ ssh-agent bash --login -i

完整的流程:

IBM@IBM-PC MINGW64 /d/xampp/htdocs
ssh-add ~/.ssh/id_rsa_github
Could not open a connection to your authentication agent.

IBM@IBM-PC MINGW64 /d/xampp/htdocs ssh-agent bash --login -i

IBM@IBM-PC MINGW64 /d/xampp/htdocs
ssh-add ~/.ssh/id_rsa_gitee
Identity added: /c/Users/IBM/.ssh/id_rsa_gitee (/c/Users/IBM/.ssh/id_rsa_gitee)

IBM@IBM-PC MINGW64 /d/xampp/htdocs ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/IBM/.ssh/id_rsa_github (/c/Users/IBM/.ssh/id_rsa_github)

第四步:将公钥将公钥添加到githubgitee上。

测试连接状态。

IBM@IBM-PC MINGW64 /d/xampp/htdocs
ssh -T git@gitee.com
Welcome to Gitee.com, myname!

IBM@IBM-PC MINGW64 /d/xampp/htdocs ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJ********viKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi myname! You've successfully authenticated, but GitHub does not provide shell access.

这样不同的仓库就连接成功了。

后续Git操作
建项目远程仓库;
将本地写好的项目初始化并关联到远程新建仓库;

// 初始化
git init
// 仓库关联
git remote add origin git@gitee.com:myname/tp5.git
// 拉取代码
git pull origin master

遇到错误

fatal: refusing to merge unrelated histories

使用解决

git pull origin master --allow-unrelated-histories
Windows系统下Git不同密钥不同账户不同邮箱的管理

原文链接:https://beltxman.com/1869.html,若无特殊说明本站内容为 行星带 原创,未经同意禁止转载。

Scroll to top