windows下git多账号配置,如何在一台电脑管理多个ssh-key

如果有多个github账号,分别访问不同的项目,这时候可以通过下面的步骤设置多个SSH秘钥

1. 生成新的ssh 秘钥

将your_email@example.com替换为你的邮箱地址,key_name是你想要的密钥名称

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

image

成功后控制台会告诉你秘钥生成在了哪个目录,这时你去目录下找到 key_name.pub 的文件,把公钥拷贝下来。

2. 在GitHub上添加SSH密钥

  1. 访问 GitHub。
  2. 点击右上角你的头像,选择 ‌Settings‌。
  3. 在左侧菜单中选择 ‌SSH and GPG keys‌。
  4. 点击 ‌New SSH key‌。
  5. 输入一个标题(例如,key_name),将刚刚拷贝的的公钥内容(位~/.ssh/key_name.pub文件中)复制到文本框中。
  6. 点击 ‌Add SSH key‌。

3. 配置SSH配置文件


# GitHub账户1
Host github.com-account1
  HostName github.com
  User git
  IdentityFile ~/.ssh/key_name1
  IdentitiesOnly yes

# GitHub账户2
Host github.com-account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/key_name2
  IdentitiesOnly yes

4. 使用配置的SSH密钥克隆仓库

当你使用git clone或git push/pull时,可以通过指定配置文件中的主机名来使用相应的密钥:

git clone git@github.com-account1:username/repo.git
git clone git@github.com-account2:username/repo.git
posted @ 2026-04-08 14:24  阳光下的小水仙  阅读(19)  评论(0)    收藏  举报