Authentication with SSH and tokens

Topic 4 of 4 in Working with remotes8 min readBeginnerFree course

Overview

Anyone can clone a public repository, but pushing — and reading private repositories — requires GitHub to know who you are. Since 13 August 2021, GitHub no longer accepts account passwords for Git operations. You use one of two methods instead: HTTPS with a token (usually managed for you) or SSH keys.

Two ways to prove who you are to GitHub. HTTPS with a credential helper: the URL looks like https://github.com/asha/shop.git; you sign in once in the browser (gh auth login or Git Credential Manager), a token is stored securely for you, and you never type your password into Git. SSH keys: a private key (~/.ssh/id_ed25519) stays on your PC, and its public key (.pub file) is pasted into GitHub; the URL looks like git@github.com:asha/shop.git; create keys with ssh-keygen -t ed25519 -C “asha@…” and test with ssh -T git@github.com. GitHub stopped accepting account passwords for Git operations in 2021 — use one of these instead.

Option 1: HTTPS with a credential helper (easiest)

HTTPS URLs look like https://github.com/asha/shop.git. Instead of a password, Git sends a token. You don’t need to manage the token yourself — a credential helper does it:

GitHub CLI

Install GitHub CLI (gh), then:

Terminal
gh auth login

Choose GitHub.com, HTTPS, and “Login with a web browser”. gh opens a page where you approve access, then configures Git to use it for credentials. From now on, git push just works.

Git Credential Manager

Git for Windows includes Git Credential Manager (GCM), and it’s available for macOS and Linux too. The first time you push to GitHub, it opens a browser window to sign in, then stores the credential in your operating system’s secure store (Windows Credential Manager, macOS Keychain, or the Linux secret service).

Either way: sign in once in the browser, never type a password into Git.

Personal access tokens

Sometimes you need a token directly — on a server, in a script, or with tools that don’t support browser sign-in. Create one at GitHub → Settings → Developer settings → Personal access tokens.

  • Fine-grained tokens (recommended): limited to specific repositories and specific permissions (for example “Contents: read and write”), with a mandatory expiry date.
  • Classic tokens: broad scopes such as repo. Still needed in a few cases fine-grained tokens don’t cover yet.

When Git asks for a password over HTTPS, paste the token instead. Treat tokens exactly like passwords:

  • give them the least permission and shortest life that works;
  • never commit them or paste them into a URL in shared scripts;
  • revoke them as soon as you suspect a leak — GitHub’s secret scanning also detects many leaked GitHub tokens automatically.

In GitHub Actions workflows, use the built-in GITHUB_TOKEN instead of a personal token where possible (GitHub Actions basics).

Option 2: SSH keys

SSH URLs look like git@github.com:asha/shop.git. Authentication uses a key pair:

  • a private key, which never leaves your computer,
  • a public key, which you give to GitHub.

When you connect, SSH proves you have the private key without sending it.

1. Create a key

Terminal
ssh-keygen -t ed25519 -C "asha@example.com"

Accept the default location (~/.ssh/id_ed25519) and set a passphrase — it protects the key if your laptop is stolen. An SSH agent (built into macOS, Windows and most Linux desktops) remembers the passphrase so you don’t type it on every push:

Terminal
ssh-add ~/.ssh/id_ed25519        # add to the agent (macOS: ssh-add --apple-use-keychain …)

Ed25519 is the modern default: short, fast and secure.

2. Give GitHub the public key

Terminal
cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI… asha@example.com

Copy the whole line, then on GitHub go to Settings → SSH and GPG keys → New SSH key and paste it. (Or: gh ssh-key add ~/.ssh/id_ed25519.pub.)

Only ever share the .pub file. The file without .pub is your private key.

3. Test

Terminal
ssh -T git@github.com

The first time, SSH shows GitHub’s host key fingerprint and asks whether to trust it — compare it with the fingerprints GitHub publishes in its documentation, then type yes. On success:

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

Switching a repository between HTTPS and SSH

Terminal
git remote -v
git remote set-url origin git@github.com:asha/shop.git      # to SSH
git remote set-url origin https://github.com/asha/shop.git  # to HTTPS

Which should I choose?

HTTPS + credential helper SSH
Setup sign in once in a browser create a key, paste it into GitHub
Works through strict firewalls yes (port 443) sometimes blocked (port 22; GitHub also offers SSH over port 443)
Same credential for the GitHub API yes, with gh no
Good for servers with fine-grained tokens with dedicated deploy keys

Both are secure. If you’re unsure, use gh auth login with HTTPS. Many developers who already use SSH for servers prefer SSH for GitHub too.

Protecting your account

Your Git credentials are only as safe as your GitHub account. Turn on two-factor authentication (GitHub requires it for anyone who contributes code) — ideally with a passkey or security key — and review your SSH keys and tokens in Settings now and then, removing ones you don’t recognise or no longer use.

If a push asks for a password: you’re on HTTPS without a credential helper. Run gh auth login, or switch the remote to SSH. Never type your account password — it won’t work.

Exercise

Prepare SSH access and switch a remote to SSH

Create an Ed25519 SSH key pair with a comment, in a practice folder so it doesn’t touch your real keys. Print the public key (the part you’d paste into GitHub), and the key’s fingerprint. Then take a repository whose origin uses an HTTPS URL and switch it to the equivalent SSH URL. In real life you’d then add the public key at github.com → Settings → SSH and GPG keys and test with ssh -T git@github.com.

Terminal
mkdir -p practice-ssh
mkdir shop && cd shop && git init -qb main
git remote add origin https://github.com/asha/shop.git
cd ..

# 1. Generate an Ed25519 key pair in practice-ssh/
#    (for real use, keep the default ~/.ssh/id_ed25519 and set a passphrase)

# 2. Show the public key and its fingerprint

# 3. Switch shop's origin from HTTPS to SSH and check

Summary

  • GitHub doesn’t accept your account password for Git operations — use HTTPS with a token or SSH keys.
  • Easiest HTTPS setup: gh auth login or Git Credential Manager signs you in through the browser and stores a token.
  • Personal access tokens replace passwords for HTTPS; prefer fine-grained tokens with an expiry and minimal permissions.
  • SSH uses a key pair: the private key stays on your computer, the public key goes into your GitHub settings.
  • Switch a repository between HTTPS and SSH with git remote set-url.