首頁
學習紀錄
遊戲心得影視Life書單案件檔案
Side Projects委託作品與二創互動實驗場
Kurau
百百 BLOG
首頁
學習紀錄
遊戲心得影視Life書單案件檔案
Side Projects委託作品與二創互動實驗場
Kurau

Kurau Blog

「隨心而寫,真真假假,都是我」

一個記錄生活、輸出興趣的個人空間。
遊戲、影視、閱讀、學習……每一段體驗都值得留下文字。

頁面導覽

  • 學習紀錄
  • 遊戲心得
  • 影視Life
  • 書單
  • 委託作品與二創
  • Kurau
  • 合作邀請

找到我

歡迎來 Discord 找我聊天!

“曾經發生的事不可能忘記,只是暫時想不起來而已。”-《神隱少女》

© 2026 Kurau All rights reserved

開發工具

github 多個帳戶 SSH登入

By Kurau·2025-11-12·Updated 2026-05-09·4 分鐘閱讀

GitHub 多帳號 SSH 切換

TL;DR
同一台機器要用 個人 + 公司 兩個 GitHub 帳號 → 用 SSH config 設不同 Host alias,clone / push 用對應的 host name。每個 repo 也要設專案級 user.name / email,避免 commit 帳號錯誤。

完整設定步驟

1. 產生兩組 SSH Key

# 個人帳戶
ssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_personal

# 工作帳戶
ssh-keygen -t ed25519 -C "work@email.com" -f ~/.ssh/id_ed25519_work
bash

-t ed25519 比 RSA 短、安全 — 2026 年首選 ed25519。

2. 設定 SSH config

# ~/.ssh/config

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

# 工作 GitHub
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes
ssh

Host 是別名(任你定),HostName 是真實 GitHub。IdentitiesOnly yes== 強制只用指定的 key,別讓 ssh-agent 嘗試其他 key。

3. 把公鑰加到 GitHub

# 複製公鑰
cat ~/.ssh/id_ed25519_personal.pub
# 貼到個人帳號:Settings → SSH and GPG keys → New SSH key

cat ~/.ssh/id_ed25519_work.pub
# 貼到工作帳號:同上
bash

4. 測試連線

ssh -T git@github.com-personal
# Hi Bobo100! You've successfully authenticated.

ssh -T git@github.com-work
# Hi work-user! You've successfully authenticated.
bash

看到正確的 username 才算成功。

5. Clone 時用對應 Host

# 個人
git clone git@github.com-personal:Bobo100/cra-template-bobonext.js.git

# 工作
git clone git@github.com-work:company-org/secret-project.git
bash

注意:不是 github.com,是你定義的 github.com-personal / github.com-work。


改既有 repo 的 remote

如果原本是 github.com 的 origin,要改成新的 host alias:

# 看現在 remote
git remote -v
# origin  git@github.com:Bobo100/repo.git

# 改成 personal host
git remote set-url origin git@github.com-personal:Bobo100/cra-template-bobonext.js.git

# 確認
git remote -v
# origin  git@github.com-personal:Bobo100/cra-template-bobonext.js.git
bash

設定 repo 級的 user.name / email

SSH key 解決連線,user.name / email 解決 commit 顯示:

# 進入 repo
cd ~/Projects/MyRepo

# 設定該 repo 用個人 user
git config user.name "Bobo100"
git config user.email "w150lione@gmail.com"

# 工作 repo 設成工作 user
cd ~/Work/CompanyRepo
git config user.name "Bobo (Work)"
git config user.email "bobo@company.com"
bash

沒設好的話 commit author 會用全域 user(可能跟 SSH key 對不上),GitHub UI 看 commit 會顯示「錯的人」。


進階:用 includeIf 自動切換

2.13+ Git 支援 includeIf ,根據資料夾路徑自動套不同 config:

# ~/.gitconfig
[user]
  name = Bobo100
  email = w150lione@gmail.com

[includeIf "gitdir:~/Work/"]
  path = ~/.gitconfig-work

[includeIf "gitdir:~/Personal/"]
  path = ~/.gitconfig-personal
ini
# ~/.gitconfig-work
[user]
  name = Bobo (Work)
  email = bobo@company.com

[core]
  sshCommand = "ssh -i ~/.ssh/id_ed25519_work"
ini
# ~/.gitconfig-personal
[user]
  name = Bobo100
  email = w150lione@gmail.com

[core]
  sshCommand = "ssh -i ~/.ssh/id_ed25519_personal"
ini

以後 clone 到 ~/Work/ 自動用工作帳號,不用每個 repo 手動設。


ssh-agent 管 key(避免每次輸密碼)

# 啟動 agent
eval "$(ssh-agent -s)"

# 加 key 進 agent
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work

# 查現在管什麼
ssh-add -l
bash

Mac 可以加 keychain:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
bash

Windows 用 GitHub for Windows 或 PuTTY,內建 agent。


常見地雷

1. 弄混 Host alias
git@github.com-personal:user/repo.git 跟 git@github.com:user/repo.git 是兩件事。後者用預設 SSH key(可能不是你想的那個)。 看 .git/config 確認 url 是用哪個 host。
2. 公鑰 vs 私鑰貼錯
上 GitHub 的是 .pub(公鑰),不是私鑰。私鑰是 絕不能外洩 的東西,洩了等於別人能裝你登入。
3. 忘記設 repo user
SSH 用 personal key,但 commit 顯示是 default user → GitHub 看 commit 是「unknown contributor」(綠點不會在 personal account 算)。
同步 GPG signing key
進階:把 commit 都 sign 上,Verified badge 看起來專業。
git config --global commit.gpgsign true
git config --global user.signingkey YOUR_KEY_ID
bash

目錄

    ◆ 相關文章

    • github 大小寫問題

      2026-05-09
    • Git教學

      2026-05-09
    • lodash

      2026-05-09
    • npm --save 到底是什麼 --save-dev 不一樣嗎

      2026-05-09
    ← 上一篇Component介紹下一篇 →Ladle 前端測試工具

    ◆ 關於作者

    Kurau

    個人寫作 / 創作的 SoT,記錄遊戲、影視、學習與生活。

    更多 Kurau 的文章