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

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

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

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

我如何用各自的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密钥生成过程中创建的名称相同。

其他回答

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

IntelliJ Idea有内置的支持 https://www.jetbrains.com/help/idea/github.html#da8d32ae

通过在~/.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/

另一种更简单的方法是使用多个桌面应用程序,就像我在做什么,使用帐户A在Github桌面,而使用帐户B在Github Kraken

使用码头工人!

这可能不适合每个人,但这是我最后为自己做的。

我所有的开发现在都在docker容器中完成。我是一个vim用户,但我知道VSCode有插件来使用docker容器。

因此,由于我有不同的docker容器为不同的项目/语言,我可以在每个内部使用不同的git配置。问题解决了。