Git & GitHub · Collaboration

SSH Keys and Tokens

GitHub no longer accepts your account password for git push. Use the browser sign-in, an SSH key or a token instead.

Advanced 5 min Lesson 16 of 19Shows Git 2.47 and GitHub.com as of 2026. GitHub changes its layout from time to time; button names stay the same.
Before you start:Cloning a Repository
0/19 in Git & GitHub
On this page · 5 min · Advanced

Learning objectives#

  • Understand the three ways to authenticate
  • Create and add an SSH key
  • Create a fine-grained personal access token
  • Keep credentials safe

Explanation#

HTTPS + Git Credential Manager (installed with Git for Windows) is the easiest: the first push opens a browser to sign in, and the credential is stored securely.

SSH keys: ssh-keygen -t ed25519 creates a key pair. Add the public key (.pub) in GitHub → Settings → SSH and GPG keys. Clone with git@github.com:… URLs and you are never asked again.

Personal access tokens are passwords for scripts and tools. Prefer fine-grained tokens: limit them to specific repositories, give only the permissions needed (for example Contents: Read and write) and set an expiry date.

Never commit keys or tokens. If one leaks, delete it on GitHub immediately and create a new one.

Step-by-step screens#

Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: Set up SSH and test it#

ShellSet up SSH and test it Illustrative
ssh-keygen -t ed25519 -C "asha@example.com"      # press Enter for the default file
cat ~/.ssh/id_ed25519.pub                         # copy this into GitHub → Settings → SSH and GPG keys
ssh -T git@github.com
git remote set-url origin git@github.com:asha/hello-github.git
Expected output
Hi asha! You've successfully authenticated, but GitHub does not provide shell access.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. ssh-keygen creates a private key and a .pub public key.
  2. Only the .pub file is copied to GitHub.
  3. ssh -T tests the connection.
  4. set-url switches an existing clone from HTTPS to SSH.

Real-world use case#

A deployment script uses a fine-grained token that can only read one repository and expires in 90 days — a leak would expose very little.

Common mistakes#

Sharing the private key

Only id_ed25519.pub goes to GitHub. The file without .pub never leaves your computer.

Classic tokens with every scope and no expiry

They can do anything forever. Use fine-grained tokens with an expiry.

Best practices#

  • HTTPS + browser sign-in for beginners
  • SSH for daily work
  • Fine-grained tokens with expiry for scripts
  • Revoke anything that leaks

Remember this#

Passwords don’t work for git push.

SSH: add the .pub key.

Tokens: fine-grained, few permissions, expiry.

Practice exercise#

Your task: Test whether your SSH key works with GitHub.

Show solution
Shell Illustrative
ssh -T git@github.com

Mini quiz#

SSH Keys and Tokens quizMultiple choice Question 1 of 1

Which file do you add to GitHub?

Score so far: 0/0

Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.