To connect to GitHub with SSH, generate a key with ssh-keygen, add the public key to your GitHub account under Settings → SSH and GPG keys, and test it with ssh -T [email protected]. After that, git clone, git pull, and git push work without a password or token.

GitHub stopped accepting account passwords for Git operations over HTTPS in 2021. You can use a personal access token instead, but SSH keys are simpler for everyday work: you set them up once per computer and never paste a token again. This guide walks through the whole setup on Linux, macOS, and Windows, then covers switching existing repositories, using two GitHub accounts, and fixing the usual errors.

If SSH itself is new to you, start with What Is SSH? A Beginner’s Guide to Secure Remote Login with SSH Keys, which explains how key pairs work.

HTTPS vs. SSH for GitHub

Every GitHub repository has two clone URLs:

HTTPSSSH
URL formathttps://github.com/user/repo.git[email protected]:user/repo.git
How you log inPersonal access token or a credential managerSSH key on your computer
SetupCreate a token, store itGenerate a key, add it to GitHub once
Port44322 (or 443 as a fallback)
Best forQuick one-off clones, locked-down networksDaily development on your own machines

Both are secure. SSH wins on convenience: there’s no token to expire, and each computer gets its own key that you can revoke on its own.

What You’ll Need

  • A GitHub account
  • Git installed (git --version should print a version)
  • An OpenSSH client, which is built into Linux, macOS, and Windows 10/11 (ssh -V to check)

On Windows, run the commands below in PowerShell or Git Bash. Where the paths differ, ~ means your user folder (C:\Users\<you>).

Step 1: Check for an Existing SSH Key

You may already have a key. List the contents of your .ssh folder:

ls -al ~/.ssh

Look for a pair of files such as id_ed25519 and id_ed25519.pub (or id_rsa and id_rsa.pub). If you have one and know its passphrase, you can skip to Step 3. If the folder doesn’t exist or is empty, create a new key.

Step 2: Generate a New SSH Key

Run ssh-keygen with the Ed25519 algorithm and the email address on your GitHub account:

ssh-keygen -t ed25519 -C "[email protected]"

The -C value is only a label so you can tell keys apart later. You’ll be asked two things:

  1. Where to save the key. Press Enter to accept the default, ~/.ssh/id_ed25519.
  2. A passphrase. Set one. It encrypts the private key on your disk, so a stolen laptop or backup doesn’t hand over access to your repositories. The SSH agent (next step) means you won’t have to type it often.

You now have two files:

~/.ssh/id_ed25519       # private key: never share or upload this
~/.ssh/id_ed25519.pub   # public key: this is what GitHub gets

If you need to connect from an older system that doesn’t support Ed25519, use RSA instead: ssh-keygen -t rsa -b 4096 -C "[email protected]".

Step 3: Add the Key to the SSH Agent

The SSH agent keeps your unlocked key in memory, so you type the passphrase once per session instead of on every push.

Linux

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Most desktop Linux distributions already run an agent, in which case only the ssh-add line is needed.

macOS

Create or edit ~/.ssh/config and add:

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Then add the key and store its passphrase in the macOS keychain:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Windows (PowerShell)

The OpenSSH agent is a Windows service that’s turned off by default. In a PowerShell window opened as Administrator, enable and start it:

Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent

Then, in a normal PowerShell window:

ssh-add $env:USERPROFILE\.ssh\id_ed25519

Git for Windows comes with its own copy of SSH, which doesn’t use the Windows agent. If Git keeps asking for your passphrase, tell it to use the Windows version:

git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

Step 4: Add the Public Key to Your GitHub Account

Copy the public key (the .pub file) to your clipboard:

# Linux (or just open the file and copy its single line)
cat ~/.ssh/id_ed25519.pub

# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Windows PowerShell
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

The line starts with ssh-ed25519 and ends with the email you used as a comment. Then, on GitHub:

  1. Click your profile picture in the top-right corner and choose Settings.
  2. In the sidebar, open SSH and GPG keys.
  3. Click New SSH key.
  4. Give it a title that names the computer, such as “Work laptop” or “Home desktop”.
  5. Leave Key type set to Authentication Key.
  6. Paste the key and click Add SSH key. GitHub may ask you to confirm with your password or two-factor code.

GitHub occasionally reorganizes its settings pages, so the labels may differ slightly. If you use the GitHub CLI, gh ssh-key add ~/.ssh/id_ed25519.pub --title "Work laptop" does the same thing from the terminal.

Step 5: Test the Connection

Always connect as the user git, not your own username. The first time, SSH shows GitHub’s host key fingerprint and asks whether to continue:

The authenticity of host 'github.com' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Before typing yes, check that the fingerprint matches one GitHub publishes for its servers. At the time of writing they are:

SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU (ED25519)
SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM (ECDSA)
SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s (RSA)

GitHub lists the current values in its documentation under “GitHub’s SSH key fingerprints”. A fingerprint that doesn’t match means you aren’t talking to GitHub, so stop there.

If everything works, you’ll see:

Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.

That message is the success. GitHub only accepts Git commands over SSH, so there’s no shell to log in to.

Step 6: Clone and Push with SSH

For new projects, copy the SSH URL from the green Code button on the repository page (choose the SSH tab) and clone it:

git clone [email protected]:yourusername/your-repo.git

From then on, git pull and git push use your key automatically. If the basic Git commands are still new, Mastering Git: Basic Commands for Version Control covers the everyday workflow.

Switch an Existing Repository from HTTPS to SSH

A repository you cloned over HTTPS keeps using HTTPS until you change its remote URL. Check the current one:

git remote -v

If it starts with https://, switch it:

git remote set-url origin [email protected]:yourusername/your-repo.git
git remote -v

The output should now show the [email protected]: URL for both fetch and push.

Using Two GitHub Accounts on One Computer

GitHub won’t let the same public key belong to two accounts, so a personal and a work account need separate keys. Create a second key with its own file name:

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work

Add id_ed25519_work.pub to the work account, then give it a host alias in ~/.ssh/config:

Host github.com
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes

IdentitiesOnly yes makes SSH offer only the key named in that block instead of every key in the agent, which is what stops GitHub from logging you in as the wrong account. For work repositories, use the alias in place of github.com:

ssh -T git@github-work
git clone git@github-work:your-company/project.git

When Port 22 Is Blocked

Some office, school, and hotel networks block outgoing connections on port 22, so ssh -T [email protected] hangs or times out. GitHub also accepts SSH on port 443 at ssh.github.com. Test it:

ssh -T -p 443 [email protected]

If that works, make it permanent in ~/.ssh/config so your existing [email protected]: URLs keep working:

Host github.com
  HostName ssh.github.com
  Port 443
  User git

Bonus: Sign Your Commits with the Same Key

Git can also use an SSH key to sign commits, which adds a Verified badge on GitHub and proves the commit came from you. It’s the same idea covered in Non-Repudiation Explained: Digital Signatures and Proof of Origin. Point Git at your public key:

git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true

Then add the same public key to GitHub a second time, this time with Key type set to Signing Key. Your commit email must also be one of the verified emails on your GitHub account, or the badge won’t appear.

Troubleshooting Common Errors

  • Permission denied (publickey). GitHub didn’t accept any key you offered. Check that the public key is on your account, that the agent has the key (ssh-add -l lists loaded keys), and that you connect as [email protected], not [email protected]. Run ssh -vT [email protected] to see which key files SSH tries.
  • Hi other-account! instead of your username. SSH offered a key that belongs to a different account. Use the IdentitiesOnly config from the two-accounts section.
  • Asked for a username and password on push. The repository still uses an HTTPS remote. Switch it with git remote set-url, as shown above.
  • Host key verification failed or REMOTE HOST IDENTIFICATION HAS CHANGED!. GitHub replaced its RSA host key in March 2023, so older known_hosts files can hold a stale entry. Remove it with ssh-keygen -R github.com, connect again, and check the fingerprint against the list above.
  • WARNING: UNPROTECTED PRIVATE KEY FILE! On Linux and macOS, fix the permissions with chmod 600 ~/.ssh/id_ed25519.
  • The connection hangs. Port 22 is probably blocked. Use port 443 as described above.

SSH Key Best Practices for GitHub

  • One key per computer. If a laptop is lost, delete just that key from GitHub without touching the others.
  • Use a passphrase and let the agent remember it.
  • Name keys clearly on GitHub, and review the list under SSH and GPG keys now and then. Delete any you don’t recognize.
  • Never commit your .ssh folder or paste the private key anywhere. Only .pub files are safe to share.
  • Use deploy keys for servers. A server that only needs to pull one repository should get a deploy key in that repository’s settings, not a key on your personal account.

Conclusion

Connecting to GitHub with SSH takes about five minutes: generate an Ed25519 key, load it into the SSH agent, add the public key to your GitHub settings, and confirm with ssh -T [email protected]. Switch older repositories with git remote set-url, and from then on every pull and push is authenticated by a key that never leaves your computer.

Once SSH is set up, publishing your work is easy too. How to Host a Static HTML Website on GitHub Pages for Free shows how to turn a repository into a live website.

Frequently Asked Questions

Is SSH or HTTPS better for GitHub?

Both are secure. SSH is more convenient on your own computers because there’s no token to create, store, or renew. HTTPS is handy on networks that block SSH or for a quick one-off clone.

Why does ssh -T [email protected] say GitHub does not provide shell access?

That’s the success message. It means your key was accepted. GitHub only runs Git commands over SSH, so it closes the session right after authenticating.

Can I use the same SSH key on two computers?

You can copy it, but it’s better not to. A separate key per computer lets you revoke one device without affecting the rest.

Can I use one SSH key for two GitHub accounts?

No. GitHub ties each public key to one account. Create a second key and use a host alias in ~/.ssh/config, as shown above.

Do I still need a personal access token?

Not for Git over SSH. You may still want one for scripts that call the GitHub API, or for HTTPS on a machine where you can’t use SSH.

What happens if my private key is stolen?

Delete the matching key from SSH and GPG keys on GitHub right away, then generate a new one. A passphrase on the key gives you time, since the stolen file can’t be used without it.

Want to keep learning? These related guides pair well with this post: