Git SSH Setup
SSH keys let you authenticate with GitHub and other Git servers without typing a username and password every time you push or pull.
Why SSH Instead of HTTPS
Git supports two main ways to talk to a remote repository: HTTPS and SSH. HTTPS asks for a username and a personal access token on every push unless you cache credentials. SSH uses a cryptographic key pair instead: a private key that stays on your machine, and a public key that you upload to GitHub. Once configured, every push and pull is authenticated automatically and silently, which makes SSH the preferred setup for anyone pushing code regularly.
The key pair works like a lock and a matching key. GitHub holds the public key, which behaves like a lock it recognizes. Your machine holds the private key, which is the only thing that can open that lock. As long as the private key never leaves your computer, the pair is safe to use indefinitely.
Checking for Existing Keys
Before generating a new key, check whether you already have one. SSH keys are usually stored in the .ssh folder in your home directory, and reusing an existing key is fine if you have one you trust.
List existing SSH keys
$ ls -al ~/.ssh
drwx------ 2 dev dev 4096 Mar 3 09:10 .
drwxr-xr-x 15 dev dev 4096 Mar 3 09:10 ..
-rw------- 1 dev dev 2610 Mar 3 09:10 id_ed25519
-rw-r--r-- 1 dev dev 600 Mar 3 09:10 id_ed25519.pub
-rw-r--r-- 1 dev dev 444 Mar 3 09:10 known_hostsGenerating a New SSH Key
Use ssh-keygen with the Ed25519 algorithm, which is faster and more secure than the older RSA keys. Pass your email with -C so the key is labeled with something recognizable in GitHub's key list.
Generate a new key
$ ssh-keygen -t ed25519 -C "you@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/dev/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dev/.ssh/id_ed25519
Your public key has been saved in /home/dev/.ssh/id_ed25519.pubAdding the Key to the ssh-agent
The ssh-agent runs in the background and remembers your passphrase for the session, so you aren't prompted on every single Git command. Start it, then add your private key.
Start the agent and add your key
$ eval "$(ssh-agent -s)"
Agent pid 41823
$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/dev/.ssh/id_ed25519 (you@example.com)Adding the Public Key to GitHub
Copy the contents of the .pub file, then paste it into GitHub under Settings > SSH and GPG keys > New SSH key. Never paste the private key anywhere; only the .pub file is meant to be shared.
- Copy the key: cat ~/.ssh/id_ed25519.pub (or use clip / pbcopy to copy it directly)
- Go to GitHub > Settings > SSH and GPG keys
- Click New SSH key, give it a descriptive title like "Work Laptop"
- Paste the public key content and click Add SSH key
Testing the Connection
GitHub provides a special SSH endpoint just for verifying that authentication works, without cloning anything. Run it after adding your key to confirm everything is wired up correctly.
Test the SSH connection
$ ssh -T git@github.com
Hi octocat! You've successfully authenticated, but GitHub does not
provide shell access.Common Setup Details
Exercise: Git SSH Setup
Why do developers set up SSH keys for use with Git remotes?