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

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

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

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

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


当前回答

我看到这里有很多可行的变通办法。作为权宜之计,@Greg的方法似乎相对简单。但是,从长远来看,最好为所有不同的帐户设置单独的ssh密钥。这个视频简单演示了一下。以下是视频博客中提到的步骤。

步骤1 -为新帐户创建一个新的SSH密钥,并将其保存在一个单独的文件中(例如~/. SSH /id_rsa_new_account_name),而不是在原来的文件中,例如~/. SSH /id_rsa

ssh-keygen -t rsa -C "your-email-address"

步骤2 -附加新密钥

接下来,登录到你的第二个GitHub账户 浏览到Account Overview,并附加新密钥~/.ssh/id_rsa_new_account_name。在SSH Public Keys部分中。 在终端中,通过键入SSH -add ~/. SSH /id_rsa_new_account_name告诉SSH添加新的标识。如果成功,您将看到Identity Added的响应。

步骤3 -创建配置文件

touch ~/.ssh/config

并将以下内容保存到文件中

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

#New Account
Host github-new-account-name
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_new_account_name

第四步——尝试一下 现在,每当您想要使用新帐户和相关的回购时,请为相应的回购键入此选项

git remote add origin git@github-new-account-name:your-domain.com/your-repo.git

其他回答

通过在~/.ssh/config中为github.com创建不同的主机别名, 并为每个主机别名提供自己的SSH密钥,您可以轻松地使用多个 Github账户没有混淆。 这是因为github.com不是用用户来区分的,用户总是用git来区分, 但用的是你用来连接的SSH密钥。 只要使用自己的主机别名配置远程源即可。”

以上摘要来自下面博客文章的评论。

我发现这个解释是最清楚的。至少在2012年4月,这对我来说是有效的。

http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/

可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用SSH,我的步骤和解决方案是基于HTTPS。

Create your project DIR on your local machine. Example d:\test_git_multiple_account go to the folder "test_git_multiple_account" Add few files here into the DIR Open Git bash here and run following command a. git init // initialization b. git add , // add c. git commit -m "initial commit" you will get following output : in my case i use to add one python file created from code. **[master (root-commit) d4defd9] initial commit 2 files changed, 4 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Hello.py** d. git remote add origin <HTTPS repo link> e. git remote -v // check the repo version f. git push origin master it will ask your git hub user name and password via popup screen. you will get the following output Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 411 bytes | 31.00 KiB/s, done. Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'master' on GitHub by visiting: remote: https://github.com/vishvadeepaktripathi/Hello_Py/pull/new/master remote: To https://github.com/vishvadeepaktripathi/Hello_Py.git * [new branch] master -> master

这将创建一个名为master的新分支。 你可以提交到主分支一旦你改变了分支,在这种情况下,你现有的文件将被删除。所以我建议签入主分支到第一步,然后继续为每个命令,如果你想直接签入主分支。 在第一次登录时,它可能会给你一个错误消息,并再次要求登录名和密码,然后它会将您的更改发布到Git中心。

一旦这样做,你会得到消息到新的拉请求到你的github帐户。 您可以将您的更改从主分支合并到主分支。

我在这里创建了主分支,根据你的选择命名你的分支。 还要附加屏幕截图。 在这里输入图像描述

Go to ~/.ssh Create a file named config(have no extension ) Open config file & add below codes. (change according to your account) Account 1 # account_1 Host gitlab.com-account_1 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_account_1 Account 2 # Account2 Host gitlab.com-Account2 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account2 Account 3 # Account_3 Host github.com-Account3 HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account_3 Add remote url as follows Account 1 git remote add origin git@gitlab.com-account_1:group_name/repo_name.git Account 2 git remote add origin git@gitlab.com-Account2:group_name/repo_name.git Account 3 git remote add origin github.com-Account3:github_username/repo_name.git

确保IdentityFile名称与您在ssh密钥生成过程中创建的名称相同。

除了为多个帐户创建多个SSH密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。

#this store the password permanently
$ git config --global credential.helper wincred

我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。

在一台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