github gitlab 多用户多平台切换

一、背景

我需要用账号1 来登录并管理github 账号

我需要用账号2 来登录并管理gitlab 账号

二、设置账号 邮箱

设置账号1用户名与邮箱

git config --global user.name "miaojiang"
git config --global user.email "[email protected]"

三、生成本地密钥

生成账号1 对应的密钥

ssh-keygen -t rsa -C '[email protected]'

连续三次回车

四、github 添加公钥

4.1、查看密钥

路径:通常位于用户的 ~/.ssh/ 目录下

cd ~/.ssh

查看密钥

  • id_rsa 是私钥文件,用于对加密的数据进行解密。
  • id_rsa.pub 是公钥文件,用于与他人分享你的公钥,以便他们可以将加密的数据发送给你。

4.2、github 添加公钥

1、、将公钥 内容 填写在github 的setting中

读取并复制id_rsa.pub 里面的内容

cat id_rsa.pub

2、登录github--点击头像--Setttings

3、SSH and GPC keys

 点击New SSH Key 添加公钥

五、本地项目推送

5.1 github 创建仓库

举例,创建名为mock的仓库

5.2 本地设置仓库

1、进入项目路径,执行

git init

2、添加项目文件

git add README.md

或者

git add .

3、commit

git commit -m "first commit"

4、
git branch -M master

5、关联远程仓库

git remote add origin [email protected]:187133/mock.git

注意:

 生成远程仓库地址,可以选择HTTPS 方式与 SSH方式

如果选择HTTPS方式,后期提交代码还需要填写用户名与密码。

为了方便,我选择SSH方式 生成远程仓库链接方式。

6、推送

git push -u origin master

六、切换用户名切换平台

6.1、本地备份github密钥

1、进入路径:

cd ~/.ssh

2、备份

mv id_rsa id_rsa_github
mv id_rsa.pub id_rsa.pub_github 

6.2 设置新的用户名与新的邮箱

 git config --global user.name "zhangsan"
git config --global user.email "[email protected]"

6.3 生成新的密钥

ssh-keygen -t rsa -C '[email protected]'

连续三次回车

6.4 可以关联新的仓库

git remote add origin [email protected]:zhangsan/mock.git

七、关联新的仓库

本地的仓库,在之前,已经关联过远程A仓库了,现在我想将本地的项目,取消A仓库的关联,关联新的B仓库。

1、删除关联的origin的远程库

git remote rm origin

2、 关联新的的仓库

git remote add origin https://gitee.com/xxxxxx.git

八、报错处理 error: remote origin already exists.

如果你clone下来一个别人的仓库,在此基础上完成你的代码,推送到自己的仓库可能遇到如下问题:
error: remote origin already exists.表示远程仓库已存在。
因此你要进行以下操作:
1、先输入git remote rm origin 删除关联的origin的远程库
2、关联自己的仓库 git remote add origin https://gitee.com/xxxxxx.git
3、最后git push origin master,这样就推送到自己的仓库了
 

猜你喜欢

转载自blog.csdn.net/qq_39208536/article/details/131919488