LoginSignup
0

More than 5 years have passed since last update.

【Github】一台电脑使用多个 Github 账户

Posted at

账户所用 SSH 秘钥生成

$ cd ~/.ssh
$ ls
id_rsa id_rsa.pub

$ ssh-keygen -t rsa -c [mail] -f [filename]

将生成的 SSH 秘钥登录到 Github 账号中

登录 Github 账户,进入 https://github.com/settings/ssh
「Add SSH Key」中,将新生成的 .pub 秘钥粘贴至输入框

编辑 ~/.ssh/config

对文件 ~/.ssh/config 进行编辑(如文件不存在则新建一个):

Host github.com
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa  # 主账号所使用的 SSH 秘钥
  TCPKeepAlive yes
  IdentitiesOnly yes
Host github.com.sub  # (1) 副账号:可任意命名
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_sub  # (2) 副账号所使用 SSH 秘钥路径
  TCPKeepAlive yes
  IdentitiesOnly yes

使用副账号 clone 远程仓库

正常情况下 git clonegit remote add

$ git clone git@github.com:xxx/multi-account-sample.git
$ git remote add origin git@github.com:xxx/multi-account-sample.git

在使用副账号时则将@之后的部分进行修改

$ git clone git@github.com.sub:xxx/multi-account-sample.git
$ git remote add origin git@github.com.sub:xxx/multi-account-sample.git

设置副账号的 user.nameuser.email

在副账号所管理的文件下,执行一次如下命令:

$ git config user.name [sub_account.user_name]
$ git config user.email [sub_account.email]

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0