Windows 下配置多個 GitHub 賬號的 SSH Key
假設你有以下兩個 SSH key 文件:
- 第一個賬號:
id_rsa
(默認) - 第二個賬號:
id_rsa_github
? 步驟:在 Windows 上配置多個 GitHub 賬號 SSH Key
1?? 打開 SSH 配置文件(沒有就新建)
路徑為:
C:\Users\Administrator\.ssh\config
如果沒有這個文件,就用記事本新建一個名為
config
的無擴展名文件。
2?? 編輯 config 文件,寫入如下內容:
# 第一個 GitHub 賬號(默認)
Host github.comHostName github.comUser gitIdentityFile C:/Users/Administrator/.ssh/id_rsaIdentitiesOnly yes# 第二個 GitHub 賬號(別名為 github-second)
Host github-secondHostName github.comUser gitIdentityFile C:/Users/Administrator/.ssh/id_rsa_githubIdentitiesOnly yes
Windows 下路徑中的
\
換成/
是 SSH 格式要求。
3?? 克隆第二個賬號的倉庫用別名
git clone git@github-second:your-username/your-repo.git
4?? 已有項目如何修改遠程地址?
進入項目文件夾:
cd your-repo
git remote set-url origin git@github-second:your-username/your-repo.git
5?? 測試 SSH 是否配置成功
打開 Git Bash 或 PowerShell,測試連接:
ssh -T git@github.com # 測試第一個賬號
ssh -T git@github-second # 測試第二個賬號
成功會提示:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
? 補充建議
設置 Git 用戶(針對每個倉庫):
git config user.name "你的用戶名"
git config user.email "你的郵箱"
或全局設置:
git config --global user.name "你的默認用戶名"
git config --global user.email "你的默認郵箱"