Generating an SSH Public/Private Key Pair in Windows to Link to your GitHub for Passwordless Git Operations
Table of contents
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.
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 commandcd /C/Users/<yourwindowsusername>/
If unsure about your windows username, just type
whoami
on the bash terminal, and then you should have it.Check if the hidden
.ssh
folder is in the current/C/Users/<yourwindowsusername>/
directory using the commandls -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.Create
.ssh
folder usingmkdir .ssh
and enterls -lha
command to ascertain its creation.Enter
cd .ssh
to navigate to.ssh
directory.Now list files with
ls
command. If you see files like "id_ed25519
,id_ed25519.pub
,id_rsa
, orid_rsa.pub
", you have SSH keys and can proceed to step 9 otherwise proceed to step 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.
Next, key the command
eval "$(ssh-agent -s)"
to start the SSH agent.Following this, add the private key (one without .pub extension) to the SSH agent with the command "
ssh-add ~/.ssh/id_ed25519
"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 pathC:\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.
- 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:
Launch Git Bash
Check your current working directory
Change to your home directory if necessary
Check if the .ssh folder exists
Create the .ssh folder if it doesn't exist
Navigate to the .ssh folder
Check if you have any existing SSH keys
Generate a new SSH key if necessary
Start the SSH agent
Add your private key to the SSH agent
Copy your public key to the clipboard
Add your public key to your GitHub account