如何在电脑上配置多个github账号?

Sat*_*ian 0 git version-control github github-enterprise

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

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

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

Mar*_*Liu 11

在同一台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\.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
Run Code Online (Sandbox Code Playgroud)

4.通过SSH协议使用两个GitHub账号

您可以通过以下方式克隆您的 github.company.com 帐户:

git clone git@github.company.com:companyaccountname/reponame
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式将个人帐户添加为本地存储库中的远程帐户:

git remote add personal git@github-personal:personalaccountname/reponame
Run Code Online (Sandbox Code Playgroud)

通过以下命令将文件从个人帐户获取到公司存储库:

git merge upstream master --allow-unrelated-histories
# make changes
git add .
git commit -m 'add the changes from peronal repo'
git push origin master
Run Code Online (Sandbox Code Playgroud)

要由个人仓库的提交者从本地公司仓库提交/推送更改,您只需要在本地仓库提交更改之前重新配置用户名和电子邮件:

git config user.name <personal github account username>
git config user.email <email for login the personal github account>
Run Code Online (Sandbox Code Playgroud)

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