首页 > 解决方案 > Multiple github accounts with multiple SSH keys resulting in multiple contributors

问题描述

I have 2 different Github accounts. And I've setup 2 different SSH keys. Registered both keys on Github with the correct account and created a git config file that should use the right credentials for working with the remote repository (ie use my personal account for personal work and work account for remote work). This is what my git config file looks like:

Host personal.github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes

Host work.github.com
HostName github.com
PreferredAuthentications publickey
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes

It works, I can clone and push projects.

The problem:

Let's say i create a new remote repo in my work account. When I clone the project on my local machine I use: git clone git@work.github.com:[username]/my-plan.git

Then make some changes, commit them and push it back to the remote repo. It works, but instead of showing my work account credentials under "Contributors" it actually shows my personal account.

If I run git remote -v I get:

origin  git@work.github.com:[username]/my-plan.git (fetch)
origin  git@work.github.com:[username]/my-plan.git (push)

I dont understand why is this happening when the associated SSH key is connected to my work account and not my personal account. What I need to do to make sure that every time i push the project back, its from my work account and not personal one?

标签: gitgithubconfig

解决方案


我认为问题不在于您的 SSH 密钥,github 与“推送”提交的用户和“提交”更改的用户不同。

您可以使用浏览器导航到:

https://api.github.com/repos/[username]/[repo]/commits

每个提交都会有

  "author": {
    "name": "<author1>",
    "email": "<email1>",
    "date": "2019-09-17T12:56:43Z"
  },
  "committer": {
    "name": "<author2>",
    "email": "<email2>",
    "date": "2019-09-17T12:56:43Z"
  },

如果电子邮件不同 - 这就是问题所在,您应该在本地为当前项目配置 git ( git config user.email "<your email for this repo>")。


推荐阅读