尝试从我的计算机上处理我实际的“工作”存储库以及 GitHub 上的存储库。
首先设置了工作帐户,一切正常。
但是,我的帐户似乎无法推送到我的存储库,该存储库是在不同的帐户/电子邮件下设置的。
我尝试将工作 key 复制到我的帐户,但这会引发错误,因为 key 只能附加到一个帐户。
如何使用各自的 GitHub 凭据向两个帐户推送/pull ?
最佳答案
您所需要做的就是使用多个 SSH key 对配置 SSH 设置。
此链接很容易理解(感谢埃里克):
生成 SSH key (Win/msysgit):
第一个链接中的相关步骤:
生成 SSH key :
ssh-keygen -t ed25519 -C "john@doe.example.com"
按照提示确定名称,例如
id_ed25519_example_company
。将 SSH 公钥从
~/.ssh/id_ed25519_doe_company.pub
复制到 GitHub,并告诉 ssh 该 key :ssh-add ~/.ssh/id_ed25519_doe_company
在
~/.ssh
中创建一个config
文件,其中包含以下内容:Host github-doe-company HostName github.com User git IdentityFile ~/.ssh/id_ed25519_doe_company
添加您的 Remote :
git remote add origin git@github-doe-company:username/repo.git
或更改使用:
git remote set-url origin git@github-doe-company:username/repo.git
此外,如果您使用不同角色使用多个存储库,则需要确保您的各个存储库相应地覆盖用户设置:
设置用户名、电子邮件和 GitHub token - 覆盖单个存储库的设置 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在评论中告诉我这一点。
关于git - 同一台电脑上有多个 GitHub 帐户吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60416980/