如何为同一台机器配置多个 GitHub 账号?

解读

在国内面试中,这道题考察的不是“能不能 clone 两个仓库”,而是“能否让同一台开发机干净、可维护地同时对接多个 GitHub 身份(公司主账号、个人小号、客户项目账号等)”。
面试官想听到的是:

  1. 你理解 Git 与 GitHub 的认证分层(本地 Git 配置 vs 远程 HTTPS/SSH 认证);
  2. 你能给出可落地、可脚本化、可回滚的方案;
  3. 你清楚国内网络环境下“SSH 端口偶尔被封、HTTPS 需要代理”的痛点,并给出兼容策略;
  4. 你能证明方案不会把 commit 记错身份、不会把 token 泄露到日志。

一句话:让“哪个目录用哪个账号”这件事对团队零思考、零风险。

知识点

  1. Git 三层配置优先级:仓库级 > 全局级 > 系统级。
  2. GitHub 两种协议:
    • HTTPS:用 username + personal access token 认证,远程 URL 里可嵌用户名做隔离;
    • SSH:用公钥区分身份,同一台机可生成多组 key,靠 ~/.ssh/config 的 Host 别名映射。
  3. conditional include:Git 2.13+ 支持 [includeIf],按目录自动引入不同配置段,解决 user.name/user.email 写死问题。
  4. credential 辅助工具:
    • Windows:Git Credential Manager Core;
    • macOS:osxkeychain;
    • Linux:store/cache,也可对接密码管理器。
  5. 国内加速:HTTPS 走 https://ghproxy.com 或公司 Nexus 代理;SSH 走 ProxyCommand nc -X connect -x 127.0.0.1:7890 %h %p 统一穿透。
  6. 安全红线:token 最小权限、SSH key 专用、不硬编码在代码或 CI 日志里。

答案

下面给出一套“目录即账号”的完整方案,全部命令在 macOS/Linux/WSL2 下验证通过,Windows 只需把路径改成盘符风格即可。

步骤 1:生成两套 SSH key

ssh-keygen -t ed25519 -C "company@xxx.com" -f ~/.ssh/id_ed25519_company
ssh-keygen -t ed25519 -C "personal@xxx.com" -f ~/.ssh/id_ed25519_personal

把公钥分别上传到对应的 GitHub 账号 Settings → SSH keys。

步骤 2:写 ~/.ssh/config 做别名隔离

Host github-company
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_company
    IdentitiesOnly yes

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes

步骤 3:建立目录策略

~/code/
├─company/      ← 公司项目
└─personal/     ← 个人项目

步骤 4:用 conditional include 让 Git 自动选对身份
创建 ~/.gitconfig.company

[user]
    name = Zhang San
    email = company@xxx.com
[github]
    user = company-gh-username
[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_company

创建 ~/.gitconfig.personal

[user]
    name = San Zhang
    email = personal@xxx.com
[github]
    user = personal-gh-username
[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_personal

在全局 ~/.gitconfig 末尾追加

[includeIf "gitdir:~/code/company/"]
    path = ~/.gitconfig.company
[includeIf "gitdir:~/code/personal/"]
    path = ~/.gitconfig.personal

步骤 5:克隆仓库时使用别名远程地址

cd ~/code/company
git clone git@github-company:org/project.git

cd ~/code/personal
git clone git@github-personal:me/side-project.git

至此,commit 作者信息、push 认证通道完全隔离,切换目录即切换身份,无需手动 git config 或 ssh-add。

HTTPS 场景(国内 443 端口更稳定)

  1. 给每个账号建 token(classic 或 fine-grained),权限最小化;
  2. 用 git config --global credential.https://github.com.useHttpPath true 让 key 带仓库路径;
  3. 在 ~/code/company/ 下执行
git config --local credential.https://github.com.username company-gh-username

第一次 push 时输入 token,Git Credential Manager 会按路径缓存,后续自动匹配。
4. 如果公司强制走统一代理,加环境变量

export https_proxy=http://127.0.0.1:7890

让 Git 与系统代理保持一致即可。

验证

cd ~/code/company/project
git log --oneline -1 --format="%an <%ae>"   # 应输出公司邮箱
ssh -T git@github-company                   # 应输出 Hi company-gh-username!

同理检查 personal 目录,确保互不串扰。

拓展思考

  1. 三套以上账号怎么办?
    继续追加 Host 别名与 includeIf 即可,Git 配置段支持无限扩展;脚本化生成 key 与 config,避免手敲出错。
  2. 同一仓库要同时推送到不同远端(例如 GitHub + 公司 Gitea)?
    在 .git/config 里写两个 remote,各自对应不同的 remote.*.url 与 remote.*.pushurl,commit 只需一次,push 时分两次即可;若担心 author 不一致,用 git commit --author 强制指定。
  3. CI 场景如何复用同一套思路?
    在 Docker 层把不同账号的 key 挂到 /root/.ssh,容器启动时通过 GIT_SSH_COMMAND 环境变量动态指定 key,避免把私钥打镜像;配合 GitHub fine-grained token,只给 repo 级读权限,最小化泄露面。
  4. 国内团队常把代码同时托管在 GitHub 与 Gitee,如何保持双库同步?
    用 GitHub Actions 或 Gitee Go 做镜像 job,密钥存在各自平台的 Secrets 里,脚本里用 git push --mirror 即可;本地开发机仍按目录隔离账号,防止 push 错远端。