一台电脑登录多个GitHub方式

  1. 首先生成多个SSH Key(给不同账号配置对应的私钥)
1
ssh-keygen -t ed25519 -C "你的邮箱"
img2

在Ubuntu系统上保存位置: 根目录./ssh

img3
  1. 把 .pub 文件内容 填到github里面:

    1
    2
    // 查看内容
    cat ~/.ssh/id_ed25519.pub
    img1
  2. 把key添加到ssh agent上,由于默认使用的是.ssh/id_rsa,所以你需要显示告诉ssh agent加上新的key

    1
    ssh-add ~/.ssh/id_ed25519

    想要查看的话执行:ssh-add -l

  3. 因为是多账户所以要配置.ssh/config

    一般默认没有config文件,需要手动创建 touch config

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $ vi .ssh/config

    # 加上以下内容
    #default github
    Host github-A
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

    Host github-B
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work_two
    IdentitiesOnly yes

    到这里git的配置完成了。

    可以进行远程测试:

    1
    2
    3
    $ ssh –T github-A

    $ ssh –T github-B

    将代码进行上传,这里有一点需要注意,我们远程的写法需要改变。需要填上刚才配置config的HOST的名字:

    原来的写法

    1
    $ git clone git@github.com: 11699/learngit.git

    现在

    1
    $ git clone git@github-B:librityYu/Whatif.git

    还想在完美点可以给仓库设置单独的用户名:

    1
    2
    3
    $ git config user.name "one_name" ; git config user.email "one_email"

    $ git config user.name "two_name" ; git config user.email "two_email"