同时使用gitHub和gitLab

一,生成ssh密钥并配置

分别对gitlab和github生成对应的密钥(默认情况下本地生成的秘钥位于C:/Users/用户名/.ssh/)

1,生成gitlab密钥并配置

  • 电脑开始菜单中找到已安装的gitbash并打开,输入命令:

    ssh-keygen -t rsa -C "公司邮箱地址"

    按回车,再按3次enter键,生成对应的gitlab密钥:id_rsa和id_rsa.pub

jerry@Jerry MINGW64 ~
$ ssh-keygen -t rsa -C "[email protected]" -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/jerry/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/jerry/.ssh/id_rsa
Your public key has been saved in /c/Users/jerry/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:58T8LFOv9JZrA7Z2ghVVSCKKs+dx1UwuZiG/pq9wEQU [email protected]
The key's randomart image is:
+---[RSA 4096]----+
|          E.+.oo.|
|       . . = Bo  |
|      o . . *.+  |
|       o o =.o   |
|      . S B +.   |
|       o * B+.   |
|        o *++o.. |
|         o.=+o*  |
|          .oo=oo |
+----[SHA256]-----+

将gitlab公钥即id_rsa.pub(Windows下路径为C:/Users/用户名/.ssh/)中的内容配置到公司的gitlab上。

操作步骤如下图。用记事本打开id_rsa.pub,复制有所内容,粘贴到4里;5中标题可任意输入。

1213182-20200811171134726-1223274988.png

2,生成gitlab密钥并配置

  • 在gitbash中输入命令:

    ssh-keygen -t rsa -C "github邮箱地址" -f ~/.ssh/github_rsa

  • 生成对应的github密钥:github_rsa和github_rsa.pub

jerry@Jerry MINGW64 ~
$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/github_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/jerry/.ssh/github_rsa
Your public key has been saved in /c/Users/jerry/.ssh/github_rsa.pub
The key fingerprint is:
SHA256:DvG4umzBRa3gCht0hB9YclwE8SCXoDgGx01IuC/zsfE [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|.BX%Bo .         |
|==B== . .        |
|=oo..+..         |
|=. .. o+         |
| = o .o S        |
|+ = o  +         |
| + = .. .        |
|  o.E.           |
|   .+.           |
+----[SHA256]-----+
  • 将github公钥即github_rsa.pub中的内容配置到自己的github上。操作步骤如下:


    image.png

    打开github_rsa.pub,复制有所内容,填入后点击“Add SSH key”按钮。接着可能会跳转页面需要输入你的GitHub密码,输入确定即可。


    1213182-20200811171229765-156685640.png

3,配置git,访问不同host时访问不同的密钥

  • 进入密钥生成的位置(C:/Users/用户名/.ssh/),手动创建一个config文件(注意这个config文件要无后缀)。

  • 在新建的config文件里面配置如下内容:

    # 自己的github账号配置
    Host github.com
        port 22
        User git
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile C:\Users\jerry\.ssh\github_rsa
    
    # 公司的gitlab账号配置(HostName为公司的gitlab地址)
    Host gitlab.com
        port 22
        User git
        HostName gitlab.xxx.com
        User git
        PreferredAuthentications publickey
        IdentityFile C:\Users\jerry\.ssh\id_rsa
    

    字段配置简单说明:

    Host
        Host可以看作是一个你要识别的模式,对识别的模式,配置对应的主机名和ssh文件
    Port
        自定义的端口。默认为22,可不配置
    User
        自定义的用户名,默认为git,可不配置
    HostName
        真正连接的服务器地址, 公司可能为仅数字IP ,如192.168.41.203
    PreferredAuthentications
        指定优先使用哪种方式验证,支持密码和秘钥验证方式
    IdentityFile
        指定本次连接使用的密钥文件
    

设置HostName时需要注意,复制公司gitlab或者自己的github地址时,需要把"https://"去掉,只保留github.com部分。

二,验证是否设置成功

在C:/Users/用户名/.ssh中,右键打开Git Bash Here,分别输入命令:

# 测试github
ssh -T [email protected]

# 测试gitlab(@符后面的为公司gitlab地址)
ssh -T [email protected]

如下图所示则说明配置成功

jerry@Jerry MINGW64 ~
$ ssh -T [email protected]
The authenticity of host 'github.com (20.205.xxx.xxx)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7xxxxxxxxxxxxxxxARLviKw6E5SY8.
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' (RSA) to the list of known hosts.
Hi yangjian1218! You've successfully authenticated, but GitHub does not provide shell access.

jerry@Jerry MINGW64 ~
$ ssh -T [email protected]
The authenticity of host '192.168.xx.xxx (192.168.xx.xxx)' can't be established.
ED25519 key fingerprint is SHA256:Ib8w8v6Cu5xxxxxxxxxxx34zDifjGQo84X5Y.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.xx.xxx' (ED25519) to the list of known hosts.
Welcome to GitLab, 杨健!

三,git仓库配置

1,简介

在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:
仓库级别 local 【优先级最高】
用户级别 global【优先级次之】
系统级别 system【优先级最低】

  • git 仓库级别对应的配置文件是当前仓库下的.git/config 【在当前目录下.git目录默认是隐藏的,所以在文件管理器中我们要打开显示以藏文件】


    1213182-20200811171543448-1343147572.png
  • image-20211104104132089.png

    git 用户级别对应的配置文件是用户宿主目录下的~/.gitconfig 【宿主目录:C:\Users\jerry】

  • git系统级别对应的配置文件是git安装目录下的 /etc/gitconfig

    简单了解后我们就可以进行配置了

2,配置

  • 用户级别配置

    用户级别是配置公司gitlba账号还是自己github账号,可以自由选择。因为平常使用公司的代码频率较高,所以我选择将gitlab账号配置成用户级别。gitBath下执行如下命令:

git config --global user.name '杨健' #公司账号名称
git config --global user.email '[email protected]' #公司账号邮箱
  • 仓库级别配置

    local(仓库级别)配置成github的账号。选择一个文件夹作为github的本地仓库,在该文件夹里鼠标右键打开Git Bash Here,执行命令:git init

    再执行命令:

git config --local user.name 'yangjian1218'  #github账号名称
git config --local user.email '[email protected]'  #github账号邮箱
之后自己的github的代码都应该在这个仓库下进行pull、push操作。

3.切换账号

如果之前已经上传过代码到其中一个账号,需要上传到另一个账号中,如之前已经上传到gitLab,现在要切换到github中,先要解除设置.

$ git remote rm origin

不然会报错:error: remote origin already exists.

或者直接删除.git文件夹.

添加账号(这一步可省略,)

git config --local user.name 'yangjian1218'  
git config --local user.email '[email protected]' 

再添加远程git仓库

$ git remote add origin https://github.com/yangjian1218/Face-Updown-classify.git

[参考]https://www.cnblogs.com/lfr0123/p/13477001.html

你可能感兴趣的:(同时使用gitHub和gitLab)