目 录CONTENT

文章目录

Git | 同时使用 Github 和 GitLab

如风
2023-10-24 / 0 评论 / 0 点赞 / 48 阅读 / 336 字

Git | 同时使用 Github 和 GitLab

背景

由于公司使用的是 GitLab 仓库管理项目代码,而我的博客项目保存在 Github,每天记录了一些笔记都需要提交到 Github,但是 Gitlab 使用的是公司邮箱而GitHub使用的是个人邮箱,使用同一套 SSH 公钥能保证代码正常提交,但不可避免的会造成了提交不匹配的问题。所以这个时候通过 Git 同时使用 Github 和 GitLab 就十分重要!

解决方案

根据不同的邮箱生成两套 SSH Key,指令如下

# 生成一个 GitLab 用的 SSH-Key
$ ssh-keygen -t rsa -C 'xxx@company.com' -f ~/.ssh/gitlab-rsa
# 生成一个 GitHub 用的 SSH-Key
$ ssh-keygen -t rsa -C 'xxx@qq.com' -f ~/.ssh/github-rsa

分别将 gitlab-rsa.pub 和 GitHub-rsa.pub 放到 gitlab和GitHub网站上

在 ~/.ssh 目录下新建一个 config 文件,添加如下内容

# Host 和 HostName 填写git服务器的域名
# IdentityFile 指定私钥的路径
 
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab-rsa
 
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-rsa

通过 ssh 命令分别测试

$ ssh -T git@gitlab.com
$ ssh -T git@github.com
 
# 成功的话会返回如下内容
# Hi xxx! You've successfully authenticated...

参考

0

评论区