Specify a Different SSH Key per Git Repository.

If you have more than one GitHub account, you’ll want different repositories to authenticate with different SSH keys — even though they’re all on the same host (github.com). You can do that with a Host alias in your SSH config.

Set the SSH folder permissions

1chmod 700 ~/.ssh
2chmod 644 ~/.ssh/config

Add a Host alias in ~/.ssh/config

Define your default key, then add a second entry with a friendly alias (here github-work) that points at the same host but uses a different key:

 1# Default key for github.com
 2Host github.com
 3  HostName github.com
 4  User git
 5  IdentityFile ~/.ssh/id_rsa
 6
 7# Work account key, aliased as "github-work"
 8Host github-work
 9  HostName github.com
10  User git
11  IdentityFile ~/.ssh/id_rsa_work

Point a repository at the alias

Change the repository’s remote to use the alias instead of github.com:

1git remote set-url origin git@github-work:myuser/myrepo.git

Confirm it:

1git remote -v
2# origin  git@github-work:myuser/myrepo.git (fetch)
3# origin  git@github-work:myuser/myrepo.git (push)

Now push and fetch on that repository use the right key automatically.