As developers, we often work on multiple GitHub accounts. Whether it’s for personal projects, work repositories, or collaborating on open-source projects, managing these different accounts can quickly become a headache—especially when switching between them. Thankfully, we can easily configure multiple GitHub accounts using SSH keys on both macOS and Windows. This approach will save you time and frustration by letting Git automatically authenticate using the correct SSH key, depending on the repository you’re working with.
In this tutorial, we'll walk through the steps to set up multiple GitHub accounts on both macOS and Windows using SSH keys. By the end of this guide, you'll be able to switch between accounts seamlessly while working with Git.
Why SSH Keys?
Using SSH keys for authentication on GitHub is not only more secure but also more convenient than using HTTPS. It eliminates the need to repeatedly enter your username and password when interacting with your repositories. Moreover, SSH is particularly useful when working with multiple accounts because you can assign a unique SSH key to each GitHub account.
Prerequisites
Before you begin, make sure you have the following:
A GitHub account (You can repeat this process for as many GitHub accounts as needed).
Git installed on your computer (You can install Git from here).
Access to a Terminal (macOS) or Command Prompt/PowerShell (Windows).
Step-by-Step Guide
1. Generate SSH Keys for Each Account
The first step in setting up multiple GitHub accounts with SSH is generating SSH keys for each account. This way, we’ll have separate keys for each GitHub account.
macOS/Windows
Open your Terminal (macOS) or Command Prompt/PowerShell (Windows) and run the following command to generate a new SSH key. You'll do this for each GitHub account:
ssh-keygen -t ed25519 -C "your-email@example.com"
- Replace your-email@example.com with your actual email address linked to your GitHub account.
- When prompted to save the key, choose a unique name for each key to prevent overwriting (e.g., id_ed25519_personal for your personal GitHub account and id_ed25519_work for your work GitHub account). Here's what the prompts will look like:
Enter file in which to save the key (/Users/you/.ssh/id_ed25519): ~/.ssh/id_ed25519_personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Note: If you're on Windows, the path to the keys will be something like C:\Users<YourUsername>.ssh.
2. Add SSH Keys to the SSH Agent
Once your SSH keys are generated, you need to add them to the SSH agent to manage them during the Git operations.
macOS/Windows:
Run the following commands to add each SSH key to the SSH agent:
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work
On Windows, make sure you're running PowerShell or Command Prompt as Administrator.
3. Configure Your SSH Settings
Now that the SSH keys are added to the agent, it’s time to configure SSH to know which key to use for each account. This is done by editing the ~/.ssh/config file (or the equivalent path on Windows).
macOS/Windows:
Open the SSH config file in your preferred text editor:
nano ~/.ssh/config
On Windows, the config file is typically located in C:\Users<YourUsername>.ssh\config.
Add the following entries to the config file for each account:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
This configuration tells SSH to use the correct SSH key for each GitHub account. The Host value (e.g., github-personal or github-work) can be anything meaningful and will be used when you clone or interact with repositories.
Add SSH Keys to Your GitHub Accounts
Now that your SSH keys are configured, you need to add the public keys to your GitHub accounts. Here’s how:Copy the public key for each SSH key you generated:
cat ~/.ssh/id_ed25519_personal.pub
cat ~/.ssh/id_ed25519_work.pub
Log in to GitHub for each account, go to Settings → SSH and GPG keys → New SSH Key, and paste the corresponding public key into the form.
Give each key a descriptive name (e.g., Personal key and Work key) so you can easily identify them later.
5. Clone Repositories Using the Correct SSH Key
Now that everything is set up, you can clone repositories using the correct SSH key for each account.
Personal Account
To clone a repository using your personal GitHub account, use the following command:
git clone git@github-personal:username/repo.git
Work Account
To clone a repository using your work GitHub account, use:
git clone git@github-work:username/repo.git
In both cases, replace username/repo.git with the actual repository you want to clone.
Why This Setup Helps
No more password prompts: Using SSH for authentication eliminates the need to enter your GitHub username and password repeatedly.
Seamless switching: You can easily switch between your personal and work accounts with minimal effort.
Better security: SSH keys are more secure than HTTPS, reducing the risk of unauthorized access.
Troubleshooting
If you encounter any issues, here are a few things to check:
- SSH Agent Not Running: Ensure the SSH agent is running (eval "$(ssh-agent -s)") and the keys are added (ssh-add -l).
- Key Permissions: Ensure the SSH key file permissions are correct:
chmod 600 ~/.ssh/id_ed25519_personal
chmod 600 ~/.ssh/id_ed25519_work
- Correct SSH URL: Ensure you’re using the correct SSH URL (e.g., git@github-personal or git@github-work).
Conclusion
Configuring multiple GitHub accounts with separate SSH keys allows you to streamline your workflow and reduce the complexity of managing multiple accounts. By following these steps, you can easily switch between accounts, authenticate securely, and focus on coding.
Happy coding! 💻🚀
Top comments (0)