How to Use Multiple GitHub Accounts on MacOS
Introduction
GitHub is a powerful platform for collaboration and version control, widely used by developers worldwide. However, managing multiple GitHub accounts on the same machine can be tricky. In this guide, we'll walk you through the steps to set up and use two GitHub accounts on your Mac, allowing you to seamlessly switch between personal and work projects.
Step 1: Generate Separate SSH Keys
SSH keys are used for secure authentication when interacting with GitHub repositories. To use multiple GitHub accounts, you'll need to generate separate SSH keys for each account.
Open Terminal on your Mac.
Run the following command to generate an SSH key for the first account, replacing "your_email@example.com" with your actual email address:
ssh-keygen -t ed25519 -C "your_email@example.com" #For legacy systems use ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
When prompted, save the key with a unique filename, such as "id_rsa_your_name".
Repeat the process for the second account, using a different filename like "id_rsa_company_name".
Step 2: Configure SSH Config File
Next, you'll need to configure your SSH config file to use the correct SSH key for each GitHub account.
Open or create the SSH config file by running:
nano ~/.ssh/config
Add the following configuration for each GitHub account:
# Personal GitHub account Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_john # Work GitHub account Host github.com-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_google
Step 3: Add Public Keys to GitHub Accounts
Now, you'll need to add the public keys to the corresponding GitHub accounts.
Log in to your personal GitHub account and go to the "SSH and GPG keys" section of your Settings page.
Click "New SSH key" and paste the contents of "~/.ssh/id_rsa_john.pub".
pbcopy < ~/.ssh/id_rsa_john.pub
Repeat this process for your work GitHub account, using the "id_rsa_google.pub" key.
Step 4: Test Connections
To ensure everything is set up correctly, test the connections for both accounts.
Run the following commands in Terminal:
ssh -T git@github.com-personal ssh -T git@github.com-work
Step 5: Working with Repositories
When cloning or working with repositories, use the appropriate hostname:
git clone git@github.com-personal:your-username/repo.git
git clone git@github.com-work:your-org-reponame/repo.git
Step 6: Set Git User Configuration
Lastly, set the Git user.email
and user.name
for each repository:
git config user.email "your_personal_email@example.com"
git config user.name "Your Personal Name"
git config user.email "your_work_email@example.com"
git config user.name "Your Work Name"
Conclusion
By following these steps, you can seamlessly use two GitHub accounts on your Mac, switching between them as needed for your personal and work projects. Properly configured SSH keys and Git configurations ensure smooth interactions with GitHub repositories, enhancing your productivity and organization.
Thank you for joining me on this technical journey. Your support and interest mean the world to me. I look forward to sharing more insightful content with you in the future. Until then, stay curious and keep exploring!