Git SSH Setup
In this page:
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
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
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
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
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
# ~/.ssh/config
Host github.com-work
HostName github.com
IdentityFile ~/.ssh/id_ed25519_work
- Sharing the private key such as
id_ed25519instead of the publicid_ed25519.pubwhen adding the key to GitHub. - Cloning with an
https://URL and expecting SSH to be used, when the remote must be the[email protected]:user/repo.gitform. - Skipping
ssh -T [email protected]after setup, so a wrong key or a missingssh-agentis only discovered when a push fails.
- 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: