How do I connect two different GitHub accounts on macOS

brandonwie

Brandon Wie

Posted on November 14, 2022

How do I connect two different GitHub accounts on macOS

1. Check if you have any SSH key

 $ ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

ls

  • -a: show all file
  • -l: show file details

2. Generate SSH key

 $ cd ~/.ssh # if you already have the folder
 $ mkdir ~/.ssh # if you don't have it

 $ ssh-keygen -t ed25519 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

ssh-keygen

  • -t: key type
  • -f: file name to store the key
  • -b: key bit number case ED25519: Fixed
  • -N: new passphrase
  • -C: comment

  • using passphrase is recommended and if you set one, you'll be asked when adding the keys to the SSH agent

3. Add the key to SSH agent

$ ssh-add --apple-user-keychain ~/.ssh/your-key-file-name
# easy way
$ ssh-add your-key-file-name
# use the file without .pub

# check if the keys are added successfully
$ ssh-add -l
Enter fullscreen mode Exit fullscreen mode

4. Copy and paste on GitHub

Copy

$ pbcopy < ~/.ssh/your-key-file-name.pub
# use the file with .pub
Enter fullscreen mode Exit fullscreen mode

terminal

  • -pbcopy < ~/path/to/file: copy to the clipboard
  • -pbpaste > ~/path/to/file: create a file and paste it
# pbpaste example
$ pbcopy < ~/.ssh/my-key.pub
$ pbpaste > ~/.ssh/something.txt
# something.txt will be created and has the copied content
Enter fullscreen mode Exit fullscreen mode

on the GitHub profile page, click icon(top-right) > Settings > SSH and GPG keys > New SSH key
put any recognizable title and paste the key
put any distinguishable title and paste the key

5. Do 1 to 4 again for another GitHub account

6. Create config file (no extension) in ~/.ssh folder and add

# My
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/my-ssh-file # not .pub file
# Work
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/work-ssh-file # not .pub file
# careful with the IdentityFile spelling. It's NOT identify!
Enter fullscreen mode Exit fullscreen mode

The Host is important.
When you clone the repository using SSH, the Host github.com-personal or github.com-work must replace the github.com.
For instant, git clone git@github.com:{org_name}/{repo_name}.git becomes git clone git@github.com-work:{org_name}/{repo_name}.git when you clone it.

7. Create or change ~/.gitconfig

Suppose you have ./code folder and want to use ~/code/personal folder for you and ~/code/work folder for your work.
I prefer to have all the .gitconfig* files at $HOME level, but it's up to you.
Create .gitconfig-personal and .gitconfig-work as well.

# .gitconfig
[includeIf "gitdir:~/code/personal/"]
    path = ~/.gitconfig-personal
[includeIf "gitdir:~/code/work/"]
    path = ~/.gitconfig.work

# .gitconfig-personal
[user]
    name = Your Name
    email = your-email@email.com 
    username = your-username

# .gitconfig-work
[user]
    name = Your Name # same as displayed
    email = work-email@email.com 
    username = work-username
Enter fullscreen mode Exit fullscreen mode

8. Try cloning company repo to ~/code/work folder

# suppose you created folder structure as above,
$ cd ~/code/work
# if you just copied the SSH clone command, you will have
# git@github.com:{org_name}/{repo_name}.git 

# AS MENTIONED ABOVE, YOU MUST CHANGE `github.com` to `github.com-work` 
# as written on the Host section in the ssh config file

$ git clone git@github.com-work:{org_name}/{repo_name}.git
Enter fullscreen mode Exit fullscreen mode

You will probably asked to confirm if you really want to connect using the key because the connection was never established before
the prompt you will see after start cloningAfter clone is completed, check the ~/.ssh folder,
you will now have known_hosts file.

Try with your personal repository and create the connection as well.

9. Another way

If you don't want to do anything with the ssh config file, add the lines below to at the bottom of your .gitconfig-work, .gitconfig-personal files separately

[core]
  sshCommand = ssh -i ~/.ssh/work -F ~/code/work/
# sshCommand = ssh -i path/to/your/ssh/file -F path/to/your/folder/connected/to/the/ssh/key
# `git push` and `git fetch` will use this command instead of `ssh` when they need to connect to a remote system.
Enter fullscreen mode Exit fullscreen mode

Image description

If sshCommand option is set, changing host names as #8 when cloning won't work because now the commands using ssh such as push or fetch will use the specified command of the sshCommand on .gitconfig-work or .gitconfig-personal for instance.

Hope you find it helpful.

Happy Coding!

💖 💪 🙅 🚩
brandonwie
Brandon Wie

Posted on November 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related