Git and GitHub · Part 3 · GitHub and remote repositories
SSH keys and authentication
Why GitHub needs proof
Anyone can read a public repository, but only its owner and invited people can push to it. So on every push, GitHub needs proof of who is pushing.
The GitHub password doesn't work for this. Since August 2021, GitHub rejects passwords for Git commands:
remote: Support for password authentication was removed on August 13, 2021.
fatal: Authentication failed for 'https://github.com/jknithin36/repo.git/'There are two ways that do work:
| SSH | HTTPS + token | |
|---|---|---|
| Remote URL | git@github.com:jknithin36/repo.git | https://github.com/jknithin36/repo.git |
| Proof of identity | A key pair on the Mac | A personal access token, saved by a credential helper |
| Setup | Once, about 5 minutes | Quick with the GitHub CLI |
| Expiry | None (GitHub removes keys unused for a year) | Tokens usually expire |
Both stop asking once set up. Most developers use SSH, so this lesson sets that up, with the HTTPS route near the end.
How an SSH key works
SSH uses a key pair: two files made together.
| File | What it is | Share it? |
|---|---|---|
id_ed25519.pub | The public key, like a padlock: copies can be handed out | Yes, give it to GitHub |
id_ed25519 | The private key, the only key that fits that padlock | Never. Treat it like a password |
On each connection, GitHub sends a random challenge. The Mac signs it with the private key, and GitHub checks the signature against the public key it holds. Only the matching private key can make that signature, so GitHub knows who it is, and the private key itself never leaves the Mac.
Setting up SSH on a Mac
1.Check for existing keys
ls -la ~/.sshIf id_ed25519 and id_ed25519.pub (or id_rsa and id_rsa.pub) are listed, a key already exists: skip to step 5 to test it. If the folder is missing or has no id_ files, carry on.
2.Generate a key pair
ssh-keygen -t ed25519 -C "your-github-email@example.com"-t ed25519is the key type: modern, secure and short. Older guides usersa, which still works.-Cadds a label to tell keys apart. The email is the usual choice.
It asks three questions:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/you/.ssh/id_ed25519):
Enter passphrase for "/Users/you/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:Press Enter to keep the default location, then type a passphrase twice. It locks the private key file, so a stolen copy is useless without it. The Keychain will remember it, so it's typed only once.
Your identification has been saved in /Users/you/.ssh/id_ed25519
Your public key has been saved in /Users/you/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:6Gi4MfGupuP3bL0nn4IFo93wNWIYb7uUqG3vIjdcC+g your-github-email@example.com3.Let macOS remember the passphrase
The ssh-agent is a background program that holds the unlocked key in memory. Make sure it's running:
eval "$(ssh-agent -s)"Open the SSH config file, creating it if needed:
touch ~/.ssh/config
code ~/.ssh/configAdd this and save:
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519| Line | Meaning |
|---|---|
Host github.com | These settings apply when connecting to GitHub |
AddKeysToAgent yes | Load the key into the agent automatically |
UseKeychain yes | Keep the passphrase in the macOS Keychain (Mac only) |
IdentityFile | Which private key to use |
Then add the key to the agent and the Keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519After the passphrase is typed this once, macOS remembers it, even after a restart.
4.Give GitHub the public key
Copy the .pub file to the clipboard:
pbcopy < ~/.ssh/id_ed25519.pubpbcopy is the Mac's copy-to-clipboard command. The key is one line of text, safe to share:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your-github-email@example.comThen on GitHub:
- Click the profile picture (top right), then Settings.
- In the sidebar, click SSH and GPG keys, then New SSH key.
- Title: the computer's name, like MacBook Pro 2026. Key type: Authentication Key.
- Paste the key (Cmd + V) and click Add SSH key. GitHub may ask for the password or 2FA.
5.Test the connection
ssh -T git@github.comThe first time, SSH asks about GitHub itself:
The authenticity of host 'github.com (140.82.112.3)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?This checks that the server really is GitHub, not an impostor. The fingerprint above is GitHub's real Ed25519 one, listed in GitHub's docs under “GitHub's SSH key fingerprints”. If it matches, type yes. The Mac saves it in ~/.ssh/known_hosts and won't ask again.
Hi jknithin36! You've successfully authenticated, but GitHub does not provide shell access.“Hi” and the username mean it worked. “No shell access” is normal: GitHub can be used for Git, not logged into like a computer.
A first real GitHub repository
1.Create it on the website
- Click + (top right), then New repository.
- Name it git-learning, and choose Private for practice.
- Leave README, .gitignore and license unticked, then click Create repository.
The repository must start empty, because an existing one is about to be pushed. A README made on GitHub is a commit the local repository doesn't have, and the first push gets rejected. On the setup page, select SSH so the URL starts with git@github.com:.
2.Push a project
The practice repository from Lesson 3 works, or any other:
cd ~/Desktop/git-practice
git remote add origin git@github.com:jknithin36/git-learning.git
git push -u origin mainTo github.com:jknithin36/git-learning.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.Refresh the page on GitHub: the files and the whole commit history are there. Click commits to see every message.
3.Or do it all with the GitHub CLI
With gh installed (brew install gh), log in once:
gh auth loginChoose GitHub.com, then SSH, then the key id_ed25519.pub (gh can upload it, replacing step 4 above), then Login with a web browser. After that, one command creates the repository, adds origin and pushes:
gh repo create git-learning --private --source=. --remote=origin --pushSwitching a repository from HTTPS to SSH
A repository cloned with an https:// URL keeps asking for credentials. Point it at the SSH URL instead:
git remote -v
git remote set-url origin git@github.com:jknithin36/old-project.git
git remote -vorigin https://github.com/jknithin36/old-project.git (fetch)
origin https://github.com/jknithin36/old-project.git (push)
origin git@github.com:jknithin36/old-project.git (fetch)
origin git@github.com:jknithin36/old-project.git (push)The HTTPS route
Some office and university networks block SSH. HTTPS works everywhere, and the GitHub CLI makes it easy:
gh auth loginChoose HTTPS, and answer yes to “Authenticate Git with your GitHub credentials?”. gh creates and stores a token, and git push over HTTPS works with no prompts. (The manual way is creating a personal access token under Settings, then Developer settings, and pasting it where Git asks for a password.)
Two GitHub accounts on one Mac
A job may come with a separate GitHub account. Each account needs its own key:
ssh-keygen -t ed25519 -C "you@work.com" -f ~/.ssh/id_ed25519_workAdd the new .pub to the work account, then give it a nickname in ~/.ssh/config:
# Personal (default)
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# Work
Host github-work
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesgithub-work is a nickname for the real github.com that uses the work key. IdentitiesOnly yes makes each one use only its own key; without it, the agent may offer the personal key first, and GitHub treats the push as the personal account. Use the nickname in work URLs, and set the work email in those repositories (the local level from Lesson 2):
ssh -T git@github-work
git clone git@github-work:company/repo.git
cd repo
git config user.email "you@work.com"Bonus: the Verified badge
Anyone can set user.name to someone else's name. A signed commit proves who made it, and GitHub shows it with a green Verified badge. The same SSH key can sign:
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign trueThen on GitHub, under SSH and GPG keys, click New SSH key again, choose Key type Signing Key, and paste the same public key. New commits show Verified, as long as their email is a verified email on the account. Some companies require signed commits.
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
Permission denied (publickey) | GitHub doesn't recognize the key | Check the key is on GitHub, list loaded keys with ssh-add -l, and run ssh-add --apple-use-keychain ~/.ssh/id_ed25519 again |
Support for password authentication was removed | HTTPS with the account password | Switch to SSH with git remote set-url, or run gh auth login |
Host key verification failed | GitHub's fingerprint isn't saved or doesn't match | Run ssh -T git@github.com and accept the fingerprint after checking it |
Repository not found | A typo in the URL, or the account has no access | Check git remote -v, the repository name and which account the key belongs to |
! [rejected] main -> main (fetch first) | GitHub has commits the local repository doesn't, such as a README made on the website | git pull --rebase origin main, then git push -u origin main |
For the README case, a plain git pull fails with refusing to merge unrelated histories, because the two histories share no commit. The rebase puts the local commits on top of GitHub's README commit instead. To see exactly which keys SSH tries, run ssh -vT git@github.com (-v for verbose).
Security rules
- Never share, upload or commit the private key (the file without
.pub). - Use a passphrase. The Keychain makes it painless.
- One key per computer. If a laptop is lost, delete only its key on GitHub, and the other computers keep working.
- Turn on 2FA under Settings, then Password and authentication.
Quick reference
| Task | Command |
|---|---|
| Check for keys | ls -la ~/.ssh |
| Generate a key | ssh-keygen -t ed25519 -C "email" |
| Add the key to the agent and Keychain | ssh-add --apple-use-keychain ~/.ssh/id_ed25519 |
| Copy the public key | pbcopy < ~/.ssh/id_ed25519.pub |
| Test the connection | ssh -T git@github.com |
| List loaded keys | ssh-add -l |
| Switch a remote to SSH | git remote set-url origin git@github.com:user/repo.git |
| Log in with the GitHub CLI | gh auth login |
| Create and push a repository | gh repo create name --private --source=. --push |