I find myself doing this everytime I change a new Macbook. Here's how we can use our account simultaneously based on the project we're working on. Example I use here is using work and personal github account.
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they existIf there's none, then generate a new SSH key
First let's create for work account and type this to create SSH key pair for user authentication:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"It will asked where to save the file, press enter because by default SSH keys for user authentication are usually stored in the user's .ssh directory under the home directory. Then, I usually also press enter for the passphrase.
After that, It will create public and private key under ~/.ssh/ directory.
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]Now, we will generate another SSH key for our personal account and do the same as above:
$ ssh-keygen -t rsa -C "email@personal.com" -f "id_rsa_personal"By this time, we have 2 keys generated under ~/.ssh/ directory, which is id_rsa.pub and id_rsa_personal.pub.
Start agent in background
$ eval "$(ssh-agent -s)"
> Agent pid 59566check to see if your ~/.ssh/config file exists in the default location.
$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.If the file doesn't exist, create the file.
$ touch ~/.ssh/configOpen your ~/.ssh/config file, then modify the file, replacing ~/.ssh/id_rsa if you are not using the default location and name for your id_rsa key.
# Work account
Host git.company.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
# Personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personalCopy the SSH key to clipboard
$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboardDo the same for your personal GitHub account. This time, copy ~/.ssh/id_rsa_personal.pub key and add it to your personal GitHub acount.
This is what I do whenever I setup a new project. The key is to set the user name and email used in the project based on the config file.
$ cd myProject
$ git init
# setup GitHub username in myProject
$ git config user.name "gitusername"
# setup GitHub email in myProject
$ git config user.email "email@personal.com"
# add all the file changes
$ git add .
# commit the changes
$ git commit -m "initial commit"
$ git branch -M main
# make sure the host name is correct based on the config file. example here is @github.com-personal
$ git remote add origin git@github.com-personal:gitusername/my-project.git
$ git push -u origin main$ git clone git@github.com-personal:gitusername/other-project.gitThat's it! By using the right host name based on config file, it will take care of the SSH key being used at each of projects that we're working on.
More info about Connecting to Github with SSH.