How to manage multiple GitHub accounts on a single machine with SSH keys on Mac?

How to manage multiple GitHub accounts on a single machine with SSH keys on Mac?

Table of contents

No heading

No headings in the article.

Managing multiple GitHub accounts on a single machine can be challenging, especially when you need to switch between personal and work-related projects frequently. However, by using SSH keys and configuring your machine's SSH client, you can easily switch between your different GitHub accounts without any hassle.

Here are the steps you can follow to manage multiple GitHub accounts on a single machine with SSH keys:

Step 1: Open the terminal and navigate to the SSH directory by typing the following command: cd ~/.ssh

Step 2: Create SSH keys for your work and personal GitHub accounts using the following commands:

For work account: ssh-keygen -t ed25519 -C "<your work-email>" -f "github-work"

For personal account: ssh-keygen -t ed25519 -C "<your personal-email>" -f "github-personal"

Step 3: Set a passphrase for each SSH key when prompted.

Step 4: Add the SSH keys to the SSH agent by typing the following commands:

For work account: ssh-add ~/.ssh/github-work

For personal account: ssh-add ~/.ssh/github-personal

Step 5: Copy the public SSH key for each account by typing the following commands:

For work account: pbcopy < ~/.ssh/github-work.pub

For personal account: pbcopy < ~/.ssh/github-personal.pub

Step 6: Add the copied SSH public keys to your GitHub accounts by navigating to Settings -> SSH and GPG keys -> New SSH Key and paste the public key.

Step 7: Open the SSH configuration file by typing the following command: open -e ~/.ssh/config

Add the following lines to the configuration file:

#work account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-work

#personal account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-personal

Step 8: You can configure one of your accounts as the main or global account by typing the following commands:

git config --global user.name "<your name>"
git config --global user.email "<your work-email>"

Step 9: Clone the repository using the following commands:

For normal clone: git clone git@github.com:username/repo.git

For specific account clone:

  • Work account: git clone git@github.com-work:username/repo.git

  • Personal account: git clone git@github.com-personal:username/repo.git

Step 10: To check which GitHub account you are currently using, type the following command:

git config user.name or git config user.email

To set a particular GitHub account for the project (inside the project folder), type the following commands:

For personal account:

git config user.name "<your name>"
git config user.email "<your personal-email>"

Following these steps, you can easily manage multiple GitHub accounts on a single machine with SSH keys. Now you can switch between your personal and work-related projects without any confusion.