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

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

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

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

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


当前回答

使用HTTPS:

将远程url更改为https:

git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git

你可以开始了:

git push

为了确保提交显示为USERNAME执行,可以设置user.name和user。为这个项目发送电子邮件:

git config user.name USERNAME
git config user.email USERNAME@example.com

其他回答

导航到要将更改推到另一个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

在一台Windows机器上管理多个GitHub帐户(HTTPS)

假设你之前在你的机器上使用git并配置了git全局配置文件。要检查它,打开终端,然后:

git config --global -e

它打开你的编辑器,你可能会看到这样的东西:

[user]
    email = yourEmail@gmail.com
    name = Your_Name
...

这很好,因为你可以把你的代码推送到GitHub账户,而不用每次都输入凭证。 但如果它需要从另一个账户推送回购呢?在这种情况下,git将拒绝403 err,您必须更改全局git凭据。要在凭据管理器中存储一个回购名称,可以使用这个lat集:

git config --global credential.github.com.useHttpPath true

要检查它打开配置一次 Git配置——global -e 您将看到新的配置行

[credential]
    useHttpPath = true
...

就是它。现在,当你第一次推到任何帐户,你会看到一个弹出 Screenshot_1

输入特定的此回购帐户凭据,这将“绑定”此帐户的回购。因此,在您的机器中,您可以指定任意数量的帐户/回购。

想要更详细的解释,你可以看看我在youtube上找到的这个很酷的视频: https://youtu.be/2MGGJtTH0bQ

不像其他答案,在那里你需要遵循几个步骤来使用两个不同的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 /配置文件,如在其他答案中提到的,如果您的两个帐户的主机名不同。

这个答案是给初学者的(非git大师)。我最近遇到了这个问题,也许这只是我的问题,但大多数答案似乎需要对git有相当深入的了解。在阅读了包括这个线程在内的几个堆栈溢出的答案后,下面是我需要采取的步骤,以便在GitHub帐户之间轻松切换(例如,假设有两个GitHub帐户,github.com/personal和gitHub.com/work):

Check for existing ssh keys: Open Terminal and run this command to see/list existing ssh keys ls -al ~/.ssh files with extension .pub are your ssh keys so you should have two for the personal and work accounts. If there is only one or none, its time to generate other wise skip this.- Generating ssh key: login to github (either the personal or work acc.), navigate to Settings and copy the associated email.now go back to Terminal and run ssh-keygen -t rsa -C "the copied email", you'll see:Generating public/private rsa key pair. Enter file in which to save the key (/.../.ssh/id_rsa): id_rsa is the default name for the soon to be generated ssh key so copy the path and rename the default, e.g. /.../.ssh/id_rsa_work if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is: and the image. done.Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting. At this stage, you should see two ssh keys when running ls -al ~/.ssh again. Associate ssh key with gitHub account: Next step is to copy one of the ssh keys, run this but replacing your own ssh key name: pbcopy < ~/.ssh/id_rsa_work.pub, replace id_rsa_work.pub with what you called yours.Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied is id_rsa_work] and navigate toSettings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :D) give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG key instead of New SSH key :D). Associate ssh key with gitHub account: Repeat the above step for your second account. Edit the global git configuration: Last step is to make sure the global configuration file is aware of all github accounts (so to say). Run git config --global --edit to edit this global file, if this opens vim and you don't know how to use it, press i to enter Insert mode, edit the file as below, and press esc followed by :wq to exit insert mode: [inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true

完成了!现在,当尝试从一个回购中推或拉,你会被问到哪个GitHub账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。

有很多答案解释如何完成你的要求,但我想提供一个不同的角度。

我认为把工作和个人事情分开是有道理的。在所有操作系统上切换用户帐户是非常简单的,所以你可以为工作和娱乐创建单独的操作系统帐户,并在每个操作系统中登录不同的github帐户。

保持清晰的关注点分离也可以……

防止你的工作文件和个人文件混在一起 降低对错误资源执行操作的风险 可能会将安全漏洞限制在一组帐户/凭据上