找回密码
 立即注册
首页 业界区 业界 Windows下Gitlab多账号(3个及以上)SSH配置 ...

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

孓访懔 2025-7-4 16:54:07
多 Git 账号管理(SSH 模式)

动机
我在使用多个 Git 账号时经常遇到麻烦:每次切换账号都要重新登录,尤其是浏览器身份验证时。我默认浏览器使用 Edge,但 GitHub 的登录信息保存在 Chrome 中,跳转验证让人头皮发麻。
公司使用 GitLab,强制要求 SSH,而我个人的 GitHub 账号之前是用 HTTPS。频繁在公司和个人项目间切换,要反复输入密码,还经常需要改 git config 设置用户名和邮箱。
为了解决这些问题,我统一改用 SSH,并配置多个密钥和 .gitconfig,实现账号之间的无缝切换。
适用于以下场景

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

为每个账号生成一对公钥和私钥:
  1. # 个人 GitHub 账号
  2. ssh-keygen -t ed25519 -C "xxxyyy@gmail.com" -f ~/.ssh/id_ed25519_personal
  3. # 公司 GitHub 账号
  4. ssh-keygen -t ed25519 -C "xxxyyy@your-company.com" -f ~/.ssh/id_ed25519_company_github
  5. # 公司 GitLab 账号
  6. ssh-keygen -t ed25519 -C "xxxyyy@your-company.com" -f ~/.ssh/id_ed25519_company_gitlab
复制代码
1.png

2. 配置 SSH 配置文件

在 C:\Users\你的用户名\.ssh\config 中配置如下内容:
  1. # Company GitHub account
  2. Host github.com-company
  3.     HostName github.com
  4.     User git
  5.     IdentityFile ~/.ssh/id_ed25519_companyGithub
  6. # Company GitLab account
  7. Host gitlab.com-company
  8.     HostName gitlab.com
  9.     User git
  10.     IdentityFile ~/.ssh/id_ed25519_companyGitlab
  11.        
  12. # Personal GitHub account
  13. Host github.com-personal
  14.     HostName github.com
  15.     User git
  16.     IdentityFile ~/.ssh/id_ed25519_personal
复制代码
3. 创建账号专属的项目目录和配置文件

3.1 创建三个文件夹

建议将不同账号的项目放在不同路径中,例如:
  1. C:/Projekte/Personal/
  2. C:/Projekte/CompanyGithub/
  3. C:/Projekte/CompanyGitlab/
复制代码
2.png

3.2 分别创建 .gitconfig 文件

个人 GitHub 账号:C:\Users\你的用户名\.gitconfig-personal
  1. [user]
  2.         name = xxxyyy
  3.         email = xxxyyy@gmail.com
复制代码
公司 GitHub 账号:C:\Users\你的用户名\.gitconfig-company-github
  1. [user]
  2.         name = xxxyyy-dev0511
  3.         email = xxxyyy@your-company.com
复制代码
公司 GitLab 账号:C:\Users\你的用户名\.gitconfig-company-gitlab
  1. [user]
  2.         name = xxxyyy
  3.         email = xxxyyy@your-company.com
复制代码
3.3 验证配置
  1. git config --file C:/Users/你的用户名/.gitconfig-personal --list
  2. git config --file C:/Users/你的用户名/.gitconfig-company-github --list
  3. git config --file C:/Users/你的用户名/.gitconfig-company-gitlab --list
复制代码
4. 配置全局 .gitconfig 文件

在 C:\Users\你的用户名\.gitconfig 中添加如下内容:
  1. [user]
  2.         name = xxxyyy
  3.         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 项目
  1. git clone git@gitlab.com-company:ComnanyRepo/kelvin5/iris/k5-graphql.git
复制代码
5.2 Clone 公司 GitHub 项目
  1. git clone git@github.com-company:ComnanyRepo/Grafana-Docker.git
复制代码
5.3 Clone 个人 GitHub 项目
  1. git clone git@github.com-personal:cjy513203427/IADBE.git
复制代码
来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

您需要登录后才可以回帖 登录 | 立即注册