Generating an SSH Public/Private Key Pair in Windows to Link to your GitHub for Passwordless Git Operations

Mutuma GitongaMutuma Gitonga
4 min read

Introduction

By default, a local git client requires GitHub password authentication for common operations such as git clone, git pull, and git push. Sticking to this form of authentication is a great hassle and perhaps that's why you are here.

This article covers the process of generating a Secure Shell (SSH) public-private key pair to accomplish this in Windows using Git Bash. What is Git Bash?

Git Bash is a Windows command line/graphical user interface tool for interacting with Git on Windows. You may know that one can equally use Windows Powershell to interact with git.

So what makes Git bash unique? Well, Git bash supports Unix-style commands in addition to a shell that allows running bash scripts more seamlessly than Powershell would offer. Download Git bash here. Watch a comprehensive Git bash installation tutorial here.

SSH Key Generation Process

Here is a step-wise SSH key pair generation in Windows:
First launch git bash.

  1. Check your working directory using the command pwd or look at the git bash menu bar which should show the current directory path.

    By default, git bash launches with the working directory being /C/Users/<yourwindowsusername>/. If this is not the case, change to this directory using the command cd /C/Users/<yourwindowsusername>/

    If unsure about your windows username, just type whoami on the bash terminal, and then you should have it.

  2. Check if the hidden .ssh folder is in the current /C/Users/<yourwindowsusername>/ directory using the command ls -lha to print existing files and folders. If .ssh appears on the list, you likely have ssh keys already generated so proceed to step 4, otherwise proceed to step 3.

  3. Create .ssh folder using mkdir .ssh and enter ls -lha command to ascertain its creation.

  4. Enter cd .ssh to navigate to .ssh directory.

  5. Now list files with ls command. If you see files like "id_ed25519, id_ed25519.pub, id_rsa, or id_rsa.pub", you have SSH keys and can proceed to step 9 otherwise proceed to step 6.

  6. Generate new ssh public-private key pair using the following command: ssh-keygen -t ed25519 -C . You should see Generating public/private ed25519 key pair, and a prompt asking: Enter file in which to save the key. Simply press Enter to save the key in this directory.

  • A prompt comes up: Enter passphrase (empty for no passphrase): A passphrase isn't mandatory so hit Enter to continue. A confirmation message on the same (i.e., Enter passphrase) will appear but press Enter again to proceed.

  • Other notifications inform you that your identification and public key has been saved in the directory path provided previously. Besides, the key fingerprint string and random art are equally printed. Now enter ls to view the created keys.

  1. Next, key the command eval "$(ssh-agent -s)" to start the SSH agent.

  2. Following this, add the private key (one without .pub extension) to the SSH agent with the command "ssh-add ~/.ssh/id_ed25519"

  3. Next, add the public key (one with .pub extension) to GitHub SSH keys.

  • First, copy the public key into the clipboard with the command clip < /c/Users/<yourwindowsusername>/.ssh/id_ed25519.pub. Alternatively, you can navigate to the same directory on file explorer by pasting the working directory path C:\Users<windowsusername>.ssh on the file explorer address bar, then open the public key in a text editor and copy it.

  • Now, login into your GitHub account.

  • Click on your profile icon on the top right, then click settings.

  • Click on SSH and GPG keys under Access.

  • Click on the New SSH key

  • Key in a suitable name in the title box, paste the public key into the bottom box, then confirm by clicking Add SSH key.

  1. Once that is done, you can now clone git repositories into your local machine using SSH addresses rather than HTTPS without requiring a password.

Conclusion

In this article, we have learned how to generate an SSH public/private key pair in Windows to ease GitHub authentication as summarized below:

  1. Launch Git Bash

  2. Check your current working directory

  3. Change to your home directory if necessary

  4. Check if the .ssh folder exists

  5. Create the .ssh folder if it doesn't exist

  6. Navigate to the .ssh folder

  7. Check if you have any existing SSH keys

  8. Generate a new SSH key if necessary

  9. Start the SSH agent

  10. Add your private key to the SSH agent

  11. Copy your public key to the clipboard

  12. Add your public key to your GitHub account

0
Subscribe to my newsletter

Read articles from Mutuma Gitonga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mutuma Gitonga
Mutuma Gitonga

I am a programmer with a great interest in sharing web programming nuggets with the tech community. My particular areas of interest include Ruby, Ruby on Rails, Javascript, and ReactJs.