0

github.company.com所以,我有我的工作计算机,它在终端上连接到我的 GitHub Enterprise 帐户 ( )。现在我也想在这里设置我的个人帐户 ( 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这里是什么意思?

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

4

1 回答 1

12

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

1. 为 github.company.com 帐户创建 SSH 密钥

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

首先,使用ssh-keygen创建id_rsaid_rsa.pubC:\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>

当您想使用公司帐户提交更改时,只需在提交更改之前再次重新配置用户名和电子邮件。

于 2018-03-29T02:59:31.733 回答