同一台计算机上有多个 GitHub 帐户? [英] Multiple GitHub accounts on the same computer?

查看:30
本文介绍了同一台计算机上有多个 GitHub 帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试同时处理我的实际工作"和工作".存储库,以及我在 git hub 上的存储库,来自我的计算机.

Trying to work on both my actual "work" repos, and my repos on git hub, from my computer.

首先设置了工作帐户,一切正常.

The work account was set up first, and everything works flawlessly.

但是,我的帐户似乎无法推送到我在不同帐户/电子邮件下设置的存储库.

My account, however, cannot seem to push to my repo, which is set up under a different account/email.

我已尝试将我的工作密钥复制到我的帐户,但这会引发错误,因为当然一个密钥只能附加到一个帐户.

I've tried copying my work key up to my account, but that throws an error because of course a key can be only attached to one account.

如何从两个帐户各自的 GitHub 凭据推送/拉取?

How can I push/pull to and from both accounts from their respective GitHub credentials?

推荐答案

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

All you need to do is configure your SSH setup with multiple SSH keypairs.

生成 SSH 密钥 (Win/msysgit)https://help.github.com/articles/generating-an-ssh-键/

Generating SSH keys (Win/msysgit) https://help.github.com/articles/generating-an-ssh-key/

第一个链接的相关步骤:

  1. 生成 SSH 密钥 ssh-keygen -t rsa -C "john@doe.com",按照提示操作并决定一个名称,例如id_rsa_doe_company.
  2. 将 SSH 公钥从 ~/.ssh/id_rsa_doe_company.pub 复制到 GitHub 并将密钥告诉 ssh:ssh-add ~/.ssh/id_rsa_doe_company.
  3. ~/.ssh中创建一个config文件,内容如下:
  1. Generate an SSH-key ssh-keygen -t rsa -C "john@doe.com", follow the prompts and decide a name, e.g. id_rsa_doe_company.
  2. Copy the SSH public-key to GitHub from ~/.ssh/id_rsa_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_rsa_doe_company.
  3. Create a config file in ~/.ssh with the following contents:
Host github-doe-company
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_doe_company

  • 添加您的远程 git remote add origin git@github-doe-company:username/repo.git 或使用 git remote set-url origin git@github-doe-company 更改:用户名/repo.git

  • 此外,如果您使用不同的角色处理多个存储库,则需要确保您的各个存储库相应地覆盖了用户设置:


    Also, if you're working with multiple repositories using different personas, you need to make sure that your individual repositories have the user settings overridden accordingly:

    设置用户名、电子邮件和 GitHub 令牌 – 覆盖单个存储库的设置https://help.github.com/articles/setting-your-commit-email-address-in-git/

    Setting user name, email and GitHub token – Overriding settings for individual repos https://help.github.com/articles/setting-your-commit-email-address-in-git/

    希望这会有所帮助.

    注意:有些人可能需要将不同的电子邮件用于不同的存储库,从 git 2.13 开始,您可以通过编辑位于以下位置的全局配置文件来基于目录设置电子邮件:~/.gitconfig 使用这样的条件:

    Note: Some of you may require different emails to be used for different repositories, from git 2.13 you can set the email on a directory basis by editing the global config file found at: ~/.gitconfig using conditionals like so:

    [user]
        name = Pavan Kataria
        email = defaultemail@gmail.com
    
    [includeIf "gitdir:~/work/"]
        path = ~/work/.gitconfig
    

    然后你的工作特定配置 ~/work/.gitconfig 看起来像这样:

    And then your work specific config ~/work/.gitconfig would look like this:

    [user]
        email = pavan.kataria@company.tld
    

    感谢@alexg 在评论中告知我这一点.

    Thank you @alexg for informing me of this in the comments.

    这篇关于同一台计算机上有多个 GitHub 帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆