这听起来与我目前的工作设置非常相似。看来你已经设置了单独的ssh-keys
所以你还需要创建一个~/.ssh/config
文件并使用类似于以下内容的信息填充它:
Host work.github.com
HostName github.com
User WORK_GITHUB_USERNAME
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_work_rsa
IdentitiesOnly yes
Host personal.github.com
HostName github.com
User PERSONAL_GITHUB_USERNAME
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_personal_rsa
IdentitiesOnly yes
每个属性听起来都很不言自明,但IdentitiesOnly
一。我不会尝试解释它的用途,但这在我当前的设置中并且工作正常。
还值得注意的是Host URL
只是获取正确用户设置的指针,对于将文件正确发送到目标没有任何影响HostName
url.
现在您只需要确保您的origin
(或任何remote
一般来说)url 匹配正确Host
根据您的用户名,在您各自的存储库中找到 url。如果您已有个人存储库,则可以编辑该存储库的.git/config
在文本编辑器中创建文件:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected] /cdn-cgi/l/email-protection:PERSONAL_GITHUB_USERNAME/project.git
或通过命令行执行此操作:
git remote set-url origin [email protected] /cdn-cgi/l/email-protection:PERSONAL_GITHUB_USERNAME/project.git
与您的工作一类似:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected] /cdn-cgi/l/email-protection:your_work_organization/project.git
或者再次通过命令行:
git remote set-url origin [email protected] /cdn-cgi/l/email-protection:your_work_organization/project.git
当然,您可以随时设置您的其中一项Host
网址在你的~/.ssh/config
文件原样
Host github.com
我只用过work.github.com
更容易查看配置关系。
一旦这些都设置完毕,您应该能够推送到每个相应的遥控器。
EDIT
我刚刚发现的一件事是,如果你曾经为你的全局 git 配置值设置过user.email
值(我猜user.name
也会发送不同的值),git 将显示您作为该电子邮件用户的提交。为了解决这个问题,您可以覆盖本地存储库中的全局 git 配置设置:
$ git config user.name "John Doe"
$ git config user.email [email protected] /cdn-cgi/l/email-protection
现在应该以该存储库的正确用户身份发送提交。