Sub topic 5
Authenticating: personal access tokens, SSH keys, and the GitHub CLI
This chapter assumes you have Git configured with user.name and user.email, a GitHub account, and an empty GitHub repository whose HTTPS or SSH URL you have copied. Authentication proves to GitHub who you are when you push. The method you pick must match the URL type you chose for the remote.
How the URL decides the method
GitHub no longer accepts your account password for Git operations over HTTPS. Each URL type uses a different credential.
| Remote URL | Credential | Where it is stored |
|---|---|---|
| `https://github.com/USER/REPO.git` | Personal access token (PAT), or a login made by the GitHub CLI | Git credential helper |
| `[email protected]:USER/REPO.git` | SSH key pair | `~/.ssh` and optionally an ssh-agent |
Option 1: Personal access token over HTTPS
A personal access token is a long random string that you use in place of a password. Create one under GitHub Settings, then Developer settings, then Personal access tokens. A fine-grained token limited to one repository with read and write access to Contents is enough for pushing.
On your first push over HTTPS, Git asks for a username and a password. Enter your GitHub username, then paste the token as the password. A credential helper can save it so you are not asked again.
Option 2: SSH keys
An SSH key pair has a private key that stays on your machine and a public key that you give to GitHub. Create the pair, then add the public half to your account.
ssh-keygen -t ed25519 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pubCopy the output of the last command. In GitHub, open Settings, then SSH and GPG keys, then New SSH key, and paste it. Never share the file without the .pub extension.
Test the connection:
ssh -T [email protected]When the setup works, GitHub replies with a greeting that includes your username, such as Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. The message about shell access is normal. The command exits with a non-zero status even on success, so do not read that as a failure.
The first time you connect, SSH asks you to confirm GitHub's host fingerprint. Compare it with the fingerprints published in GitHub's documentation before typing yes.
Option 3: The GitHub CLI
The GitHub CLI (gh) can log you in and set up Git's credentials for you.
gh auth loginThe command asks which host to use, which protocol you prefer (HTTPS or SSH), and how to authenticate. The browser option opens a page where you enter a one-time code. If you choose HTTPS, gh registers itself as a credential helper, so later git push commands over HTTPS work without a token. If you choose SSH, it can upload a public key for you. Check the result with gh auth status.
Common authentication errors
Permission denied (publickey): GitHub does not recognize your key. Runssh-add -lto see whether the agent holds it, then confirm the public key is listed in your account settings.Authentication failedorInvalid username or passwordover HTTPS: you entered your account password, or the token has expired or lacks write access to the repository. Create a new token with the correct scope.Repository not foundon a repository that exists: the credential is valid but has no access to it. This often means the token was limited to other repositories.- Git asks for credentials on every push: no credential helper is storing them. Run
gh auth setup-git, or pick a helper for your operating system.
Exercise
Your remote URL is [email protected]:alice/notes.git. You created a personal access token and a push still fails with Permission denied (publickey). Why, and what are two ways to fix it?
Show answer
An SSH URL uses an SSH key, not a token, so the token is never used. Either add an SSH key to your GitHub account and check it with ssh -T [email protected], or switch the remote to the HTTPS URL with git remote set-url origin https://github.com/alice/notes.git and use the token there.