一个git如何同时绑定gitlab与github

简介: 一个git如何同时绑定gitlab与github

需求

公司电脑git为gitlab,但又想往github上传东西,需要同时使用gitlab与github.

解决

首先新建一个SSH Key

查看已有 密钥

Mac 下输入命令 ls ~/.ssh/,如果此时git已经配置过gitlab,看到 id_rsaid_rsa_pub 则说明已经有一对密钥。

ls ~/.ssh/
→ x - one glt :
 config 
→ x - one git :
 id _ rsa id _ rsa . pub id _ rsa _ hy id _ rsa _ hy . pub known _ hosts known _ hosts . old 

那此时应生成新的公钥,预备给github使用

要保证与已有密钥文件不同,我们此次命名为id_rsa_hy,最后的参数为注册github的邮箱

ssh-keygen -t rsa -f ~/.ssh/id_rsa_hy -C "12345@qq.com"

此时直接一直回车就行

上面命令执行完,~/.ssh下就出现了刚才的文件

→ x - one git :( master ) ls -/. ssh /
 COnflg 
1d_ rSa 
→ x - one git :( master )]
 id _ rsa . pub id _ rsahy idrsa _ hy . pub known _ hosts known _ hosts . old 

此时复制'id_rsa_hy.pub'中的内容到github中新建一个SSH Key

设置不同 Host 对应同一 HostName 但密钥不同

.ssh 文件夹下新建 config 文件并编辑,令不同 Host 实际映射到同一HostName,但密钥文件不同。

cd ~
cd .ssh
touch config
vim config

按i键进入编辑状态,输入以下代码

Host github.com
HostName github.com
User xxx@xxx.com       //user后边为github的邮箱
IdentityFile ~/.ssh/id_rsa_hy
Host gitlab.xxx.cn    //host后边为公司gitlab域名
HostName gitlab.xxx.cn   //同上为公司gitlab域名
User xxx@xxx.com          //user后为gitlab的邮箱
IdentityFile ~/.ssh/id_rsa

测试SSH连接

ssh -T git@github.com
ssh -t git@gitlab.xxx.cn

提示成功则表示连接成功啦!

此时可能会有:

The authenticity of host 'github.com (192.30.255.112)' can't be established.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

只需要在最后,输入yes

Are you sure you want to continue connecting (yes/no/[fingerprint])?  //输入yes,回车

最后,push/pull时,可推送到相应的仓库

由于个人gitlab使用更多,所以将全局配置(global)配置为gitlab

后续单独建立自己仓库时,再使用本地配置对应的账号

// 全局配置
$ git config --global user.name  'gitlab账号名' 
$ git config --global user.email  'gitlab账号邮箱'
// 本地配置
$ git config --local user.name  'github账号名'   
$ git config --local user.email  'github邮箱'



目录
相关文章
|
24天前
|
Linux 网络安全 开发工具
码农日常生活之Git & Gitlab & Gitk
本文是关于Git和GitLab的配置使用教程,包括基础设置、日常命令操作以及图形工具的使用。
40 2
|
11天前
|
开发工具 git 开发者
掌握版本控制的艺术:Git 与 GitHub 的高效使用指南
在软件开发中,版本控制对于代码管理和团队协作至关重要。本文详细介绍了 Git 的核心概念与命令,包括初始化、创建仓库、文件跟踪、分支管理和远程仓库操作。同时,探讨了如何利用 GitHub 进行项目组织、代码审查及自动化工作流。通过遵循最佳实践,如频繁提交、清晰的信息记录和合理使用分支,开发者可以提升工作效率和团队协作能力,确保项目的持续成功。
|
7天前
|
Shell 网络安全 开发工具
Git,GitHub,Gitee&IDEA集成Git
Git提交项目到GitHub简洁版、版本控制、安装、常用命令、分支、团队协作机制、Github、Gitee远程仓库、IDEA集成Git、IDEA集成Github、IDEA集成Gitee
Git,GitHub,Gitee&IDEA集成Git
|
1月前
|
开发工具 git
[git]记配置本地git到gitlab并推送
[git]记配置本地git到gitlab并推送
|
1月前
|
Linux 开发工具 git
[git]基于GitLab搭建本地Git服务
[git]基于GitLab搭建本地Git服务
|
2月前
|
网络安全 开发工具 git
【git】解决git报错:ssh:connect to host github.com port 22: Connection timed out 亲测有效
【git】解决git报错:ssh:connect to host github.com port 22: Connection timed out 亲测有效
508 1
|
3月前
|
Shell 测试技术 网络安全
【GIT】如何在GitHub上向一个开源项目贡献代码?
【GIT】如何在GitHub上向一个开源项目贡献代码?
47 5
|
3月前
|
开发工具 git
【已解决】error: failed to push some refs to ‘git@github.com:BATdalao/Github-green.git‘
【已解决】error: failed to push some refs to ‘git@github.com:BATdalao/Github-green.git‘
62 1
|
2月前
|
网络安全 开发工具 git
使用git克隆仓库报错:Warning: Permanently added‘github.com’ to the .....(ssh )
所以,你可以安全地忽略这个警告,它不会影响到你使用git克隆仓库。如果你已经成功克隆了仓库,那么一切都在正常工作。如果你在克隆过程中遇到其他问题,那可能需要查看具体的错误信息来解决。
43 0
|
3月前
|
Shell 开发工具 git
如何使用git上传代码github仓库
如何使用git上传代码github仓库