Sub topic 16
Choosing between HTTPS and SSH
This chapter assumes you can clone, commit, push, and pull. It also assumes you have seen both remote URL forms and know that HTTPS uses a personal access token and SSH uses a key pair. Both methods authenticate you to GitHub. Authorization, meaning what you may do in each repository, comes from your account's permissions and from the token's scopes, not from the transport you picked.
What actually differs
The two methods differ in what you store, where it lives, and which networks they work on.
| Factor | HTTPS with token | SSH with key |
|---|---|---|
| Remote URL | `https://github.com/OWNER/REPO.git` | `[email protected]:OWNER/REPO.git` |
| Secret you hold | Token string, stored by a credential helper | Private key file, optionally passphrase-protected |
| Setup effort | Create a token, configure a helper | Generate key, load agent, upload public key |
| Scope control | Per token, by scope and expiration | Per key, all-or-nothing for your account (deploy keys are per repository) |
| Network | Port 443, works through most proxies | Port 22, or port 443 through `ssh.github.com` |
| Revocation | Delete the token in settings | Delete the key in settings |
Decision criteria
Pick HTTPS with a token when:
- You are on a locked-down network where port 22 is blocked and you do not want to edit
~/.ssh/config. - You want a credential that expires on a date you choose and carries limited scopes.
- You already use GitHub CLI, since
gh auth loginconfigures a credential helper for you.
Pick SSH when:
- You push often and want no token to rotate. A key stays valid until you remove it.
- You use several accounts or hosts and want to select keys per host in
~/.ssh/config. - You want the secret protected by a passphrase held in ssh-agent.
Use a deploy key when a server or CI job needs access to one repository only. It is not tied to a person.
Switching without recloning
The choice is not permanent. The method is set by the remote URL, so you can change it for an existing clone.
git remote -v
git remote set-url origin [email protected]:OWNER/REPO.git
git remote -vCheck the output of git remote -v afterward. Then run ssh -T [email protected] to confirm the key works before your next push.
Common mistakes
- Mixing methods by accident. A cloned HTTPS URL keeps asking for a token even after you set up SSH. The URL decides, not your setup.
- Giving a token more scope than the task needs.
- Setting up a key but forgetting to add the public key to GitHub, which produces
Permission denied (publickey). - Using a personal key as a deploy key on a shared server. Anyone with that server then holds access to your whole account.
Exercise
A CI server must pull one private repository and nothing else. Which method fits, and why not your personal SSH key?
Show answer
Use a deploy key, which grants access to a single repository and is not tied to a person. A personal SSH key would give the server access to every repository your account can reach, and removing it later would also cut off your own access.
Exercise
You set up an SSH key and verified it with ssh -T, but git push still asks for a username and password. What is the likely cause and the fix?
Show answer
The clone's remote still uses an HTTPS URL. Run git remote set-url origin [email protected]:OWNER/REPO.git, then check it with git remote -v.