IDE 切换 github 账号

  1. 生成两个ssh密钥对

    • 第一个密钥对
      ssh-keygen -t rsa -C "your_first_email@example.com"
      以上指令会在用户目录下的.ssh文件夹中生成这两个文件:id_rsa和id_rsa.pub。其中,id_rsa是私钥,id_rsa.pub是公钥,需要配置到github上面

    • 第二个密钥对
      ssh-keygen -t rsa -f ~/.ssh/id_rsa_personal -C "personal_email@example.com"
      为了与第一次生成的密钥进行区分,这里我们在命令中增加了-f 参数来指定密钥名称
      同样会生成对应的公钥和私钥

    • 注意:在.ssh目录下,‌执行ssh-add id_rsa命令将秘钥添加到SSH代理中。‌如果遇到“Could not open a connection to your authentication agent”的错误,‌先执行ssh-agent bash命令启动SSH代理,‌然后再执行ssh-add id_rsa

  2. 在GitHub或其他托管平台上,分别将两个公钥(即以.pub为后缀的文件)添加到相应账号的SSH密钥中。登录到你的账号,在设置中找到SSH and GPG keys,添加新的SSH密钥。

  3. 配置ssh 用于指定不同账号对应的密钥
    在本地的~/.ssh/目录下创建一个配置文件 config

    • 第一个账号
      Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa

    • 第二个账号
      Host github-personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_udo
      “`
      注意将`IdentityFile`的值设置为你生成的密钥文件名。

  4. 测试切换是否成功

    • ssh -T git@github.com
    • ssh -T git@github-personal

posted on 2024-08-06 15:17  朝朝暮Mu  阅读(58)  评论(0)    收藏  举报