公司自己部署了私有Gitlab仓库,个人代码发布在GitHub仓库,git的提交是通过邮箱来识别的,就这导致了私人代码仓库和公司代码仓库用户混淆了。

解决办法

ssh协议配置config文件。

1.生成ssh key

# github
ssh-keygen -t rsa -C "example@github.com"

# gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "example@gitlab.com"

进入~/.ssh会发现新生成了两个文件,分别是id_rsa.pubid_rsa.gitlab.pub

2.将生成好的秘钥分别上传到GithubGitlab

3.进入~/.ssh目录下,生成config文件。

touch ~/.ssh/config
vi ~/.ssh/config

添加下面的命令

# git@gitlab.com
Host *.gitlab.com
User wuyanzu
IdentityFile ~/.ssh/id_rsa.gitlab

# git@github.com
Host *.github.com
User zuyanwu
IdentityFile ~/.ssh/id_rsa

4.最后验证一下是否配置成功

ssh -T git@gitlab.com && ssh -T git@github.com

如果出现以下命令,表示配置完成。

Welcome to GitLab, wuyanzu!

Hi zuyanwu! You've successfully authenticated, but GitHub does not provide shell access.

使用

在项目下需要设置对应的用户名和邮箱,使用以下命令:

git config --local user.name "xxx"
git config --local user.email "xxx"

否则将会使用全局的配置,你也可以更改全局的默认配置:

git config --global user.name "xxx"
git config --global user.email "xxx"

或者重置全局配置:

git config --global --unset user.name
git config --global --unset user.email