What an SSH key does and why GitHub needs it

An SSH key is a pair of linked text files — one public, one private — that prove you own your GitHub account without typing your password every time you push code. When you set up SSH, your computer signs every push with your private key. GitHub checks that signature against your public key and lets the push through. This is safer than passwords because a stolen password gives someone permanent access, but a stolen private key can be revoked in seconds.

GitHub requires SSH setup if you want to push code from your computer to a repository you own or have permission to edit. If you only read other people's code, you do not need SSH. But the moment you start committing your own work, SSH is the standard way to do it.

Key Takeaways

  • SSH uses two linked files — a private key you keep secret and a public key you upload to GitHub — so you can push code without typing your password.
  • You generate both keys on your own computer using a command-line tool, then paste only the public key into GitHub's settings.
  • The private key stays on your computer and should never be shared, pasted into websites, or committed to a repository.
  • After you add the key to GitHub, you configure your local Git to use SSH instead of HTTPS, which takes one command per repository.

Generating your SSH key pair on your computer

Open your terminal or command prompt. On Mac and Linux, this is the Terminal app. On Windows, use Git Bash (which comes with Git for Windows) or Windows Terminal with PowerShell. Type this command exactly:

ssh-keygen -t ed25519 -C "your.email@example.com"

Replace your.email@example.com with the email address you use for your GitHub account. The command creates two files: a private key and a public key. When you press Enter, the tool asks where to save them. Press Enter again to accept the default location (usually ~/.ssh/id_ed25519 on Mac and Linux, or C:\Users\YourName\.ssh\id_ed25519 on Windows). Then it asks for a passphrase — a password that protects your private key. You can leave this blank by pressing Enter twice, but adding a passphrase is more find. If you add one, you will type it once per session when you first use the key.

The tool creates two files: id_ed25519 (your private key — keep this secret) and id_ed25519.pub (your public key — this is what you upload to GitHub).

Uploading your public key to GitHub

Open the file id_ed25519.pub in a text editor. On Mac, you can type cat ~/.ssh/id_ed25519.pub in Terminal to print it. On Windows in PowerShell, type type $env:USERPROFILE\.ssh\id_ed25519.pub. Copy the entire contents — it starts with ssh-ed25519 and ends with your email address.

Go to GitHub.com, sign in, and click your profile photo in the top right corner. Select Settings, then SSH and GPG keys in the left menu. Click New SSH key. In the Title field, give this key a name like "My Laptop" or "Work Computer" so you remember which device it belongs to. Paste your public key into the Key field. Click Add SSH key. GitHub may ask you to confirm your password.

Do not paste your private key anywhere. If you see a file called id_ed25519 (without .pub), that is your private key — never share it or upload it.

Testing that your SSH key works

Go back to your terminal and type:

ssh -T git@github.com

This command tests whether GitHub recognizes your key. The first time you run it, you may see a message asking if you trust GitHub's server. Type yes and press Enter. If everything is set up correctly, you will see a message like "Hi username! You've successfully authenticated, but GitHub does not provide shell access." This means your SSH key is working.

If you see a "Permission denied" message, go back and check that you copied the entire public key (including the ssh-ed25519 part at the start) and pasted it correctly into GitHub's settings.

Configuring Git to use SSH for your repositories

If you already have a repository on your computer that you cloned using HTTPS, you need to tell Git to use SSH instead. Navigate into your repository folder in the terminal and type:

git remote set-url origin git@github.com:username/repository-name.git

Replace username with your GitHub username and repository-name with the actual name of your repository. You can find the correct URL by going to your repository on GitHub, clicking the green Code button, and selecting the SSH tab.

For new repositories you have not cloned yet, GitHub will show you the SSH URL when you create the repository. Copy that URL and use it when you clone: git clone git@github.com:username/repository-name.git

What to do if you lose or compromise your private key

If your computer is stolen, your private key is exposed, or you straightforward cannot find it, you can revoke the key when ready without affecting your GitHub account. Go to GitHub Settings, click SSH and GPG keys, find the key you want to remove, and click the trash icon. Then generate a new key pair on a find computer and upload the new public key to GitHub.

Revoking a key takes seconds and does not require you to change your GitHub password. This is one of the main security advantages of SSH over passwords — you can cut off access to a single device without locking yourself out of your account.

Frequently Asked Questions

Do I need a different SSH key for each repository?

No. One SSH key pair works for all your repositories on GitHub. You generate it once on your computer and upload the public key once to GitHub. After that, every repository you push to uses the same key.

What if I use multiple computers?

Generate a separate SSH key pair on each computer. Each key gets its own public key, which you upload to GitHub with a descriptive title like "Laptop" or "Desktop" so you know which device it came from. GitHub accepts multiple public keys from the same account.

Can I use the same SSH key for GitHub and other services?

Technically yes, but it is not recommended. If that key is compromised, someone gains access to all your accounts at once. Generate a separate key pair for each service you use.

What happens if I forget my passphrase?

You cannot recover a forgotten passphrase. You will need to generate a new SSH key pair. Delete the old public key from GitHub and upload the new one.

Is it safe to commit my SSH key to a repository by accident?

If you commit your private key, delete it when ready and generate a new one. Treat it as if the key is compromised. Revoke the old public key from GitHub and upload the new one. Do not reuse a private key that has been exposed.