← Back to Git Course | Chapter 1: Introduction & Setup | Lesson 7 of 7

Git SSH Setup

Why Use SSH with Git

HTTPS remotes ask for your username and a personal access token on every push unless you cache credentials, while SSH authenticates with a key pair stored on your machine -- once it's set up, pushing and pulling never prompt for a password again. The private key stays on your computer and is never transmitted; only the matching public key needs to be uploaded to GitHub, GitLab, or Bitbucket.

Example: Why Use SSH with Git

bash
ssh-keygen -t ed25519 -C "[email protected]"

Adding SSH Key to GitHub Account

Once generated, the public key (the .pub file) needs to be pasted into your Git hosting provider's SSH-keys settings page -- the provider then uses it to verify future connections claim to be from you, without ever seeing your private key. On GitHub specifically, open Settings > SSH and GPG keys > New SSH key, paste the public key from the .pub file into the Key field, give it a descriptive title, and save -- GitHub then trusts that key for every future SSH connection from this machine, and the same key can also be added from the terminal using the GitHub CLI.

Example: Adding SSH Key to GitHub Account

bash
cat ~/.ssh/id_ed25519.pub
# paste into GitHub Settings > SSH and GPG keys

Testing the Connection

Before relying on SSH for real work, it's worth confirming the handshake actually succeeds -- GitHub and GitLab both expose a special test endpoint that authenticates your key and replies with a friendly message instead of granting shell access.

Example: Testing the Connection

bash
ssh -T [email protected]

Switching a Remote to SSH

A repository you originally cloned over HTTPS keeps using HTTPS for every future push and pull until you explicitly point its remote URL at the SSH address instead -- the switch doesn't require re-cloning the repository.

Example: Switching a Remote to SSH

bash
git remote set-url origin [email protected]:user/repo.git

Managing Multiple Keys

Developers who work across several accounts -- a personal GitHub and a work GitLab, for example -- typically generate a separate key pair per account and map each one to its host using an SSH config file, rather than reusing one key everywhere.

Example: Managing Multiple Keys

bash
# ~/.ssh/config
Host github.com-work
  HostName github.com
  IdentityFile ~/.ssh/id_ed25519_work
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.