git使用 - 在一台电脑上同时使用gitlab和github

配置ssh密钥

分别对githubn和gitlab生成对应的密钥(默认情况下本地生成的秘钥位于/Users/用户名/.ssh/),并且配置git访问不同host时访问不同的密钥,

1、 在gitbash中使用 ssh-keygen -t rsa -C "公司邮箱地址"-f ~/.ssh/gitlab_id_rsa 生成对应的gitlab密钥:gitlab_id_rsa 和 gitlab_id_rsa.pub
2、 将gitlab公钥即 gitlab_id_rsa.pub 中的内容配置到公司的gitlab上
3、 在gitbash中使用 ssh-keygen -t rsa -C "xxxxx@163.com" -f ~/.ssh/github_id_rsa 生成对应的github密钥:github_id_rsa 和 github_id_rsa.pub
4、 将github公钥即 github_rsa.pub 中的内容配置到自己的github上

配置config文件

在密钥生成的地方添加一个config文件

# gitlab
Host gitlab
    HostName gitlab  #注意,这里填写的是公司gitlab的地址,比如world.taobao.com,不要后面的路径,
    User 公司邮箱
    IdentityFile ~/.ssh/gitlab_id_rsa
# github
Host github.com
    HostName github.com
    User xxxxx@163.com
    IdentityFile ~/.ssh/github_id_rsa

测试

在gitbash中使用 ssh -T git@HostName 进行测试,出现以下即为成功

hello@TH201956259 MINGW64 ~/.ssh
$ ssh -T git@gitlab.com
Welcome to GitLab, hello!

hello@TH201956259 MINGW64 ~/.ssh
$ ssh -T git@github.com
Hi hello! You've successfully authenticated, but GitHub does not provide shell access.

配置仓库

1. 用户级别配置
因为公司的代码使用频率较高,所以我们将git配置文件的global(用户级别)设置为公司的gitlab账号,在gitlab仓库中使用如下命令:

$ git config --global user.name 'catalinaLi' #公司账号名称
$ git config --global user.email 'catalinaLi@companyName.com' #公司账号邮箱

2. 仓库级别配置
进入github仓库执行如下命令:

$ git config --local user.name 'username' #github账号名称
$ git config --local user.email 'username@gmail.com' #github账号邮箱

之后我们github的代码都应该在这个仓库下拉取。

 

posted @ 2020-11-07 17:29  赤子说  阅读(446)  评论(0编辑  收藏  举报