Using Git with SSH on Windows

Once this is set up, you should be able to use SSH authentication with your GitHub repos with a Command Line Interface on Windows.
Once this is set up, you should be able to use SSH authentication with your GitHub repos with a Command Line Interface on Windows.

Setting up GitHub with SSH authentication on Windows can be tedious. But I have to do this whenever I set up a new Windows PC, and I can't find a de-facto guide online on how to set this up properly. So here's a quick guide for my own reference (and others who may be searching for one)...


Install Git Bash for Windows

You'll need a Linux-like terminal that supports SSHing and Git out of the box. I highly recommend Git Bash.

Install Git Bash - default options during installation process are fine.

Create an SSH Key

Launch Git Bash and create an SSH key in your user's .ssh folder. 

$ mkdir ~/.ssh
$ cd ~/.ssh
$ ssh-keygen

You can specify the filename that your SSH key gets saved to, but by default it will save it to id_rsa and id_rsa.pub

Save your public SSH key to GitHub

Retrieve your public SSH key:

$ less ~/.ssh/id_rsa.pub

Copy the contents and add it to your GitHub account's SSH keys. At the time of writing, this will be available in GitHub -> Settings -> SSH and GPG Keys


Add the SSH Key to your SSH Agent

Start an SSH agent and add your new SSH key to it. 

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

Clone the repository

At this point, you should be able to clone your GitHub repository with SSH authentication. Remember to use the SSH URL when cloning the repository, not the HTTPS URL. 


Modify your .bashrc file

This is the major gotcha. Whenever you open up Git Bash, the old SSH agent you used will no longer be running, and Git Bash will be as effective as a goldfish in remembering your SSH key. So, you'll need to create a .bashrc file to tell Git Bash to re-add the key when the terminal is launched. 

# I'm using Visual Studio Code here, but feel free to use whatever code editor you want.
$ code ~/.bashrc

In the code editor, add these two lines of code, so that an SSH agent with your SSH Key is available upon the terminal's startup.

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

FAQ

Q: Part of this guide doesn't work anymore! How am I supposed to deal with this?!
A: If this is happening, this means an outdated guide is actually ranked well on search results... Please find my email on the Contact page and let me know.