GitHub 多账户设置

1 阅读1分钟

拥有多个 GitHub 账号,通过配置 SSH 文件来让各个账户正常工作且不发生冲突。

生成 SSH

由于不同的 GitHub 不能使用同一个 SSH 公钥,所以要生成两个不同的 SSH 分别对应两个主账户和副账户。

Ubuntu 生成 SSH 的命令如下:

ssh-keygen -t rsa -f ~/.ssh/id_rsa_blog -C "blogemail@gmail.com"
ssh-keygen -t rsa -f ~/.ssh/id_rsa -C "mainemail@gmail.com"

-f 选项指定生成钥匙对的文件名

SSH 配置

编辑 ~/.ssh/config SSH 配置文件,没有该文件则新建。

# mainemail@gmail.com
Host github-main.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# blogemail@gmail.com
Host github-blog.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_blog

然后,以后使用 main 账户添加远程仓库需要这样添加:

git remote add origin git@github-main.com:username/demo.git

类似,使用 blog 账户时是这样:

git remote add origin git@github-blog.com:username/demo.git

而非原来的:

git remote add origin git@github.com:username/demo.git

测试是否配置成功

部署相应的 SSH 公钥到 GitHub 后,尝试在相应的本地仓库 git push 几个文件测试。

来源:www.sulinehk.com/post/github…