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

Git SSH Setup

SSH keys are like a special secret handshake between your computer and GitHub. Once they know each other's handshake, you do not need to type your password every time.
Syntax
bash
ssh-keygen -t ed25519 -C "[email protected]"
cat ~/.ssh/id_ed25519.pub
ssh -T git@host_name
git clone git@host_name:user/repo.git

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Sharing the private key such as id_ed25519 instead of the public id_ed25519.pub when adding the key to GitHub.
  2. Cloning with an https:// URL and expecting SSH to be used, when the remote must be the [email protected]:user/repo.git form.
  3. Skipping ssh -T [email protected] after setup, so a wrong key or a missing ssh-agent is only discovered when a push fails.
Chapter Summary
  • Git is a version control system, and the chapter covers its history, features, installation, and configuration.
  • You create a first repository and learn the overall workflow.
  • SSH setup allows secure access to remote repositories.
🔒

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.