Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Authentication: SSH Keys, Tokens and Credential Managers

Learn how Git authenticates to GitHub over HTTPS and SSH, generate an Ed25519 key, use fine-grained personal access tokens safely and fix permission denied errors.

When you push to a private repository, GitHub must know who you are. Git offers two transports, HTTPS and SSH, and each has its own way to prove your identity. GitHub stopped accepting account passwords for Git operations, so you will use either a credential manager (or token) over HTTPS, or an SSH key. Neither is hard once you see the idea behind it.

HTTPS or SSH?

HTTPS SSH
URL looks like https://github.com/owner/repo.git [email protected]:owner/repo.git
Proves identity with A token (or browser sign-in through a credential manager) A private key on your machine
Works through strict firewalls Usually (port 443) Sometimes blocked (port 22; GitHub also offers 443 via ssh.github.com)
Good for Quick start, CI, locked-down networks Daily development without prompts

You can switch an existing clone any time with git remote set-url origin <new-url>.

HTTPS with Git Credential Manager

Git for Windows and macOS installers include Git Credential Manager. The first time you push, it opens a browser window, you sign in to GitHub (with 2FA), and it stores a token in the system keychain. After that, pushes just work. On Linux you can install the same tool, or use gh auth login, which configures Git to use the GitHub CLI as a credential helper (see The GitHub CLI).

SSH keys step by step

SSH uses a key pair. The private key stays on your computer and is never shared. The public key is uploaded to GitHub. When you connect, GitHub challenges your computer to prove it holds the matching private key, without the key ever being sent.

  1. Generate an Ed25519 key (modern, short and fast):

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

    Accept the default location (~/.ssh/id_ed25519) and choose a passphrase. The passphrase encrypts the private key on disk, so a stolen laptop file is not enough.

  2. Load it into the agent so you type the passphrase once per session:

    Terminal
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    
  3. Copy the public key (the file ending in .pub) and add it under Settings > SSH and GPG keys > New SSH key. Paste the single line that starts with ssh-ed25519.

  4. Test the connection:

    Terminal
    ssh -T [email protected]
    

    The first time, SSH asks you to confirm GitHub's host fingerprint; compare it with the fingerprints GitHub publishes in its documentation before typing yes. Success prints a greeting such as Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

  5. Use the SSH URL for your remote: git clone [email protected]:owner/repo.git.

Never upload, email or commit the file without .pub. If a private key leaks, delete the matching public key from your GitHub settings immediately and generate a new pair.

Fingerprints

You can inspect a key you uploaded, or compare it with GitHub's list, by its fingerprint: a short hash of the public key. This stand-in shows the idea with a SHA-256 hash of the key text, encoded the way OpenSSH displays it. The key below is a made-up example, not a real one.

JS Stand-in: key fingerprint

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Personal access tokens

A personal access token (PAT) is a long random string you use instead of a password, for scripts, CI jobs and API calls. Create one under Settings > Developer settings > Personal access tokens. Prefer fine-grained tokens:

  • Limit them to specific repositories instead of everything you own.
  • Grant only the permissions needed (for example, Contents: read and write).
  • Give them an expiration date.

Classic tokens exist too, with broad scopes such as repo; avoid them when a fine-grained token will do. A token is a secret exactly like a password: keep it out of source code, environment dumps and chat messages. GitHub scans public repositories for leaked tokens, but treat that as a safety net, not a plan. Tokens that must be revoked can be deleted from the same settings page at any time.

Deploy keys and other credentials

  • A deploy key is an SSH key attached to a single repository (read-only by default). Servers that only need to pull code should use one, rather than a person's key.
  • GitHub Actions workflows receive a short-lived GITHUB_TOKEN automatically (see the Actions chapter).
  • GitHub Apps act with their own identity and narrowly scoped permissions; use them for bots and integrations.

Troubleshooting

Symptom Likely cause Fix
Permission denied (publickey) Key not added to GitHub, or not loaded in the agent ssh-add -l to list loaded keys; add the .pub to GitHub
Repository not found on a private repo Authenticated as the wrong account ssh -T [email protected] shows who GitHub thinks you are
Prompted for a password over HTTPS The credential helper is missing or token expired Install Git Credential Manager or run gh auth login
Too many authentication failures Several keys offered in a row Set IdentityFile and IdentitiesOnly yes for github.com in ~/.ssh/config

A minimal ~/.ssh/config entry:

TEXT
Host github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file do you upload to GitHub when adding an SSH key?

    Only the public key is uploaded. The private key never leaves your machine.
  2. 2. Why use a fine-grained personal access token instead of a classic one?

    Fine-grained tokens follow least privilege and have expirations.
  3. 3. ssh -T [email protected] prints 'Permission denied (publickey)'. What should you check first?

    That error means no offered key was accepted. List keys with ssh-add -l and check Settings > SSH keys.
  4. 4. What is the main benefit of a passphrase on your SSH key?

    A stolen private key file is useless without the passphrase.

Summary

  • GitHub no longer accepts account passwords for Git; use a credential manager, a token, or SSH.
  • SSH uses a key pair: keep the private key secret, upload only the .pub file, and protect it with a passphrase.
  • Prefer fine-grained tokens with a short expiry and minimal permissions.
  • ssh -T [email protected] tells you who GitHub thinks you are.
  • Use deploy keys, Actions tokens or GitHub Apps for automation instead of personal credentials.

Next: The GitHub CLI