同一台主机两个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
相关推荐
程序员Soldier2 小时前
open-AutoGLM部署
github
Emma歌小白3 小时前
把代码上传到 GitHub Gist
github
逛逛GitHub3 小时前
挖到 5 个很哇塞的 AI 开源项目,快来瞧瞧。
github
CoderJia程序员甲4 小时前
GitHub 热榜项目 - 日榜(2025-12-11)
git·ai·开源·llm·github
码界奇点4 小时前
基于Python与GitHub Actions的正方教务成绩自动推送系统设计与实现
开发语言·python·车载系统·自动化·毕业设计·github·源代码管理
测试人社区-小明7 小时前
量子计算对测试未来的潜在影响:软件测试的范式变革
opencv·测试工具·pycharm·机器人·自动化·github·量子计算
uhakadotcom7 小时前
Tomli 全面教程:常用 API 串联与实战指南
前端·面试·github
lin62534227 小时前
Android仿小米视频播放器的缩放滚轮
android·git·github
mit6.8248 小时前
[Solution] Github Permission denied (publickey)
github