Sub topic 4
Choosing between HTTPS and SSH URLs
This chapter assumes you have Git installed with user.name and user.email set, a local repository with at least one commit, a GitHub account, and an empty repository on GitHub. You will learn what the two URL forms look like, how each one authenticates, and how to pick one.
Two URLs for the same repository
Every GitHub repository has two addresses that Git can use. They point to the same data. The difference is the protocol, which decides how Git proves who you are.
| Row | HTTPS | SSH |
|---|---|---|
| Example | `https://github.com/YOUR-USERNAME/YOUR-REPO.git` | `[email protected]:YOUR-USERNAME/YOUR-REPO.git` |
| Credential | Personal access token or credential helper login | SSH key pair |
| Setup effort | Low | Medium, one time per machine |
| Works through strict firewalls | Usually yes (port 443) | Sometimes blocked (port 22) |
The SSH form is not a normal URL. The part before @ is the user, which is always git on GitHub. The part after : is the path to the repository. You do not log in as your own username. GitHub identifies you by your key.
How each option authenticates
HTTPS
GitHub no longer accepts your account password for Git operations over HTTPS. You use a personal access token instead, or let a helper handle it. Git prompts for a username and a password. You enter your GitHub username and paste the token as the password.
Typing a token on every push is tedious, so Git delegates storage to a credential helper. Git for Windows and macOS installers normally configure one. Signing in with the GitHub CLI (gh auth login) can also set this up for you. The next chapter covers these methods in detail.
SSH
SSH uses a key pair. The private key stays on your machine. You upload the public key to your GitHub account settings. When you push, your machine proves it holds the private key, and no secret travels over the network. After setup there are no prompts, unless you protect the key with a passphrase and your SSH agent has not loaded it.
You can test the connection before adding any remote:
ssh -T [email protected]When setup is correct, GitHub replies with a greeting that includes your username. It then closes the connection, because GitHub does not give you a shell. That is expected and not an error.
Choosing
Use these criteria:
- Pick HTTPS if you want the fastest start, work on a locked-down network that blocks port 22, or use a machine where you do not want to keep a private key.
- Pick SSH if you push often, work on several repositories, and want no token to store or rotate. It suits your own long-lived workstation.
- Pick either when unsure. The choice is not permanent.
Switching later costs one command. You change the URL of the existing remote and keep all your history and branches:
git remote set-url origin https://github.com/YOUR-USERNAME/YOUR-REPO.gitRunning git remote -v afterward shows the new fetch and push URLs.
Common mistakes
- Mixing the forms.
[email protected]/user/repo.git(slash instead of colon) is invalid. The SSH form needs the colon. - Using SSH without a key. The error
Permission denied (publickey)means GitHub did not recognize any key your machine offered. Runssh -T [email protected]to check. - Using an account password over HTTPS. Git reports an authentication failure. Use a token.
- Forgetting that the remote stores one URL. If a tool or teammate expects the other form, the remote must be changed with
set-url.
Exercise
Your office network blocks outbound port 22, and your push over [email protected]:me/app.git hangs. Which change fixes it with the least effort, and which command applies it?
Show answer
Switch the remote to the HTTPS URL, which uses port 443, with git remote set-url origin https://github.com/me/app.git. Then authenticate with a personal access token or a credential helper. History and branches stay unchanged.
Exercise
What does the git in [email protected] stand for, and why does everyone use the same one?
Show answer
It is the SSH user name that GitHub's server accepts for all accounts. GitHub tells users apart by the public key they registered, not by this name.