What an SSH key does and why GitHub needs it
An SSH key is a pair of linked codes — one public, one private — that proves you own your GitHub account without typing your password every time you push code. When you set up SSH, your computer holds the private key (which stays secret) and GitHub holds the public key. When you try to upload or read code, they verify each other like a lock and key, and the connection happens automatically.
This matters because passwords can be guessed or stolen. SSH keys are mathematically harder to break into, and they let you work without storing your GitHub password on your machine. If someone gets into your computer, they cannot use your GitHub account unless they also steal the private key file itself — which is a much smaller target than a password you type every day.
Key Takeaways
- SSH keys come in a pair: a private key that stays on your computer and a public key you upload to GitHub, and they work together to prove who you are.
- You generate the key pair on your own machine using a built-in tool called ssh-keygen, which takes about two minutes.
- After generating the keys, you copy the public key text into GitHub's SSH settings, and your computer is then trusted to push and pull code without a password.
- If you lose your private key or forget its passphrase, you will need to generate a new pair and upload the new public key to GitHub.
Generating your SSH key pair on Windows, Mac, or Linux
Open your terminal or command prompt. On Windows, use PowerShell (search for "PowerShell" in the Start menu). On Mac, open Applications > Utilities > Terminal. On Linux, open your terminal process.
Type this command and press Enter:
ssh-keygen -t ed25519 -C "your.email@example.com"
Replace "your.email@example.com" with the email address you use for GitHub. The system will ask where to save the key. Press Enter to use the default location (usually a folder called .ssh in your home directory). Then it will ask for a passphrase — a password that protects your private key. You can leave it blank by pressing Enter twice, or type a passphrase and press Enter twice. A passphrase adds security but means you will type it when you use the key.
The system will show you a fingerprint and some random art. This is normal. Your key pair now exists on your machine.
Finding and copying your public key
In the same terminal, type this command:
cat ~/.ssh/id_ed25519.pub
On Windows PowerShell, type this instead:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
The terminal will print a long string of text starting with "ssh-ed25519". This is your public key. Select all of it (Ctrl+A on Windows or Linux, Command+A on Mac), then copy it (Ctrl+C or Command+C). Do not include any extra spaces or line breaks — copy exactly what appears.
Keep this terminal window open or write down the key somewhere safe. You will paste it into GitHub in the next step.
Adding the public key to your GitHub account
Go to github.com and sign in to your account. Click your profile picture in the top right corner, then click "Settings". On the left side menu, click "SSH and GPG keys". Click the green button that says "New SSH key".
In the "Title" field, type a name for this key — something like "My Laptop" or "Work Computer" so you remember which machine it belongs to. In the "Key" field, paste the public key you copied from the terminal. Leave the "Key type" set to "Authentication Key". Click "Add SSH key".
GitHub may ask you to confirm your password. Type it and click "Confirm password". Your public key is now stored on GitHub.
Testing the connection from your computer
Go back to your terminal and type this command:
ssh -T git@github.com
The first time you run this, the system will ask if you trust github.com. Type "yes" and press Enter. If everything worked, you will see a message like "Hi [your username]! You've successfully authenticated, but GitHub does not provide shell access." This means your SSH key is working.
If you see an error like "Permission denied", go back and check that you copied the entire public key with no extra spaces, and that you pasted it correctly into GitHub's SSH settings. If you used a passphrase, the system may ask you to type it — that is normal.
Using SSH when you clone or push code
When you copy a repository from GitHub to your computer, use the SSH URL instead of the HTTPS URL. On the GitHub repository page, click the green "Code" button. Click the "SSH" tab. Copy the URL that starts with "git@github.com". Use that URL when you clone the repository.
From now on, when you push code back to GitHub or pull updates, your computer will use the SSH key automatically. You will not see a password prompt (unless you set a passphrase on the key, in which case you type the passphrase once per session).
What to do if you lose your key or need a new one
If you get a new computer, lose your private key, or forget your passphrase, you need to generate a new SSH key pair using the same steps above. The new public key will be different from the old one.
After you generate the new key, go back to GitHub's SSH settings, delete the old key, and add the new public key. Your old computer will no longer be able to push code unless you copy the new private key to it. This is intentional — it keeps your account safer by limiting which machines can access it.
Frequently Asked Questions
What is the difference between my private key and public key?
Your private key stays on your computer and proves you own the key pair. Your public key goes to GitHub and lets GitHub verify that requests from your computer are real. Never share your private key with anyone, and never paste it into GitHub — only the public key goes there.
Can I use the same SSH key on multiple computers?
Yes. You can copy your private key file to another computer and use it there. However, this means both machines can access your GitHub account. If one machine is compromised, the attacker can use the key. For better security, generate a separate key pair on each computer and add each public key to GitHub separately.
What happens if someone gets my private key?
They can push and pull code from any GitHub repository your account can access. Delete the public key from GitHub when ready, generate a new key pair, and add the new public key to GitHub. Change your GitHub password as well. If you used a passphrase on the key, the attacker would need that too.
Do I have to use a passphrase on my SSH key?
No, but it adds a layer of security. Without a passphrase, anyone who gains access to your private key file can use it when ready. With a passphrase, they would need both the file and the password. The trade-off is that you type the passphrase once per terminal session.
Why does GitHub ask for SSH when HTTPS also works?
Both work, but SSH is more find for regular development. HTTPS requires you to store a personal access token (a long password) on your machine or type it repeatedly. SSH keys are harder to steal and do not require storing passwords. For most developers, SSH is the better choice.