【git基础】Windows下Git多账号配置,同一电脑多个ssh-key的管理

 

step1: gitlab,生成gitlab网站的公钥私钥;

$ ssh-keygen -t rsa -C xxx.yy@zzz.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa): id_rsa_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_gitlab
Your public key has been saved in id_rsa_gitlab.pub

注意,【Enter passphrase (empty for no passphrase)】设置密码;

step2: github,生成github网站的公钥私钥;

$ ssh-keygen -t rsa -C xxx@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zzz/.ssh/id_rsa): id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github
Your public key has been saved in id_rsa_github.pub

step3: 将得到的id_rsa/id_rsa.pub文件保存在git默认访问的.ssh的目录;

$ ls
config  id_rsa_github  id_rsa_github.pub  id_rsa_gitlab  id_rsa_gitlab.pub  known_hosts  known_hosts.old

step4: 将github/gitlab的公钥分别上传到对应的服务器;

github:setting / SSH and GPG keys;

gitlab:setting / SSH keys

step5: 在.ssh目录创建config文件(直接vi config然后添加配置内容即可),完成对应的git配置;

每个账号单独配置一个Host,每个Host要取一个别名,每个Host主要配置HostNameIdentityFile两个属性即可

Host的名字可以取为自己喜欢的名字,不过这个会影响git相关命令,例如:
Host mygithub 这样定义的话,命令如下,即git@后面紧跟的名字改为mygithub;

HostName              这个是真实的域名地址
IdentityFile          这里是id_rsa的地址
PreferredAuthentications   配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等
User                配置使用用户名

example:git clone git@mygithub:happyamyhope/IPM.git

# GitLab.xxx.yy
Host gitlab.xxx.yy
    HostName gitlab.xxx.yy
    IdentityFile  C:\Users\zzz\.ssh\id_rsa_gitlab 
    RSAAuthentication yes
    PreferredAuthentications publickey
    User aaa.bbb@ccc.com
# Github.com
Host github.com
    HostName github.com
    IdentityFile /c/Users/zzz/.ssh/id_rsa_github
    RSAAuthentication yes
    PreferredAuthentications publickey
    User ddd@163.com

注意不同网站的名称是否正确;

step6:测试;

$ ssh -T git@github.com
/c/Users/zzz/.ssh/config line 6: Unsupported option "rsaauthentication"
/c/Users/zzz/.ssh/config line 13: Unsupported option "rsaauthentication"
Enter passphrase for key '/c/Users/zzz/.ssh/id_rsa_github':
Hi ddd! You've successfully authenticated, but GitHub does not provide shell access.

其他网站类似,比如

ssh -T git@gitlab.abc.ai

注意,ubuntu系统的配置过程几乎一样的!

step7:  不同平台帐号的管理和使用;

因为存在双/多账户,需要通过Git配置不同账户的用户和邮箱,这里采取GitLab用全局用户名和邮箱配置,而GitHub针对每个clone到本地的项目进行单独的用户名和邮箱配置,避免冲突。

1)配置全局的用户名和邮箱;

// 将GitLab设置为全局
git config --global user.name "gitlab"
git config --global user.email "gitlab@126.com"

2)为GitHub上的每个repository/项目单独设置用户名和邮箱;

// 注意:下述命令要到每个本地GitHub项目目录下执行
git config user.name "github"
git config user.email "github@126.com"

3)最后可以进行代码的修改,拉取、提交、push等,验证配置是否成功。

全局配置好像不管用啊,需要在每个git项目下单独设置用户名和邮箱;

 

参考

1. Windows下Git多账号配置,同一电脑多个ssh-key的管理

2. 一个Git终端如何配置多个Git仓库和账户;

 

posted on 2021-08-02 19:51  鹅要长大  阅读(330)  评论(0编辑  收藏  举报

导航