同一台机器上添加多个git账号

2020-10-02  本文已影响0人  无暇的风笛

步骤如下:

  1. 生成第二个账号密钥,要与原默认密钥id_rsa区分开,执行如下命令:

    ssh-keygen -t rsa -C "你的第二个账号" -f ~/.ssh/id_rsa_new
    
  2. 把新生成的公钥id_rsa_new.pub添加到你的账号里

  3. 把新生成的key添加到ssh-agent上

    先执行

    ssh-agent bash
    

    然后执行

    ssh-add ~/.ssh/id_ras_new
    
  4. 配置ssh config

    vi ~/.ssh/config
    

    添加以下内容后保存即可

    Host github.com
      HostName github.com
      IdentityFile ~/.ssh/id_rsa
    
    # 同一个域名使用不同账号
    Host github_test
      HostName github.com
      IdentityFile ~/.ssh/id_rsa_test
    
    # 不同域名
    Host git_hub
      HostName github.com
      IdentityFile ~/.ssh/id_rsa_new
    
  5. 当存在同一个域名,需要使用不同的账号时,就可以使用别名来明确需要用哪个账号进行操作,例如

    将本地仓库绑定到远端

    git remote add origin git@github_test:abcd/test.git
    
上一篇 下一篇

猜你喜欢

热点阅读