试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。

工作账户是先建立的,一切都完美无缺。

然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。

我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。

我如何用各自的GitHub凭证推/拉两个帐户?


当前回答

最简单和直接的方法(IMHO) -没有配置文件,没有太多的麻烦

只需创建另一个ssh密钥。

假设你有一个新的GitHub工作帐户,只需为它创建一个新密钥:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`

您只需要运行一次上面的程序。

现在你应该有了旧的和新的,要查看它们,运行:

ls -al ~/.ssh

从现在开始,每次你想在两者之间切换时,只需运行:

ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub

为了切换到旧的,再次运行:

 ssh-add -D
 ssh-add ~/.ssh/<previous id_rsa>

其他回答

更简单和容易修复,以避免混乱..

Windows用户可以为不同的项目使用多个或不同的git帐户。

以下步骤: 进入控制面板,搜索凭据管理器。 然后转到凭证管理器-> Windows凭证

现在在Generic Credentials Heading下删除git:https//github.com节点

这将删除当前凭据。现在你可以通过git添加任何项目,它会要求用户名和密码。

当你遇到任何问题与其他帐户做同样的过程。

不像其他答案,在那里你需要遵循几个步骤来使用两个不同的github帐户从同一台机器,对我来说,它在两个步骤。

你只需要:

1)在~/下为您的每个帐户生成SSH公私钥对。使用不同的名称和SSH位置

2)在“设置”中,将生成的公钥添加到对应的帐号“>> SSH和GPG密钥>> SSH新密钥”。

使用实例生成SSH公私钥对。

cd ~/.ssh
ssh-keygen -t rsa -C "email@work.com" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_PERSONAL"

作为上述命令的结果,id_rsa_WORK和id_rsa_WORK。pub文件将为您的工作帐户(ex - git.work.com)和id_rsa_PERSONAL和id_rsa_PERSONAL创建。Pub将为您的个人帐户(ex - github.com)创建。

创建之后,从每个公共(*.pub)文件复制内容,并对每个帐户执行步骤2。

PS:没有必要为~/下的每个git帐户创建一个主机条目。Ssh /配置文件,如在其他答案中提到的,如果您的两个帐户的主机名不同。

导航到要将更改推到另一个GitHub帐户的目录。 在终端/命令行中创建一个新的SSH密钥。 ssh-keygen -t rsa -C " your-email-address " 下面将显示: 生成rsa公私钥对。 输入保存密钥的文件(/home/your_username/.ssh/id_rsa):

复制并粘贴路径,后面跟着一个可识别的文件名:

/home/your_username/.ssh/id_rsa_personal

4)然后它会问你以下问题:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

5)您现在可以输入以下命令查看您在本地机器上拥有的所有SSH密钥:

ls -al ~/.ssh

您应该能够看到新的SSH密钥文件。正如你可以看到在我的一个,我有id_rsa_test和id_rsa_personal.pub。

drwx------  2 gmadmin gmadmin 4096 Nov 16 22:20 .
drwxr-xr-x 42 gmadmin gmadmin 4096 Nov 16 21:03 ..
-rw-------  1 gmadmin gmadmin 1766 Nov 16 22:20 id_rsa_personal
-rw-r--r--  1 gmadmin gmadmin  414 Nov 16 22:20 id_rsa_personal.pub
-rw-r--r--  1 gmadmin gmadmin  444 Nov  6 11:32 known_hosts

6)接下来需要复制存储在id_rsa_personal中的SSH密钥。酒吧文件。您可以在您选择的文本编辑器中打开此文件。我目前正在使用atom,所以我使用以下命令打开文件:

atom ~/.ssh/id_rsa_personal.pub

然后你会得到类似这样的结果:

ssh-rsa AAB3HKJLKC1yc2EAAAADAQABAAAAQCgU5+ELtwsKkmcoeF3hNd7d6CjW+dWut83R/DC01E/YzLc5ZFri18doOwuQoeTPpmIRVDGuQQsZshjDrTkFy8rwKWMlXl7va5olnGICcpg4qydEtsW+MELDmayW1HHsi2xHMMGHlNv

7)复制并导航到您的GitHub帐户→设置→SSH和GPG密钥 8)单击“新建SSH密钥”。复制键,给它一个标题并添加它。 9)从终端添加密钥

ssh-add ~/.ssh/id_rsa_personal
Enter passphrase for /home/your_username/.ssh/id_rsa_personal: 

10)配置用户和密码。

git config --global user.name "gitusername"
git config --global user.email "gitemail"

11)我们已经做好了承诺和推进的准备。

git init
git add .
git commit 
git push

你不需要维护两个不同的个人和工作账户。事实上,Github建议你保持一个单一的帐户,并帮助你合并两者。

如果您决定不需要维护多个帐户,请按照下面的链接进行合并。

https://help.github.com/articles/merging-multiple-user-accounts/

选项0:你不想乱搞操作系统设置..你只是想为一次回购提交一个不同的github账户,使用不同的公钥。

解决方案:

create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa copy and add the pub key to the github account: (see github instructions) test the key with github: ssh -i ~/.ssh/alt_rsa T git@github.com clone the repo using the git protocol (not HTTP): git clone git@github:myaccount... in the cloned repo: git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null" git config user.name [myaccount] git config user.email [myaccount email] now you should be able to git push correctly without interferring with your everyday git account