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:

Output
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:

SSHHTTPS + token
Remote URLgit@github.com:jknithin36/repo.githttps://github.com/jknithin36/repo.git
Proof of identityA key pair on the MacA personal access token, saved by a credential helper
SetupOnce, about 5 minutesQuick with the GitHub CLI
ExpiryNone (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.

FileWhat it isShare it?
id_ed25519.pubThe public key, like a padlock: copies can be handed outYes, give it to GitHub
id_ed25519The private key, the only key that fits that padlockNever. 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

Terminal
ls -la ~/.ssh

If 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

Terminal
ssh-keygen -t ed25519 -C "your-github-email@example.com"
  • -t ed25519 is the key type: modern, secure and short. Older guides use rsa, which still works.
  • -C adds a label to tell keys apart. The email is the usual choice.

It asks three questions:

Output
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.

Output
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.com

3.Let macOS remember the passphrase

The ssh-agent is a background program that holds the unlocked key in memory. Make sure it's running:

Terminal
eval "$(ssh-agent -s)"

Open the SSH config file, creating it if needed:

Terminal
touch ~/.ssh/config
code ~/.ssh/config

Add this and save:

~/.ssh/config
Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519
LineMeaning
Host github.comThese settings apply when connecting to GitHub
AddKeysToAgent yesLoad the key into the agent automatically
UseKeychain yesKeep the passphrase in the macOS Keychain (Mac only)
IdentityFileWhich private key to use

Then add the key to the agent and the Keychain:

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

After 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:

Terminal
pbcopy < ~/.ssh/id_ed25519.pub

pbcopy is the Mac's copy-to-clipboard command. The key is one line of text, safe to share:

~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your-github-email@example.com

Then on GitHub:

  1. Click the profile picture (top right), then Settings.
  2. In the sidebar, click SSH and GPG keys, then New SSH key.
  3. Title: the computer's name, like MacBook Pro 2026. Key type: Authentication Key.
  4. Paste the key (Cmd + V) and click Add SSH key. GitHub may ask for the password or 2FA.

5.Test the connection

Terminal
ssh -T git@github.com

The first time, SSH asks about GitHub itself:

Output
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.

Output
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

  1. Click + (top right), then New repository.
  2. Name it git-learning, and choose Private for practice.
  3. 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:

Terminal
cd ~/Desktop/git-practice
git remote add origin git@github.com:jknithin36/git-learning.git
git push -u origin main
Output
To 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:

Terminal
gh auth login

Choose 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:

Terminal
gh repo create git-learning --private --source=. --remote=origin --push

Switching a repository from HTTPS to SSH

A repository cloned with an https:// URL keeps asking for credentials. Point it at the SSH URL instead:

Terminal
git remote -v
git remote set-url origin git@github.com:jknithin36/old-project.git
git remote -v
Output
origin  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:

Terminal
gh auth login

Choose 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:

Terminal
ssh-keygen -t ed25519 -C "you@work.com" -f ~/.ssh/id_ed25519_work

Add the new .pub to the work account, then give it a nickname in ~/.ssh/config:

~/.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 yes

github-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):

Terminal
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:

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

Then 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

ErrorCauseFix
Permission denied (publickey)GitHub doesn't recognize the keyCheck 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 removedHTTPS with the account passwordSwitch to SSH with git remote set-url, or run gh auth login
Host key verification failedGitHub's fingerprint isn't saved or doesn't matchRun ssh -T git@github.com and accept the fingerprint after checking it
Repository not foundA typo in the URL, or the account has no accessCheck 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 websitegit 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

TaskCommand
Check for keysls -la ~/.ssh
Generate a keyssh-keygen -t ed25519 -C "email"
Add the key to the agent and Keychainssh-add --apple-use-keychain ~/.ssh/id_ed25519
Copy the public keypbcopy < ~/.ssh/id_ed25519.pub
Test the connectionssh -T git@github.com
List loaded keysssh-add -l
Switch a remote to SSHgit remote set-url origin git@github.com:user/repo.git
Log in with the GitHub CLIgh auth login
Create and push a repositorygh repo create name --private --source=. --push