How do I connect two different GitHub accounts on macOS
Tags: github, account, tutorial
Published: 2022-11-14 13:54:07

##1. Check if you have any SSH key
 ```bash
 $ ls -al ~/.ssh
 ```
 ls
 - -a: show all file
 - -l: show file details

##2. Generate SSH key
 ```bash
 $ 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"
 ```
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
```bash
$ 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
```

##4. Copy and paste on GitHub
Copy
```bash
$ pbcopy < ~/.ssh/your-key-file-name.pub
# use the file with .pub
```
terminal
- -pbcopy < ~/path/to/file: copy to the clipboard
- -pbpaste > ~/path/to/file: create a file and paste it
```bash
# pbpaste example
$ pbcopy < ~/.ssh/my-key.pub
$ pbpaste > ~/.ssh/something.txt
# something.txt will be created and has the copied content
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgtpi5bi58cicrsb2cfs.png)
put any distinguishable title and paste the key

##5. Do 1 to 4 again for another GitHub account
<br>
<br>
<br>
##6. Create `config` file (no extension) in `~/.ssh` folder and add
```config
# 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!
```
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.
```config
# .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
```

##8. Try cloning company repo to `~/code/work` folder
```bash
# 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
```
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 cloning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvy42oypu0keg90gv5t6.png)After 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
```bash
[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.
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vxvisw6i0m42zfpab59.png)

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!