一台PC创建多个Git平台账号

因为工作需要,需要使用Gitlab,所以公司的项目都是使用Gitlab来提交代码的;但是平时自己想学习研究一些东西是使用的Gitee,所以需要在一台电脑上配置多个Git平台账号,以下是配置的步骤:

删除Global的设置

首先通过如下命令查看是否设置了全局配置

shell 复制代码
git config --global --list

如果有配置,则逐一删除,比如user.email,user.name

shell 复制代码
 git config --global --unset user.email
 git config --global --unset user.name

生成一对新的RSA公私钥

用如下命令生成,此过程会让用户输入公私钥的存储位置,一般存放为 ~/.ssh/xxxx(xxxx为新的文件名,注意这里不要和旧的重名了,比如可以设置为 ~/.ssh/id_rsa_gitee)

shell 复制代码
ssh-keygen -t rsa -C "[email protected]"

配置config文件

在 ~/.ssh/目录下面添加一个名为config的文件,注意这里是没有后缀的,一般建议使用vi或者vim命令来创建文件,内容如下

shell 复制代码
Host gitlab.com
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa
    PreferredAuthentications publickey
    User zhangsan

Host gitee.com
    HostName gitee.com
    IdentityFile ~/.ssh/id_rsa_gitee
    PreferredAuthentications publickey
    User wangwu

测试

用如下命令检测是否配置成功

shell 复制代码
 ssh -T [email protected]

配置不同的user

比如对于gitlab我想用张三(zhangsan, [email protected]),对于gitee我想用王五(wangwu,[email protected]),需要做如下配置

  1. 修改~/.gitconfig,内容如下:
shell 复制代码
[user]
    name = youName
    email = [email protected]

[includeIf "gitdir:**/company/**"]
    path = ~/.gitconfig_work

[includeIf "gitdir/i:**/personal/**"]
    path = ~/.gitconfig_self
  1. 创建~/.gitconfig_work,内容如下:
shell 复制代码
[user]
    name = zhangsan
    email = [email protected]
  1. 创建~/.gitconfig_self,内容如下:
shell 复制代码
[user]
    name = wangwu
    email = [email protected]

当clone自己的项目时,要把项目clone到文件夹personal下面即可

当clone公司的项目时,要把项目clone到文件夹company下面即可

记得,在commit前运行如下命令确认一下

shell 复制代码
git config user.name
相关推荐
一直在学习的小白~14 小时前
Sourcetree安装使用的详细教程
git
陈苏同学16 小时前
从 Git 到 GitHub - 使用 Git 进行版本控制 - Git 常用命令
git·github
影子240117 小时前
git项目迁移,包括所有的提交记录和分支 gitlab迁移到gitblit
git·gitlab·git迁移
laimaxgg1 天前
Git撤销修改
git
象骑士Hack1 天前
Xterminal(或 X Terminal)通常指一类现代化的终端工具 工具介绍
git
新时代牛马1 天前
git处理浅克隆
git
学习2年半1 天前
git相关
git
极小狐1 天前
如何减少极狐GitLab 容器镜像库存储?
运维·git·rpc·kubernetes·ssh·gitlab·terraform
C.er1 天前
Git回顾
git
极小狐2 天前
如何使用极狐GitLab 软件包仓库功能托管 ruby?
开发语言·数据库·人工智能·git·机器学习·gitlab·ruby