跳轉至

建立 2024-01-01 更新 2026-09-15

單電腦使用多個帳戶

同一台電腦要登入兩個 GitHub 帳號時,SSH 會預設只用一把金鑰。解法是每帳號一把金鑰,在 ~/.ssh/config 用不同 Host 別名對應,clone 時網址寫那個別名。

參考:Connecting to GitHub with SSH。

產生金鑰

每個信箱/帳號各做一次,用 -f 指定不同檔名,避免覆蓋預設的 id_ed25519:

ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_email01

舊版 ssh:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_email01

把對應的 .pub 貼到該 GitHub 帳號的 SSH keys。

複製公鑰:

# Windows
clip < ~/.ssh/id_email01.pub

# macOS
pbcopy < ~/.ssh/id_email01.pub

~/.ssh/config

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_email01

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_email02

Git 會依你寫的 Host 選金鑰。測試:

ssh -T git@github-work
ssh -T git@github-personal

clone 時用別名

git clone git@github-work:公司帳號/專案.git
git clone git@github-personal:個人帳號/專案.git

該專案的 user.email 建議設成本機目錄層級(不要 --global),避免用公司信箱提交到個人儲存庫:

git config user.email "這個專案該用的信箱"
git config user.name "這個專案該顯示的名稱"