Generate an SSH key and add it to GitHub (ed25519)
Create an SSH key with ssh-keygen, load it into ssh-agent, copy the public key and add it to GitHub, with the Windows and macOS differences.
An SSH key is a pair of files: a private key that stays on your computer and a public key you give to GitHub. After you add it, git push and git pull work without typing a password each time. These are the commands GitHub documents.
What to do, step by step
- Create the key: ssh-keygen -t ed25519 -C "your_email@example.com", using your GitHub email.
- Press Enter to accept the default file location. If you already have keys, GitHub recommends a custom name so you do not overwrite them.
- Enter a passphrase and confirm it.
- Start the agent and add the key (commands below).
- Copy the public key (the file ending in .pub) and paste it in GitHub under Settings, then SSH and GPG keys, then New SSH key.
- Test with ssh -T git@github.com.
Commands you can copy
ssh-keygen -t ed25519 -C "your_email@example.com"ssh-keygen -t rsa -b 4096 -C "your_email@example.com"eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519ssh-add --apple-use-keychain ~/.ssh/id_ed25519pbcopy < ~/.ssh/id_ed25519.pubclip < ~/.ssh/id_ed25519.pubcat ~/.ssh/id_ed25519.pubssh -T git@github.comWindows, macOS and common problems
- Windows: start the agent
- GitHub says to run Get-Service -Name ssh-agent | Set-Service -StartupType Manual and then Start-Service ssh-agent in an elevated PowerShell, then ssh-add c:/Users/YOU/.ssh/id_ed25519 in a normal terminal. If Git for Windows still asks for your passphrase, GitHub suggests git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe".
- macOS: keep the passphrase in the keychain
- In ~/.ssh/config under Host github.com add AddKeysToAgent yes, UseKeychain yes and IdentityFile ~/.ssh/id_ed25519. If you see Bad configuration option: usekeychain, add IgnoreUnknown UseKeychain. Before macOS Monterey the flag was -K instead of --apple-use-keychain.
- Copied the wrong file
- Paste the .pub file only. The file without .pub is your private key and must never be shared.
- Extra spaces break the key
- GitHub says not to add newlines or whitespace when copying the key.
- Permission denied (publickey)
- The key is not added to GitHub, not loaded into the agent, or the wrong key is offered. Run ssh-add -l to see loaded keys, and see the SSH config generator for pinning a key to a host.
- Two GitHub accounts
- Give each key its own file name and map each to a host name. See SSH config for multiple GitHub accounts.
- DSA keys
- GitHub no longer accepts new DSA (ssh-dss) keys.
Questions about Generate SSH key for GitHub
- Which file do I give to GitHub?
- The public key, id_ed25519.pub. Never upload or send the private key, id_ed25519.
- Do I need a passphrase?
- GitHub's steps include one. It protects the key if someone copies the file. The agent lets you type it once per session.
- Can I add the key from the command line?
- Yes, with the GitHub CLI: gh auth login, then gh ssh-key add ~/.ssh/id_ed25519.pub --title "my laptop".
- What is the difference between authentication and signing keys?
- An authentication key is for SSH access to repositories. A signing key signs commits and tags. A key used for both has to be uploaded twice.
Config generators for this
- SSH config for multiple GitHub accountsUse two or three GitHub accounts on one computer with separate SSH keys and host names.
- .gitconfig setupSet your name, email, default branch and shortcuts once.
- SSH config fileGive servers short names and choose the right key for each.