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

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

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

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

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


当前回答

将github中的认证方式由SSh修改为HTTP。这样它就不会关心你是否登录了你的工作github帐户,你试图远程与你的个人github帐户(它有不同的凭据,而不是你目前登录到你的本地机器)交互。

其他回答

选项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

将github中的认证方式由SSh修改为HTTP。这样它就不会关心你是否登录了你的工作github帐户,你试图远程与你的个人github帐户(它有不同的凭据,而不是你目前登录到你的本地机器)交互。

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

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

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

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

您所需要做的就是使用多个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密钥是有效的。

这是我得到的错误:

Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我使用了下面的命令,这是有效的:

ssh-add ~ / . ssh / id_rsa_YOUR_COMPANY_NAME