Windows下Gitlab多账号(3个及以上)SSH配置

多 Git 账号管理(SSH 模式)

动机

我在使用多个 Git 账号时经常遇到麻烦:每次切换账号都要重新登录,尤其是浏览器身份验证时。我默认浏览器使用 Edge,但 GitHub 的登录信息保存在 Chrome 中,跳转验证让人头皮发麻。

公司使用 GitLab,强制要求 SSH,而我个人的 GitHub 账号之前是用 HTTPS。频繁在公司和个人项目间切换,要反复输入密码,还经常需要改 git config 设置用户名和邮箱。

为了解决这些问题,我统一改用 SSH,并配置多个密钥和 .gitconfig,实现账号之间的无缝切换。

适用于以下场景

  1. 公司项目强制要求使用 SSH 方式进行 clone 或其他 Git 操作。
  2. 多账号频繁使用时,推荐使用 SSH;偶尔使用可选择 GitHub Desktop、封装 Git 的 IDE,或浏览器登录切换。
  3. 拥有 3 个及以上 Git 账号时,浏览器切换效率低,建议使用 SSH。

1. 生成 SSH 密钥对

为每个账号生成一对公钥和私钥:

bash 复制代码
# 个人 GitHub 账号
ssh-keygen -t ed25519 -C "xxxyyy@gmail.com" -f ~/.ssh/id_ed25519_personal

# 公司 GitHub 账号
ssh-keygen -t ed25519 -C "xxxyyy@your-company.com" -f ~/.ssh/id_ed25519_company_github

# 公司 GitLab 账号
ssh-keygen -t ed25519 -C "xxxyyy@your-company.com" -f ~/.ssh/id_ed25519_company_gitlab

2. 配置 SSH 配置文件

C:\Users\你的用户名\.ssh\config 中配置如下内容:

bash 复制代码
# Company GitHub account
Host github.com-company
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_companyGithub

# Company GitLab account
Host gitlab.com-company
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_companyGitlab
	
# Personal GitHub account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal

3. 创建账号专属的项目目录和配置文件

3.1 创建三个文件夹

建议将不同账号的项目放在不同路径中,例如:

复制代码
C:/Projekte/Personal/
C:/Projekte/CompanyGithub/
C:/Projekte/CompanyGitlab/

3.2 分别创建 .gitconfig 文件

个人 GitHub 账号:C:\Users\你的用户名\.gitconfig-personal
ini 复制代码
[user]
	name = xxxyyy
	email = xxxyyy@gmail.com
公司 GitHub 账号:C:\Users\你的用户名\.gitconfig-company-github
ini 复制代码
[user]
	name = xxxyyy-dev0511
	email = xxxyyy@your-company.com
公司 GitLab 账号:C:\Users\你的用户名\.gitconfig-company-gitlab
ini 复制代码
[user]
	name = xxxyyy
	email = xxxyyy@your-company.com

3.3 验证配置

bash 复制代码
git config --file C:/Users/你的用户名/.gitconfig-personal --list
git config --file C:/Users/你的用户名/.gitconfig-company-github --list
git config --file C:/Users/你的用户名/.gitconfig-company-gitlab --list

4. 配置全局 .gitconfig 文件

C:\Users\你的用户名\.gitconfig 中添加如下内容:

ini 复制代码
[user]
	name = xxxyyy
	email = xxxyyy@your-company.com

[includeIf "gitdir:C:/Projekte/Personal/"]
    path = C:/Users/JinyaoChen/.gitconfig-personal

[includeIf "gitdir:C:/Projekte/CompanyGithub/"]
    path = C:/Users/JinyaoChen/.gitconfig-company-github

[includeIf "gitdir:C:/Projekte/CompanyGitlab/"]
    path = C:/Users/JinyaoChen/.gitconfig-company-gitlab

5. 使用 SSH clone 多账号项目

5.1 Clone 公司 GitLab 项目

bash 复制代码
git clone git@gitlab.com-company:ComnanyRepo/kelvin5/iris/k5-graphql.git

5.2 Clone 公司 GitHub 项目

bash 复制代码
git clone git@github.com-company:ComnanyRepo/Grafana-Docker.git

5.3 Clone 个人 GitHub 项目

bash 复制代码
git clone git@github.com-personal:cjy513203427/IADBE.git
相关推荐
cd_949217219 分钟前
水处理市场升级,台州海德能环保科技凭技术创新与服务并重脱颖而出
大数据·运维·科技
.YYY16 分钟前
万字详解|Linux Chrony 时间服务完整学习手册
linux·运维
Li-Yongjun25 分钟前
Linux 内核等待队列(Wait Queue)
linux·运维·windows
humors22136 分钟前
Windows运维与安全场景合集(不定期更新)
大数据·运维·服务器·程序人生·网络安全
SAP上海工博云署36 分钟前
2026年中小企业SAP服务商选型技术解析
大数据·运维·数据库·人工智能·信息可视化·运维开发·信息与通信
WXDcsdn37 分钟前
联想服务器使用RAID卡组建RAID(企业服务器解决方案)
运维·服务器
jimy140 分钟前
Linux动态加载器,loader,dynamic linker
linux·运维·服务器
三十..41 分钟前
华为云全栈:网络/存储/运维高能实战
运维·华为云
lolo大魔王44 分钟前
Linux 文件权限超详细详解(读懂权限标识、数字权限、特殊权限、chmod/chown)
linux·运维·服务器
Plastic garden1 小时前
Docker(3)Docker 镜像 & Dockerfile
运维·docker·容器