一台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 "youremail@example.com"

配置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 git@gitee.com

配置不同的user

比如对于gitlab我想用张三(zhangsan, zhangsan@abc.com),对于gitee我想用王五(wangwu,wangwu@abc.com),需要做如下配置

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

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

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

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

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

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

shell 复制代码
git config user.name
相关推荐
vibecoding日记2 天前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记2 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger3 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思4 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享4 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立4 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003965 天前
git切换当前分支到远程分支
git
ON10N5 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code