同一台主机两个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程序员甲10 小时前
GitHub 热榜项目 - 日榜(2026-1-13)
人工智能·ai·大模型·github·ai教程
iuu_star14 小时前
项目开源-零基础掌握GitHub大文件上传(Git)
开源·github
wangruofeng14 小时前
OpenCode 上手初体验:从安装到基础使用
github·ai编程
Albert Edison15 小时前
【Git】多人协作二(不同分支下)
git·elasticsearch·svn·github
数据大魔方19 小时前
【期货量化实战】豆粕期货量化交易策略(Python完整代码)
开发语言·数据库·python·算法·github·程序员创富
逛逛GitHub20 小时前
这 2 个 Skills 开源项目,最近在 GitHub 上火了。
github
是毛毛吧1 天前
GitHub前沿----AI Agent 爆发前夜:深度解析 UI-TARS, MCP 与 Claude Code 如何重塑开发流
开源·github
这儿有一堆花1 天前
Hexo博客保姆级安装教程
github
Cosimac1 天前
用 npm 做免费图床,这操作绝了!
npm·github
WaterRun1 天前
一个由Rust实现的, 好得多的Windows tree命令: tree++项目简介
rust·github