Search code examples
gitgithubversion-controlgithub-enterprise

How to configure multiple github accounts on your computer?


So, I have my work computer and that is connected to my GitHub Enterprise Account (github.company.com) on the terminal. Now I want to set up my personal account (github.com) on here too.

I've been following this tutorial - https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574 and on step 3 when I have to create my config file should my HostName be github.company.com or github.com? Can I have any (meaningful) name for Host? Also, what does User here mean?

Also, how do I switch between both these accounts on the terminal - i.e. my personal and my enterprise account? There are certain things I need to commit from my personal account, and use the enterprise account with the rest.


Solution

  • Detailed steps to use two GitHub accounts on the same PC as below:

    1. Create SSH key for the github.company.com account

    If you already added the SSH key to your github.company.com account, then skip this step.

    First, use ssh-keygen to create id_rsa and id_rsa.pub in C:\Users\username\.ssh.

    Then, add the content of id_rsa.pub file as a SSH key in github.company.com account.

    2. Create SSH Key for your personal account github.com

    Use ssh-keygen -t rsa -C "email address for the personal github account", and save the key in /c/Users/username/.ssh/id_rsa_personal.

    Now add the content of id_rsa_personal.pub file as a SSH Key in your personal GitHub account.

    3. Config the two SSH Keys

    In C:\Users\username\.ssh diectory, create a config file with below content:

    Host github.company.com
      HostName github.company.com
      User git
      IdentityFile ~/.ssh/id_rsa
    Host github-personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_personal
    

    4. Work with the two GitHub accounts by SSH protocol

    You can clone your github.company.com account by:

    git clone git@github.company.com:companyaccountname/reponame
    

    And then add the personal account as a remote in the local repo by:

    git remote add personal git@github-personal:personalaccountname/reponame
    

    To get the file from personal account to company repo by below commands:

    git merge upstream master --allow-unrelated-histories
    # make changes
    git add .
    git commit -m 'add the changes from peronal repo'
    git push origin master
    

    To commit/push changes from local company repo by the committer of the personal repo, you just need to re-config the username and email before committing changes in the local repo:

    git config user.name <personal github account username>
    git config user.email <email for login the personal github account>
    

    When you want to commit changes with the company account, just re-config username and email again before committing changes.