Skip to content

Instantly share code, notes, and snippets.

@stantronic
Last active June 21, 2023 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stantronic/d035392d8752f5b0b410d49cda529adc to your computer and use it in GitHub Desktop.
Save stantronic/d035392d8752f5b0b410d49cda529adc to your computer and use it in GitHub Desktop.
Set up Mac to ssh to two different Github accounts

Set up Mac to ssh to two different Github accounts

Pre-requisites

  • You have two accounts with github
  • You have git installed on your personal machine (currently comes pre-installed on a Macbook Pro)

Steps

Lets suppose you have two accounts on Github

Step 1. Create work ssh keys

On terminal:

ssh-keygen -t ed25519 -C "work@email.com" -f "id_ed25519_work"

Follow prompts for passprhase etc. (This will save private and public keys to ~/.ssh folder)

Step 2. Add work ssh key to ssh agent

On terminal:

ssh-add ~/.ssh/id_ed25519_work

Make sure this is the private key and not the file ending in .pub

Step 3. Add public key to your work account on Github

On web: github > profile avatar > settings > SSH and GPG Keys > New SSH Key Give it a descriptive name - e.g. "Personal Macbook bought 2023" Leave type as Authentication Key

On terminal:

cat ~/.ssh/id_ed25519_work.pub | pbcopy

(make sure this is the .pub file you are copying)

On web: paste into the Key field and press "Add SSH key"

Step 4. Repeat steps 1-3 for personal account

e.g.

Step 5. Create ssh config file

On terminal:

vim ~/.ssh/config

With contents:

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

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

Using github in different repos

Instead of git@github.com use git@work-github or git@personal-github depending on which account you are using

e.g.

  • Cloning:
git clone git@work-github:<organization>/<repositoryname>
  • Adding new remote
git remote add origin git@personal-github:<username>/<repositoryname>

Once remote conection is established you shouldn't need to worrry about this again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment