Multiple GitHub Accounts on the Same Mac

Multiple GitHub Accounts on the Same Mac

managing multiple GitHub accounts on a single Mac can be challenging, but with SSH keys and some configuration, it becomes much easier. Here's a step-by-step guide to setting up and using multiple GitHub accounts on your Mac.


Step 1: Navigate to the .ssh Directory

First, you need to navigate to the .ssh directory on your Mac, which is where SSH-related files are stored. To do this, open a terminal window and enter the following command:

cd ~/.ssh        

if the .ssh directory does not exist, you can create it by running:

mkdir ~/.ssh        

Step 2: Create SSH Keys

Next, you'll create an SSH key pair for each GitHub account you need. SSH keys are used for secure authentication when connecting to remote servers like GitHub. Use the following commands to create the SSH keys:

ssh-keygen -t rsa -C "email@personal.com" -f "github-personal"

ssh-keygen -t rsa -C "email@company.com" -f "github-company"        

The ssh-keygen command generates an RSA SSH key pair with the specified encryption type (-t rsa) and comment (-C). The -f option sets the file name for the private and public SSH keys. You'll be prompted to enter a passphrase, which adds an extra layer of security to your private key. Make sure to remember this passphrase, as you'll need it later. or let it empty by pressing enter.

  • -t: This option specifies the type of key algorithm to use for generating the SSH key pair. which is rsa.
  • -C: This option allows you to add a comment or label to the key. The comment is typically an email address or any other relevant identifier, which helps distinguish the key pair's purpose or owner. Using meaningful comments can be beneficial when managing multiple SSH keys for different accounts or purposes.
  • -f: This option specifies the filename for the generated SSH key pair. The private key will be saved with the filename provided, and the corresponding public key will be saved with the same filename but with the ".pub" extension. Both files will be stored in the user's home directory under the ".ssh" directory.

After running the ssh-keygen command with the appropriate options and providing any necessary inputs, you will have two files generated:

  1. <filename> (private key): This file contains the private key and must be kept secure. It is used to authenticate yourself when connecting to remote systems or services.
  2. <filename>.pub (public key): This file contains the public key and can be safely shared with remote servers or services. The public key is added to the "authorized_keys" file on the remote server, allowing you to authenticate using the corresponding private key.

In our case it should be four files github-personal, github-personal.pub, github-company and github-company.pub


Step 3: Add SSH Keys to the Agent

To manage your SSH keys and passphrases conveniently, you can use the SSH agent. Start the SSH agent by running the following command:

eval "$(ssh-agent -s)"        

Then, add your private SSH keys to the agent and store your passphrase in the macOS keychain using the following commands:

ssh-add --apple-use-keychain ~/.ssh/github-personal

ssh-add --apple-use-keychain ~/.ssh/github-company        

Step 4: Add SSH Public Keys to GitHub Accounts

SSH keys work in pairs - a private key (usually named id_rsa) and a corresponding public key (usually named id_rsa.pub). Now, you'll add the public SSH keys to your GitHub accounts to enable secure authentication.

Copy the content of each public key to the clipboard using the following commands:

pbcopy < ~/.ssh/github-personal.pub

pbcopy < ~/.ssh/github-company.pub        

Next, go to your GitHub account settings, navigate to "SSH and GPG keys," click "New SSH Key," add a title , paste the public key content into the "Key" field, and save. Repeat this step for all your GitHub accounts.


Step 5: Configure SSH Alias

To easily switch between different GitHub accounts or organizations, you can set up aliases for each account in the SSH configuration file (~/.ssh/config).

open ~/.ssh/config        

If the file does not exist, you can create it using the following command:

touch ~/.ssh/config        

then open it and add aliases.

Host github.com-personal

   HostName github.com

   User git

   IdentityFile ~/.ssh/github-personal

Host github.com-company

   HostName github.com

   User git

   IdentityFile ~/.ssh/github-company        

In this configuration, "github.com-personal" and "github.com-company" are custom hostname aliases that represent your personal and company GitHub accounts, respectively. The "HostName" specifies the actual GitHub server hostname, "User" defines the remote user ("git" in this case), and "IdentityFile" points to the private SSH key for each account.


Step 6: Clone and Configure Repositories

Now that your SSH keys and aliases are set up, you can use them to interact with your GitHub repositories.

git clone git@github.com-personal:github-personal/{repo-name}.git        

To clone a repository from your personal account, use the following command:

git clone git@github.com-company:github-company/{repo-name}.git        

Once you have cloned a repository, check the current user configuration using the following command:

git config --list        

If your email and name are not set, use the following commands to set them:

git config user.email "email@personal.com"

git config user.name "personal name"        

Do the same for your company email.


Step 7: Change Remote URL for Cloned Repositories

If you have already cloned repositories using HTTPS, you can change the remote URL to use SSH for a smoother experience. First, navigate to the repository directory, then run the following commands to update the remote URL:

git remote -v # To check current URLs
git remote remove origin # Remove existed URLs
git remote add origin git@github.com-personal:github-personal/{repo-name}.git # Add SSH URL        

Congratulations! You have now successfully set up and can work with multiple GitHub accounts on your Mac. With these steps, you can seamlessly switch between different GitHub accounts and organizations without any conflicts. Happy coding!

By Hesham Nawar







Thanks for sharing, it is very helpful

This is very useful we can check the status of the connection of the ssh after configuration by using the below commands , ssh -T github-personal or sss -T <named used in the config >

To view or add a comment, sign in

Others also viewed

Explore content categories