Sub topic 3
Authenticating to GitHub
This chapter assumes you have Git installed, a GitHub account, and a terminal. It also assumes you know the clone URL formats from the previous chapter. Here you will set up the credentials that let git clone talk to GitHub.
When authentication is needed
Authentication proves who you are to GitHub. Whether you need it depends on the repository and the URL type.
- Cloning a public repository over HTTPS needs no credentials.
- Cloning a private repository needs credentials over both HTTPS and SSH.
- Cloning any repository over SSH needs a registered key, even if the repository is public.
- Pushing to any repository needs credentials.
HTTPS authentication
GitHub does not accept your account password for Git operations over HTTPS. You use one of these instead:
- A personal access token (PAT) is a generated string that you enter where Git asks for a password. You create it in your GitHub account settings and give it only the scopes it needs, such as read access to repository contents.
- A credential helper is a program that stores your credentials so Git does not prompt every time. Git for Windows and macOS installers usually configure one. Git Credential Manager can sign you in through the browser, so you never copy a token by hand.
When Git prompts for a username, enter your GitHub username. When it prompts for a password, paste the token.
git clone https://github.com/octocat/Hello-World.gitSSH authentication
SSH uses a key pair. The private key stays on your machine. The public key is registered with your GitHub account. GitHub checks that you hold the matching private key without the key ever being sent.
Generate a key if you do not have one:
ssh-keygen -t ed25519 -C "[email protected]"Accept the default file location, which is ~/.ssh/id_ed25519, and set a passphrase. Then print the public key and copy it:
cat ~/.ssh/id_ed25519.pubIn GitHub, open Settings, then SSH and GPG keys, then New SSH key, and paste the contents of the .pub file. Never share the file without the .pub extension.
Test the connection before you clone:
ssh -T [email protected]A working setup prints a message like Hi octocat! You've successfully authenticated, but GitHub does not provide shell access. That message is a success, even though the command exits with a non-zero status. Now clone:
git clone [email protected]:octocat/Hello-World.gitOn the first connection, SSH asks you to confirm GitHub's host fingerprint. Compare it with the fingerprints GitHub publishes in its documentation before typing yes.
Authenticating with the GitHub CLI
The GitHub CLI can handle credentials for you. gh auth login walks you through choosing HTTPS or SSH and signing in through the browser. It can also upload an SSH key and set up Git to use its credentials.
gh auth login
gh repo clone octocat/Hello-WorldComparing the methods
| Method | Setup effort | Works for public repos without login | Good fit |
|---|---|---|---|
| HTTPS, no credentials | None | Yes | One-off read-only clones |
| HTTPS with token or helper | Low | Yes | Locked-down networks that allow only port 443 |
| SSH key | Medium | No, a key is always needed | Daily work on many repositories |
| GitHub CLI | Low | Yes | Starting quickly on a new machine |
Exercise
You can clone a public repository over HTTPS without logging in, but the SSH clone of the same repository fails with "Permission denied (publickey)". Why, and what is the quickest way to check your setup?
Show answer
SSH always requires a key that is registered with a GitHub account, even for public repositories. HTTPS allows anonymous reads on public repositories. Run ssh -T [email protected]. If it does not greet you by username, generate a key with ssh-keygen, add the public key to GitHub, and test again.