Skip to content

Instantly share code, notes, and snippets.

@tsur
Last active August 6, 2017 19:19
Show Gist options
  • Save tsur/bad7af800a84b795b3e5 to your computer and use it in GitHub Desktop.
Save tsur/bad7af800a84b795b3e5 to your computer and use it in GitHub Desktop.
multiple ssh keys work setup

First create publick keys and name them as home_rsa and job_rsa

$ ssh-keygen -t rsa -C "[email protected]"
$ ssh-keygen -t rsa -C "[email protected]"

Then add them to ssh process:

$ cd ~/.ssh
$ ssh-add home_rsa
$ ssh-add job_rsa
$ ssh-add -l

Go to github and register both public keys into its respectives github accounts:

# on osx
$ pbcopy < ~/.ssh/home_rsa.pub
$ echo 'Copy to your home github account'
$ pbcopy < ~/.ssh/job_rsa.pub
$ echo 'Copy to your job github account'
# on linux
$ cat ~/.ssh/home_rsa.pub | xclip -i
$ echo 'Copy to your home github account'
$ cat ~/.ssh/job_rsa.pub | xclip -i
$ echo 'Copy to your job github account'

Create a config file into `~/.ssh/' dir:

$ vim ~/.ssh/config

and paste into the config file the text below replacing <YOUR_HOME_GITHUB_USERNAME> and <YOUR_JOB_GITHUB_USERNAME> by your real username github accounts

#home account
Host github.com
    HostName github.com
    User <YOUR_HOME_GITHUB_USERNAME>
    IdentityFile ~/.ssh/home_rsa
    IdentitiesOnly yes
    PreferredAuthentications publickey

#job account
Host job.github.com
    HostName github.com
    User <YOUR_JOB_GITHUB_USERNAME>
    IdentityFile ~/.ssh/job_rsa
    IdentitiesOnly yes
    PreferredAuthentications publickey

Edit/create a ~/.gitconfig as here with your <YOUR_HOME_GITHUB_USERNAME> in the user field and your home github account email in the email field

Finally, clone your repo as follows:

# Home Account
$ git clone [email protected]:<YOUR_HOME_GITHUB_USERNAME>/<REPO_NAME>.git
# Job Account
$ git clone [email protected]:<YOUR_JOB_GITHUB_USERNAME>/<REPO_NAME>.git

Remember to update user in job repos.

$ git config user.name "<YOUR_JOB_GITHUB_USERNAME>"
$ git config user.email "<YOUR_JOB_GITHUB_PRIMARY_EMAIL>" 

if updating config locally does not work, do it globally.

git config --global --replace-all user.name "<YOUR_JOB_GITHUB_USERNAME>"
git config --global --replace-all user.email "<YOUR_JOB_GITHUB_PRIMARY_EMAIL>" 

Github uses your primary email as the commiteer

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