如何在您的计算机上配置多个 github 帐户?

How to configure multiple github accounts on your computer?

所以,我有我的工作计算机,它已连接到我在终端上的 GitHub 企业帐户 (github.company.com)。现在我也想在这里设置我的个人帐户(github.com)。

我一直在学习本教程 - https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574 在第 3 步,当我必须创建我的配置文件时,我的 HostName 应该是 github.company.com 还是 github.com?我可以为 Host 取任何(有意义的)名称吗?还有,这里的User是什么意思?

此外,如何在终端上的这两个帐户之间切换 - 即我的个人帐户和我的企业帐户?有些事情我需要从我的个人帐户中提交,其余的使用企业帐户。

在同一台 PC 上使用两个 GitHub 帐户的详细步骤如下:

1。为 github.company.com 帐户

创建 SSH 密钥

如果您已经将 SSH 密钥添加到您的 github.company.com 帐户,请跳过此步骤。

首先,使用ssh-keygenC:\Users\username\.ssh中创建id_rsaid_rsa.pub

然后,在github.company.com帐户中添加id_rsa.pub file的内容作为SSH密钥。

2。为您的个人帐户创建 SSH 密钥 github.com

使用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 密钥

C:\Users\username\.ssh 目录中,创建一个包含以下内容的 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

要 commit/push 由个人回购的提交者从本地公司回购更改,您只需要 re-config 在本地回购中提交更改之前的用户名和电子邮件:

git config user.name <personal github account username>
git config user.email <email for login the personal github account>

当您想使用公司帐户提交更改时,只需在提交更改前再次 re-config 用户名和电子邮件。