试图从我的电脑上完成我实际的“工作”回购,以及我在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>

其他回答

以防你不想弄乱~/。这里提到的Ssh /config文件,你可以运行git config core。ssh -i ~/.使用实例Ssh /custom_id_rsa”,你想从一个不同的帐户提交。

其余的设置是相同的:

使用SSH -keygen -t rsa -f ~/为第二个帐户创建新的SSH密钥。SSH -f ~/.ssh/custom_id_rsa 使用您的其他帐户登录github,转到https://github.com/settings/keys,并粘贴~/.ssh/custom_id_rsa.pub的内容 确保您使用SSH而不是HTTPS作为远程url: git remote set-url origin git@github.com:upstream_project_teamname/upstream_project.git

使用码头工人!

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

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

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

相反,您只需将新的电子邮件id添加到您的个人git帐户。这样您就不需要添加另一个SSH密钥。只需按以下步骤配置新邮箱即可 Git配置——全局用户。电子邮件newemail。然后你就可以复制那个回购了。

您所需要做的就是使用多个SSH密钥对配置您的SSH设置。

这个链接很容易理解(谢谢Eric): http://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574 生成SSH密钥(Win/msysgit): https://help.github.com/articles/generating-an-ssh-key/

第一个环节的相关步骤:

Generate an SSH-key: ssh-keygen -t ed25519 -C "john@doe.example.com" Follow the prompts and decide a name, e.g. id_ed25519_example_company. Copy the SSH public-key to GitHub from ~/.ssh/id_ed25519_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_ed25519_doe_company Create a config file in ~/.ssh with the following contents: Host github-doe-company HostName github.com User git IdentityFile ~/.ssh/id_ed25519_doe_company Add your remote: git remote add origin git@github-doe-company:username/repo.git or change using: git remote set-url origin git@github-doe-company:username/repo.git


此外,如果你正在使用多个使用不同角色的存储库,你需要确保你的各个存储库有相应的用户设置覆盖:

设置用户名,电子邮件和GitHub令牌-覆盖个人回购的设置 https://help.github.com/articles/setting-your-commit-email-address-in-git/

注意: 有些人可能需要不同的电子邮件用于不同的存储库,从git 2.13开始,你可以通过编辑全局配置文件在目录的基础上设置电子邮件:~/。Gitconfig使用这样的条件:

[user]
    name = Default Name
    email = defaultemail@example.com

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

然后是特定于工作的配置~/work/。Gitconfig看起来是这样的:

[user]
    name = Pavan Kataria
    email = pavan.kataria@example.com

谢谢@alexg在评论中告诉我这件事。

简单的方法,不需要处理SSH键盘

只需更改user.name & user。在回购的本地电子邮件。给那些你想要推到回购的人。

示例:我在gitlab有一个工作帐户,有一个项目。克隆这个项目/回购后,在终端中,我输入:

git config user.name "my-work-username"
git config user.email "my-work-email-id"

现在,假设我在Gitlab中有另一个个人项目/回购,我想将它与我的个人帐户关联起来。就像上面一样,克隆后,我输入:

git config user.name "my-personal-username"
git config user.email "my-personal-email-id"

希望这能有所帮助。如果对你有用就给它点赞!:)