Multiple GitHub Accounts on One Machine

Working with multiple GitHub accounts on the same computer can seem tricky, but it’s actually quite manageable with the right approach. Here’s a simplified guide to help you set it up:

How the connection happens to the Git host

GitHub uses …


This content originally appeared on DEV Community and was authored by Varun Gujarathi

Working with multiple GitHub accounts on the same computer can seem tricky, but it's actually quite manageable with the right approach. Here's a simplified guide to help you set it up:

How the connection happens to the Git host

GitHub uses SSH keys to verify your identity. When you try to connect to GitHub, your computer offers an SSH key. If GitHub recognizes it, you're in! The problem arises when you have multiple accounts with different keys – GitHub can get confused about which key belongs to which account.

The Solution: SSH Config File

The magic lies in a file called config within your .ssh directory. This file lets you create different profiles for each of your GitHub accounts. Think of it like giving each account a unique nickname that your computer and GitHub both understand.

Steps

  1. Generate SSH Keys: For each GitHub account, generate a unique SSH key pair. Use the ssh-keygen command in your terminal. The important note here is that each file name should be different. Refer Github Docs.
    For example:

    ssh-keygen -t ed25519 -C "your_work_email@example.com" -f "id_ed25519_work" 
    ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f "id_ed25519_personal"
    
  2. Add SSH Keys to GitHub: Add the public key of each key pair to the corresponding GitHub account settings.
    Refer Github Docs

  3. Create/Edit SSH Config File: Open (or create if it doesn't exist) the config file located in your .ssh directory (usually ~/.ssh/config).
    Add a separate block for each account, like this:

    # Work GitHub
    Host work_github
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_work
    
    # Personal GitHub
    Host personal_github   
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_personal
    
  4. Clone Repositories with Specific Accounts: When cloning a repository, use the Host you defined in your config file:

    git clone git@work_github:your-organization/work-repository.git
    git clone git@personal_github:your-username/personal-repository.git
    

Handling Different GitHub Domains

If you're using both github.com and an enterprise GitHub (e.g., github.yourcompany.com), the process is similar. The main difference is in the HostName entry within your config file.

For example:

# Enterprise Account
Host github.yourcompany.com
  HostName github.yourcompany.com
  User git
  IdentityFile ~/.ssh/id_ed25519_enterprise

Important Notes:

  • Security: Keep your private keys safe! Treat them like passwords.
  • Git Configuration: You might need to configure user.name and user.email for each repository to ensure your commits are attributed to the correct account. By following these steps, you can seamlessly manage multiple GitHub accounts on your machine without any conflicts. Happy coding!

P.S.: This tutorial is for other Git providers like GitLab, Bitbucket, etc as well, with minor changes in steps.


This content originally appeared on DEV Community and was authored by Varun Gujarathi


Print Share Comment Cite Upload Translate Updates
APA

Varun Gujarathi | Sciencx (2024-11-01T23:39:03+00:00) Multiple GitHub Accounts on One Machine. Retrieved from https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/

MLA
" » Multiple GitHub Accounts on One Machine." Varun Gujarathi | Sciencx - Friday November 1, 2024, https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/
HARVARD
Varun Gujarathi | Sciencx Friday November 1, 2024 » Multiple GitHub Accounts on One Machine., viewed ,<https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/>
VANCOUVER
Varun Gujarathi | Sciencx - » Multiple GitHub Accounts on One Machine. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/
CHICAGO
" » Multiple GitHub Accounts on One Machine." Varun Gujarathi | Sciencx - Accessed . https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/
IEEE
" » Multiple GitHub Accounts on One Machine." Varun Gujarathi | Sciencx [Online]. Available: https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/. [Accessed: ]
rf:citation
» Multiple GitHub Accounts on One Machine | Varun Gujarathi | Sciencx | https://www.scien.cx/2024/11/01/multiple-github-accounts-on-one-machine/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.