How to add multiple GitHub accounts in Windows and Mac in 2024

If you are facing an issue with adding your private and official GitHub account on the same computer you are in the right place. In this article, we will see how you can add multiple GitHub accounts in Windows or Mac and how can you manage multiple github accounts.

Setting the Stage: Preparing Your System

Before adding multiple accounts, ensure your system is prepared:

  1. Update Your System: For both MacBook and Windows, ensure your operating system is up-to-date. This ensures compatibility and security.
  2. Install Git: If haven’t installed it then install Git. It’s the backbone for interacting with GitHub repositories.
  3. Basic Understanding of Terminal (MacBook) / Command Prompt (Windows): Familiarize yourself with basic command-line operations as most interactions with Git are through these interfaces.

Adding Multiple GitHub Accounts on MacBook

1) Generate SSH Keys: Open Terminal. Use ssh-keygen to generate a new SSH key for each GitHub account. Label each key clearly to avoid confusion.

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    2) Add SSH Keys to the SSH Agent: Ensure the ssh-agent is running and add your new SSH key to it. This step is crucial for managing multiple keys.

    eval "$(ssh-agent -s)"
    ssh-add -K ~/.ssh/your_private_key

    3) Add SSH Keys to GitHub Accounts: For each GitHub account, go to Settings > SSH and GPG keys > New SSH key. Add your public key here.

    4) Configure Git with Multiple Identities: Use .gitconfig to set up configurations for each GitHub account.

    [user]
    name = your_username
    email = your_email@example.com

    5) Use SSH Config to Manage Multiple Keys: Create a config file in your ~/.ssh directory. Define each Host for your GitHub accounts with specific Identity

    File and HostName.

    Host github.com-user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_user1
    
    Host github.com-user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_user2

    Clone Repositories Using the Correct URLs: When cloning a repository, use the specific Host you defined in the SSH config.

    Adding Multiple GitHub Accounts on Windows

    1) Generate SSH Keys Using Git Bash: Open Git Bash and repeat the SSH key generation process for each account.

    2) Add SSH Keys to the SSH Agent: Similar to MacBook, add the generated SSH keys to the ssh-agent.

    eval $(ssh-agent -s)
    ssh-add ~/.ssh/your_private_key

    3) Add SSH Keys to GitHub Accounts: This step is the same as in MacBook; add your public SSH keys to your GitHub accounts.

    4) Configure Git for Multiple Identities: Use Git Bash to configure user information for each repository.

    5) Managing SSH Keys with SSH Config: Create a config file in your ~/.ssh directory, similar to MacBook, and configure each Host for your GitHub accounts.

    6) Cloning Repositories with Specific Accounts: Use the Host defined in SSH config to clone repositories.

    Best Practices and Tips

    1. Security: Regularly update and manage your SSH keys. Use strong and diffrent for each key.
    2. Organization: Keep your work organized. Use clear naming conventions for SSH keys and GitHub repositories.
    3. Backup: Always back up your SSH keys in a secure location.
    4. Troubleshooting: If you encounter issues, check your SSH and Git configurations, and ensure you’re using the correct URLs for cloning.
    5. Learning and Documentation: GitHub and Git offer extensive documentation. Refer to these resources for specific queries.

    Conclusion: By following the above steps you can easily work with multiple github accounts in your computer.

    Frequently Asked Questions

    1. Can I use the same SSH key for two or multiple GitHub accounts? No, it’s recommended to use different SSH keys for different accounts for security and management purposes.
    2. How do I switch between different GitHub accounts on the same machine? Configure your Git settings for each project or use the SSH config file to manage different identities.
    3. What should I do if my SSH key is compromised? Immediately remove the compromised key from your GitHub accounts, generate a new key, and replace it in your SSH agent and GitHub settings.