Windows和Linux下Gitlab以及Github多账号(3个及以上)SSH配置 📅 2026/6/24 4:27:25 多 Git 账号管理(SSH 模式)—— Win 篇动机我在使用多个 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 密钥对为每个账号生成一对公钥和私钥# 个人 GitHub 账号 ssh-keygen -t ed25519 -C xxxyyygmail.com -f ~/.ssh/id_ed25519_personal # 公司 GitHub 账号 ssh-keygen -t ed25519 -C xxxyyyyour-company.com -f ~/.ssh/id_ed25519_company_github # 公司 GitLab 账号 ssh-keygen -t ed25519 -C xxxyyyyour-company.com -f ~/.ssh/id_ed25519_company_gitlab2. 配置 SSH 配置文件在C:\Users\你的用户名\.ssh\config中配置如下内容# 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_personal3. 创建账号专属的项目目录和配置文件3.1 创建三个文件夹建议将不同账号的项目放在不同路径中例如C:/Projekte/Personal/ C:/Projekte/CompanyGithub/ C:/Projekte/CompanyGitlab/3.2 分别创建.gitconfig文件个人 GitHub 账号C:\Users\你的用户名\.gitconfig-personal[user] name xxxyyy email xxxyyygmail.com公司 GitHub 账号C:\Users\你的用户名\.gitconfig-company-github[user] name xxxyyy-dev0511 email xxxyyyyour-company.com公司 GitLab 账号C:\Users\你的用户名\.gitconfig-company-gitlab[user] name xxxyyy email xxxyyyyour-company.com3.3 验证配置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 --list4. 配置全局.gitconfig文件在C:\Users\你的用户名\.gitconfig中添加如下内容[user] name xxxyyy email xxxyyyyour-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-gitlab5. 使用 SSH clone 多账号项目5.1 Clone 公司 GitLab 项目git clone gitgitlab.com-company:ComnanyRepo/kelvin5/iris/k5-graphql.git5.2 Clone 公司 GitHub 项目git clone gitgithub.com-company:ComnanyRepo/Grafana-Docker.git5.3 Clone 个人 GitHub 项目git clone gitgithub.com-personal:cjy513203427/IADBE.git多 Git 账号管理(SSH 模式)—— Linux 篇动机Linux 服务器环境下没有浏览器无法使用 HTTPS 图形化登录公司 GitLab 强制 SSH而 GitHub 项目也建议统一用 SSH 避免重复输密码。配置多个 SSH 密钥 includeIf规则后不同账号的仓库可以自动使用对应密钥和用户身份无需手动切换。适用于以下场景无图形界面的 Linux 服务器(GPU server、云主机等)只能使用 SSH。同时维护多个平台账号(如公司 GitHub 公司 GitLab)需要自动路由身份。希望git commit的作者信息按仓库目录自动切换不需要每个仓库手动git config。1. 生成 SSH 密钥对为每个账号生成一对公钥和私钥# 公司 GitHub 账号 ssh-keygen -t ed25519 -C xxxyyyyour-company.com -f ~/.ssh/id_ed25519_company_github # 公司 GitLab 账号 ssh-keygen -t ed25519 -C xxxyyyyour-company.com -f ~/.ssh/id_ed25519_company_gitlab生成后~/.ssh/目录结构如下~/.ssh/ ├── id_ed25519_company_github ├── id_ed25519_company_github.pub ├── id_ed25519_company_gitlab └── id_ed25519_company_gitlab.pub2. 将公钥添加到各平台输出公钥内容分别粘贴到对应平台的Settings → SSH Keys → Add new key# GitHub 公钥 cat ~/.ssh/id_ed25519_company_github.pub # GitLab 公钥 cat ~/.ssh/id_ed25519_company_gitlab.pub添加后验证连接ssh -T gitgithub.com-company # Hi xxxyyy-dev0511! Youve successfully authenticated... ssh -T gitgitlab.com-company # Welcome to GitLab, xxxyyy!2. 配置 SSH 配置文件在~/.ssh/config中写入以下内容(文件不存在则新建)# 公司 GitHub 账号 Host github.com-company HostName github.com User git IdentityFile ~/.ssh/id_ed25519_company_github IdentitiesOnly yes # 公司 GitLab 账号 Host gitlab.com-company HostName gitlab.com User git IdentityFile ~/.ssh/id_ed25519_company_gitlab IdentitiesOnly yesIdentitiesOnly yes禁止 SSH 自动尝试其他密钥确保精确匹配。3. 创建账号专属的项目目录和配置文件3.1 创建两个文件夹建议将不同账号的项目放在不同路径中mkdir -p ~/project/CompanyGithub mkdir -p ~/project/CompanyGitlab3.2 分别创建.gitconfig文件公司 GitHub 账号~/.gitconfig-company-github[user] name xxxyyy-dev0511 email xxxyyyyour-company.com公司 GitLab 账号~/.gitconfig-company-gitlab[user] name xxxyyy email xxxyyyyour-company.com3.3 验证配置git config --file ~/.gitconfig-company-github --list git config --file ~/.gitconfig-company-gitlab --list4. 配置全局.gitconfig文件在~/.gitconfig中添加如下内容[user] name xxxyyy email xxxyyyyour-company.com [includeIf gitdir:~/project/CompanyGithub/] path ~/.gitconfig-company-github [includeIf gitdir:~/project/CompanyGitlab/] path ~/.gitconfig-company-gitlab注意includeIf规则只在进入某个git 仓库内部(存在.git目录)时才生效在父目录~/project/CompanyGithub/下直接运行git config不会触发。5. 使用 SSH clone 多账号项目5.1 Clone 公司 GitLab 项目cd ~/project/CompanyGitlab git clone gitgitlab.com-company:CompanyOrg/kelvin5/iris/k4-wms.git5.2 Clone 公司 GitHub 项目cd ~/project/CompanyGithub git clone gitgithub.com-company:CompanyOrg/support-ai-agent-server.gitClone URL 中的 Host 别名(github.com-company、gitlab.com-company)必须与~/.ssh/config中的Host条目一致SSH 才能自动选择正确的密钥。与 Windows 版的主要差异项目WindowsLinuxSSH 配置路径C:\Users\用户名\.ssh\config~/.ssh/configgitconfig 路径C:\Users\用户名\.gitconfig~/.gitconfig项目目录C:/Projekte/CompanyGithub/~/project/CompanyGithub/验证连接同同无图形界面不适用SSH 是唯一方式验证最终效果进入具体仓库确认身份来源cd ~/project/CompanyGithub/support-ai-agent-server/ git config --show-origin user.name # file:/home/用户名/.gitconfig-company-github xxxyyy-dev0511 cd ~/project/CompanyGitlab/k4-wms/ git config --show-origin user.name # file:/home/用户名/.gitconfig-company-gitlab xxxyyy