DEV Community

Federico Roman
Federico Roman

Posted on

Configure multiple ssh credentials with git

If you wish to configure multiple SSH keys on your computer because you use it for both work and personal projects, you need to set up different users in your repository.

Assuming that the first key is referred to as a “personal_key” and the second is referred to as a “work_key," both of which have their own public files extensions, you can edit the config file (~/.ssh/config) to include the following details.

Host work-github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/work_key
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/personal_key
Enter fullscreen mode Exit fullscreen mode

Once saved, go to the working folder where you are running this project from (for example home/dashpy/work) and use command git remote -v. This should bring up something similar to:

$ git remote -v
origin  git@github.com:dashpy/insight.git (fetch)
origin  git@github.com:dashpy/insight.git (push)
Enter fullscreen mode Exit fullscreen mode

We want to change that and tell git that when I'm working in this work project use my work ssh key, so to do that we type

git remote set-url origin git@work-github.com:Company/product.git

Enter fullscreen mode Exit fullscreen mode

Which will tell Git that it should use your work SSH key when this particular working project is used.

The end.

Oldest comments (0)