在同一台 PC 上使用两个 GitHub 帐户的详细步骤如下:
1. 为 github.company.com 帐户创建 SSH 密钥
如果您已将 SSH 密钥添加到您的 github.company.com 帐户,请跳过此步骤。
首先,使用ssh-keygen创建id_rsa和id_rsa.pub在C:\Users\username\.ssh.
然后,将 的内容id_rsa.pub file作为 SSH 密钥添加到 github.company.com 帐户中。
2. 为您的个人帐户 github.com 创建 SSH Key
使用ssh-keygen -t rsa -C "email address for the personal github account",并将密钥保存在 中/c/Users/username/.ssh/id_rsa_personal。
现在将id_rsa_personal.pub文件的内容作为 SSH 密钥添加到您的个人 GitHub 帐户中。
3.配置两个SSH Key
在C:\Users\username\.sshdictory 中,创建一个config包含以下内容的文件:
Host github.company.com
HostName github.company.com
User git
IdentityFile ~/.ssh/id_rsa
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
4.通过SSH协议使用两个GitHub帐户
您可以通过以下方式克隆您的 github.company.com 帐户:
git clone git@github.company.com:companyaccountname/reponame
然后通过以下方式将个人帐户添加为本地存储库中的远程:
git remote add personal git@github-personal:personalaccountname/reponame
通过以下命令将文件从个人帐户获取到公司仓库:
git merge upstream master --allow-unrelated-histories
# make changes
git add .
git commit -m 'add the changes from peronal repo'
git push origin master
要由个人 repo 的提交者从本地公司 repo 提交/推送更改,您只需在本地 repo 中提交更改之前重新配置用户名和电子邮件:
git config user.name <personal github account username>
git config user.email <email for login the personal github account>
当您想使用公司帐户提交更改时,只需在提交更改之前再次重新配置用户名和电子邮件。