Using Git with SSH in Windows 10

February 21st 2020 Git Windows SSH

Although Git can be used over HTTPS with username and password authentication, it's much more convenient to use over SSH. Even with Git Credential Manager for Windows being bundled with Git for Windows.

Now that OpenSSH client is included in Windows 10, SSH can be easily set up without any third-party clients:

  • First, generate your SSH key. Although you can transfer key files between computers, I suggest generating a new one on each computer you use.

    cd ~\.ssh\
    ssh-keygen
    
  • Then, associate the generated key with your Windows login by adding it to the OpenSSH Authentication Agent service.

    ssh-add ~\.ssh\id_rsa
    

    In my case, the service was disabled and the command failed with the following error:

    Error connecting to agent: No such file or directory

    The documentation suggested trying to start the service:

    Start-Service ssh-agent
    

    For me, it just failed with a different error:

    Start-Service : Service 'OpenSSH Authentication Agent (ssh-agent)' cannot be started due to the following error: Cannot start service ssh-agent on computer ' .'.

    To resolve the issue, I had to change the service startup type from Disabled to Automatic in its properties dialog (and start the service then).

    OpenSSH Authentication Agent properties dialog

With that, the command-line Git client is ready to be used with SSH.

UI clients will typically require additional configuration which is application dependant. In my favorite UI client Fork, this can be done in the File > Configure SSH Keys dialog.

Configure SSH Keys dialog in Fork

Of course, the contents of the generated SSH public key file (with .pub extension, e.g. id_rsa.pub) must be added to your account for the Git service provider. Add the same key to all services, if you use multiple.

The location of the relevant settings differs between the Git services:

  • In GitHub, it's in the SSH and GPG keys section of Personal settings.
  • In GitLab, it's in the SSH Keys section of User Settings.
  • In Bitbucket, it's in the SSH keys section of your Bitbucket settings. URL is different for different users.
  • In Azure DevOps, it's in the SSH public keys section of User settings. URL is different for different users.

Make sure that you use the SSH URL instead of the HTTPS one when cloning new repositories (it's the one not starting with https).

For existing repositories, you can change the remote URL to the SSH one, e.g.:

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License