Search code examples
gitsshatlassian-sourcetree

How to configure SSH keys on Windows for SourceTree, terminal and IDEs such as IntelliJ and Eclipse?


How does one configure SSH keys to work with SourceTree, the terminal and IDEs like IntelliJ and Eclipse on Windows?

This was driving me nuts for a while and I eventually came up with the following after a great deal of research and trial and error. Thought I'd share here for future reference for myself and others.


Solution

  • All files are saved in C:\Users\windows-username\.ssh (default .ssh location)

    Create SSH keys using PuTTYgen (note: this is a separate utility to PuTTY)

    Select key type from bottom (e.g. RSA, ED25519). Click "generate" and follow on screen instructions to move mouse (makes key random).

    You will now have a public and private key generated. These now need to be saved.

    Save private key

    Save private key (will save as .ppk file). This is so you can easily reload the key in the future and for use with SourceTree or any other utility that uses pageant.

    Export Open SSH key

    Also use Conversions -> Export OpenSSH key to produce a private key for use from terminal or an IDE.

    Create config file in .ssh folder

    Create a config file (file named config with no extension) in the .ssh folder that looks like this:

    Host github.com
     Hostname github.com
     User github-user-1
     IdentityFile ~/.ssh/github-user-1-private-key
    
    Host github.com
     Hostname github.com
     User github-user-2
     IdentityFile ~/.ssh/github-user-2-private-key
    
    Host gitlab.com
     Hostname gitlab.com
     User gitlab-user
     IdentityFile ~/.ssh/gitlab-user-private-key
    

    The example above shows how you can have 2 GitHub users and 1 GitLab user at the same time.

    Add Public keys to relevant accounts

    You will need to add the public keys (copy & paste from PuTTYgen window) to your online accounts. Look for documentation on how to do this online.

    Terminal and IDE Use

    You should now be able to use Git from the terminal or with IDE integration. It will ask for the password for your private key.

    SourceTree Use

    To use in SourceTree, you will need to load up Pageant (loads in System Tray - small computer wearing a hat). Right click -> Add key or open, add key. Load the .ppk file and enter your private key password.