同一台主机两个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
相关推荐
m0_6948455732 分钟前
Docker 从入门到实践教程:docker_practice 完整学习指南
运维·服务器·docker·容器·云计算·github
TT哇1 小时前
将 Gitee 混合项目仓库拆分并精准迁移至 GitHub(保留完整 Commit 历史)
gitee·github
阿里嘎多学长2 小时前
2026-03-24 GitHub 热点项目精选
开发语言·程序员·github·代码托管
逛逛GitHub3 小时前
开源 2 周 5 万 Star!AI 大佬开源了一个自己做实验的工具。
github
请你喝好果汁6413 小时前
GitHub 开发流程学习笔记
笔记·学习·github
峰向AI4 小时前
两个 Claude Code 插件一起用,效果居然这么好?
github
CaracalTiger4 小时前
Windows 环境下 OpenClaw 的安装与千问Qwen、Kimi、MiniMax、GLM国产大模型配置完全指南
运维·ide·windows·开源·github·aigc·ai编程
yy_xzz4 小时前
【Linux开发】 05 Linux 多进程并发服务器
linux·服务器·github
汪海游龙5 小时前
03.26 AI 精选:让 Claude 像人一样操作电脑执行任务
github