Skip to main content

You can have different ssh keys for different git projects by configuring hosts in the .ssh/config file

1 min read

So you have one project on github and another on bitbucket and you want different SSH keys for each project.

Github's docs say to add this to your .ssh/config file

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

But, if you add a new record to this file with a HostName set

Host bitbucket.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_bitbucket_rsa

you can tell SSH to use this ssh identity for bitbucket.com and fall back to the default otherwise.

You could even be more specific and do

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_github_rsa

Host bitbucket.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_bitbucket_rsa

You can even alias these using the host name if you're using github for both personal and work

Host github_personal
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_github_personal_rsa

Host github_work
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_github_work_rsa

so now instead of your remote origin being

remote.origin.url=git@github.com:madole/very-important-project.git

its either

Personal

remote.origin.url=git@github_personal/very-important-project.git

or

Work

remote.origin.url=git@github_work/very-important-project.git

© 2021 by Madole.
GitHub Repository
Last build: 1729596316231