同一台主机两个github账号切换使用

同一主机两个github账号切换使用

可能某些原因,我们需要在公司与个人之间切换相关的开发者账号。如同一台主机两个github账号

此为windows环境

操作步骤如下

首先,创建2组 公私钥

默认用户目录 ~/.ssh

bash 复制代码
$ ssh-keygen -t ed25519 -C "your_email_1@example.com"
Enter file in which to save the key (~/.ssh/id_ed25519):
# 回车, 默认生成 id_ed25519 和 id_ed25519.pub

$ ssh-keygen -t ed25519 -C "your_email_1@example.com"
Enter file in which to save the key (~/.ssh/id_ed25519):
# 录入 id_ed25519_sec, 生成 id_ed25519_sec 和 id_ed25519_sec.pub

其次,生成一个新文件 ~\.ssh\config

假设两个github账号分别为

comment username email
github-one oneAcc oneAcc@aa.cc
github-sec testeru testeru@xx.cc
bash 复制代码
# Default account (first account)
Host github-one
    HostName github.com
    PreferredAuthentications publickey
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

# Second account
Host github-sec
    HostName github.com
    PreferredAuthentications publickey
    User git
    IdentityFile ~/.ssh/id_ed25519_sec
    IdentitiesOnly yes

再次,将生成的公钥分别放入不同的github账号中

github > settings -> keys -> Access -> SSH and GPG keys -> New SSH key

最后测试

假设 second account 账号实际为 testeru, 且有一个repository 为abc

复制代码
$ git clone git@github-sec:testeru/abc.git

备注

代码修改提交(可能会失败),如失败,设置当前目录下的用户为

bash 复制代码
$ git config user.name testeru
$ git config user.email testeru@xx.cc
相关推荐
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2026-01-27)
github
无限进步_18 小时前
206. 反转链表 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
无限进步_1 天前
203. 移除链表元素 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
测试_AI_一辰1 天前
Agent & RAG 测试工程 02:RAG 从最小闭环到可信
开发语言·前端·人工智能·github·ai编程
iwanghang1 天前
Windows下AndroidStudio4.0.0使用GitHub管理版本,以及如何改成码云Gitee
windows·github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-01-26)
ai·开源·大模型·github·ai教程
轻抚酸~1 天前
使用git维护github项目的简单实践
git·github
前端 贾公子2 天前
Git优雅使用:git tag操作
javascript·github
康康的AI博客2 天前
用Claude Code构建企业级GitHub CI/CD安全自动化流程
安全·ci/cd·github
前端 贾公子2 天前
npm 发包配置双重身份验证
前端·javascript·微信小程序·小程序·github