How to Use a Custom SSH Identity with Git

By Andre Perunicic | August 24, 2017
Follow @prncc

This page describes how to specify which SSH profile to use with git. This can be handy if you have more than one GitHub account and don’t wish to reuse your default SSH key or type in a password every time you push or pull. The commands will be written from the perspective of a GitHub user, but should be easy to adapt to other scenarios as well.

Step-by-Step Instructions

  1. Generate a new SSH key:

    ssh-keygen -t rsa -b 4096 -C "name@domain.com"
    

    Let’s assume the identity file you created is in ~/.ssh/id_rsa_custom.

  2. Upload the key to GitHub (link leads to GitHub’s instructions).

  3. SSH will look for profiles in the user’s ~/.ssh/config file. Add something similar to this to that file:

    Host custom
        Hostname        github.com
        IdentityFile    ~/.ssh/id_rsa_custom
        IdentitiesOnly yes
    

    The IdentitiesOnly yes option is required to prevent the SSH sending the identity file matching the default filename, as is the default. If you have a file named ~/.ssh/id_rsa that will get tried BEFORE your ~/.ssh/id_rsa_custom without this option.

  4. Associate the remote git repository to your local repo:

    git remote add origin git@custom:user/myrepo.git
    

    If starting from scratch, you can just do:

    git clone git@custom:user/myrepo.git
    

    From then on, commands like git push -v origin master should work normally without any prompts.

If you got to this page from Google, I hope you found the answer you were looking for. Get in touch with us if you have a data sourcing or analysis task you want done quickly and reliably.