Skip to content

Instantly share code, notes, and snippets.

@Chubby-Chocobo
Last active August 31, 2021 06:56
Show Gist options
  • Save Chubby-Chocobo/54e6a21edb7223bba99deae2510dc237 to your computer and use it in GitHub Desktop.
Save Chubby-Chocobo/54e6a21edb7223bba99deae2510dc237 to your computer and use it in GitHub Desktop.
Setup SSH keys for multiple github accounts and repos

Setup SSH keys for multiple github accounts and repos

Prepare SSH keys:

See guideline to create a new SSH key

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

Choose/rename the keys, for example like these

  ~/.ssh/id_rsa_xxx
  ~/.ssh/id_rsa_yyy

Clear cached SSH keys first

  $ ssh-add -D

Add new keys

  $ ssh-add ~/.ssh/id_rsa_xxx
  $ ssh-add ~/.ssh/id_rsa_yyy

Recheck the keys list

  $ ssh-add -l

Add SSH config

  $ cd ~/.ssh/
  $ touch config
  $ vi config

Then added

  # Identity for xxx repos here
  Host github-xxx
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_xxx
    IdentitiesOnly yes

  # Identity for yyy repos here
  Host github-yyy
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_yyy
    IdentitiesOnly yes

Update git config

Clone your repo by alternative url

  $ git clone git@github-xxx:xxx/xxx.git xxx
  $ git clone git@github-xxx:yyy/yyy.git yyy

Or modify config for existing working directory

  $ cd xxx
  $ git remote set-url origin git@github-xxx:xxx/xxx.git

  $ cd yyy
  $ git remote set-url origin git@github-yyy:yyy/yyy.git

Update user name and email to for commit/push

  $ cd xxx
  $ git config user.name "xxx"
  $ git config user.email "[email protected]" 
  
  $ cd yyy
  $ git config user.name "yyy"
  $ git config user.email "[email protected]" 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment