Blog post image for Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows - Git connects to remotes by default via HTTPS, which requires you to enter your login and password every time you run a command like Git pull or git push, using the SSH protocol. You may connect to servers and authenticate to access their services. The three services listed allow Git to connect through SSH rather than HTTPS. Using public-key encryption eliminates the need to type a login and password for each Git command.
Blog

Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Blog

Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Published: Updated: 06 Mins read

Introduction

By default, Git talks to remotes over HTTPS, so it asks for your username and password on every git pull or git push. SSH fixes that. GitHub, GitLab, and Bitbucket all let Git authenticate over SSH with public-key encryption instead. Set it up once and you stop typing credentials for every Git command.

Info

An SSH key is a pair of files: a private key that never leaves your machine, and a public key you upload to each service. Authentication works by proving you hold the private key. No password travels over the network.

Make sure Git is installed

Before you start, check whether Git is already on your machine. Run this in Windows Terminal (or PowerShell):

Terminal window
git --version

If you get a version number back, youโ€™re set skip to Generate SSH keys. If the command isnโ€™t found, install Git using whichever method you prefer.

  1. Download the latest Git for Windows from the official site.
  2. Run the installer. The defaults are sensible clicking Next through each screen is fine for most setups, including the editor, line-ending, and terminal choices.
  3. Click Install, then Finish.
  4. Open a new terminal and confirm it worked:
Terminal window
git --version

Note

After installing Git, set your global name and email every commit you make is stamped with them:

Terminal window
git config --global user.name 'USERNAME'
git config --global user.email '[email protected]'

Generate SSH keys

Open Windows Terminal and generate a new key pair, replacing [email protected] with your email address. Use Ed25519 itโ€™s what GitHub, GitLab, and Bitbucket recommend today. Reach for RSA only on an older system or server that doesnโ€™t support Ed25519.

Terminal window
ssh-keygen -t ed25519 -C "[email protected]"

This creates .ssh\id_ed25519 (private) and .ssh\id_ed25519.pub (public) in your user folder. Ed25519 keys are small and fast, with security on par with a 4096-bit RSA key.

After running the command, complete the prompts:

  1. Choose where to save the private key. Press Enter to accept the default location (C:\Users\you\.ssh\id_ed25519, or id_rsa for an RSA key):
Terminal window
Enter file in which to save the key (/c/Users/you/.ssh/id_ed25519): [Press Enter]
  1. If a key already exists, youโ€™ll be asked whether to overwrite it. Type y and press Enter:
Terminal window
Overwrite (y/n)?
  1. Enter and re-enter a passphrase (think of it as a password for the key):
Terminal window
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Tip

A passphrase encrypts your private key on disk, so a stolen key file is useless without it. On Windows you can have the OpenSSH Authentication Agent remember it for you (see the FAQ below) so you only type it once.

The whole interaction should look like this:

Terminal window
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/you/.ssh/id_ed25519):
Created directory '/c/Users/you/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/you/.ssh/id_ed25519.
Your public key has been saved in /c/Users/you/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx YOUR_EMAIL@EXAMPLE.COM

Confirm the key landed in your .ssh folder:

Terminal window
Get-ChildItem .\.ssh\

A typical .ssh folder looks like this:

  • Directory.ssh/
    • id_ed25519 Ed25519 private key never share this
    • id_ed25519.pub Ed25519 public key safe to upload
    • id_rsa RSA private key (legacy) never share this
    • id_rsa.pub RSA public key (legacy) safe to upload
    • known_hosts
    • config

Add the public SSH key to your account

Copy your public key to the clipboard with PowerShell pick the tab that matches the key type you created:

Terminal window
Get-Content .\.ssh\id_ed25519.pub | Set-Clipboard

Warning

Only ever copy and paste the public key (the .pub file). The private key (id_ed25519 or id_rsa, with no extension) must never be uploaded or shared with anyone.

Now add that public key to your account. Pick your service below:

Sign in to your GitHub account at github.com. Click your profile photo in the upper-right corner, then Settings:

GitHub Settings

Select SSH and GPG keys from the sidebar, then New SSH key. Give it a descriptive title (for example, your computerโ€™s name) and paste your public key into the Key field. Click Add SSH key:

GitHub Settings

The key now appears in the list of SSH keys on your account:

GitHub Settings

Test connecting via SSH

Before you start using SSH with Git, all three services let you check that the connection works.

Once youโ€™ve added your SSH key to your GitHub account, open the terminal and type:

Terminal window

If youโ€™re connecting to GitHub over SSH for the first time, the SSH client will ask if you trust the GitHub serverโ€™s public key:

Terminal window
The authenticity of host 'github.com (140.82.113.4)' can't be established.
RSA key fingerprint is SHA256:a5d6c20b1790b4c144b9d26c9b201bbee3797aa010f2701c09c1b3a6262d2c02.
Are you sure you want to continue connecting (yes/no)?

Type yes and press Enter. GitHub is added to your known hosts and wonโ€™t prompt again:

Terminal window
Warning: Permanently added 'github.com,140.82.113.4' (RSA) to the list of known hosts.

GitHub only allows this SSH connection for testing, not shell access, so it confirms youโ€™re authenticated and then closes the connection:

Terminal window
Hi YOUR_USER_NAME! You've successfully authenticated, but GitHub does not provide shell access.

The whole interaction should look something like this:

Terminal window
The authenticity of host 'github.com (140.82.113.4)' can't be established.
RSA key fingerprint is SHA256:a5d6c20b1790b4c144b9d26c9b201bbee3797aa010f2701c09c1b3a6262d2c02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,140.82.113.4' (RSA) to the list of known hosts.
Hi your_user_name! You've successfully authenticated, but GitHub does not provide shell access.

Test passed youโ€™re ready to use SSH with GitHub.

Frequently Asked Questions

Use the built-in OpenSSH Authentication Agent. In an admin PowerShell, enable and start the service, then add your key:

Terminal window
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $HOME\.ssh\id_ed25519

The agent keeps the decrypted key in memory, so you type the passphrase once per login instead of on every Git command.

Use ed25519. The keys are smaller and faster than RSA with comparable security, and itโ€™s what GitHub, GitLab, and Bitbucket recommend. Generate one with ssh-keygen -t ed25519 -C "[email protected]". Reach for rsa -b 4096 only when you need to connect to an older server that doesnโ€™t speak Ed25519.

Yes. The same public key can be added to as many accounts and services as you like thereโ€™s no need for a separate key per provider. Just paste id_ed25519.pub (or id_rsa.pub) into each serviceโ€™s SSH-keys settings.

Usually the key isnโ€™t loaded in the agent, the public key wasnโ€™t added to the service, or the wrong key path is being used. Run ssh -vT [email protected] to see which key the client offers, confirm the OpenSSH agent is running, and make sure you uploaded the matching .pub file.

The OpenSSH client ships with Windows 10/11 but can be disabled. Install it from an admin PowerShell:

Terminal window
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

Git for Windows also bundles ssh/ssh-keygen, so running the commands inside Git Bash works too.


References

Related Posts

You might also enjoy

Check out some of our other posts on similar topics

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Introduction By default, Git talks to remotes over HTTPS, so it asks for your username and password on every git pull or git push. SSH fixes that. GitHub, GitLab, and Bitbucket all let Git aut

Customization Windows Terminal With Starship

Customization Windows Terminal With Starship

Introduction In this article, we will learn how to install PowerShell and Starship, how to configure the Windows Terminal, and how to customize the Windows Terminal with Starship. What Is a W

Dotfiles: A Git-Based Strategy for Configuration Management

Dotfiles: A Git-Based Strategy for Configuration Management

Introduction Your dotfiles those hidden .-prefixed configuration files scattered across your home directory are the muscle memory of your environment. They hold your shell aliases, your editor s

10+ Secret Git Commands That Will Save Hours Every Week

10+ Secret Git Commands That Will Save Hours Every Week

Introduction As a Software Engineer, DevOps Engineer, or GitHub user, you probably use Git daily. But are you making the most of it? Git is packed with powerful commands that can save

How to Install and Setup FireWall on Amazon Linux 2

How to Install and Setup FireWall on Amazon Linux 2

Introduction We will learn how to install and setup FireWall on Amazon Linux 2 in this tutorial. We will also discover how to set up FireWall so that it functions with the Amazon Linux 2. Prer

VIM Cheat Sheet

VIM Cheat Sheet

What Is VIM? VIM (Vi Improved) is a versatile text editor pre-installed on most Linux systems, known for its efficiency in command-line file editing. Its modal nature switching between modes like

6 related posts