Sub topic 2
HTTPS and SSH remote URLs
This chapter assumes you can clone, commit, push, and pull, and that you are comfortable in a terminal. It covers how a remote's URL decides which authentication method Git uses, and how to change it.
The URL picks the method
A remote is a named URL that Git talks to. GitHub offers two URL forms for the same repository, and each one uses a different login method.
| Form | Example | Credential Git uses |
|---|---|---|
| HTTPS | `https://github.com/OWNER/REPO.git` | Username and a personal access token |
| SSH | `[email protected]:OWNER/REPO.git` | An SSH key pair |
Both forms point at the same repository. Your permissions do not change. Only the way you prove who you are changes.
In the SSH form, git is the user name on GitHub's server. It is the same for everyone. GitHub identifies you by your key, not by that user name.
Check which one a repository uses
Run git remote -v inside a repository. It lists each remote with its fetch and push URLs. The start of the URL tells you the method: https:// means HTTPS, and [email protected]: means SSH.
A fresh clone uses whichever URL you cloned from. If you copied the HTTPS link, the remote is HTTPS, even if you have an SSH key set up.
Switch with git remote set-url
You do not need to clone again to change methods. Update the existing remote:
git remote -v
git remote set-url origin [email protected]:OWNER/REPO.git
git remote set-url origin https://github.com/OWNER/REPO.gitThe first command shows the current URLs. The second switches origin to SSH. The third switches it back to HTTPS. Replace OWNER/REPO with the real account and repository name.
Run git remote -v again after each change to confirm it. The switch only edits the URL stored in .git/config. Your commits, branches, and history are untouched.
Common errors
- Changing the URL but not the credentials. After switching to SSH, a token does nothing. You must have a registered SSH key. After switching to HTTPS, your SSH key is ignored and Git asks for a token.
- Typing the wrong form. SSH URLs use a colon between host and path (
github.com:OWNER). Writing a slash there is a different, invalid URL. - Assuming one setting covers all repositories. The remote URL is stored per repository. You can use HTTPS in one project and SSH in another.
- Typing your account password over HTTPS. GitHub no longer accepts account passwords for Git operations. HTTPS needs a token, which the next chapters cover.
Exercise
A repository's git remote -v output shows [email protected]:acme/app.git. You want to use HTTPS with a token instead. Which command do you run, and do you need to clone again?
Show answer
Run git remote set-url origin https://github.com/acme/app.git. You do not need to clone again, because the command only changes the stored URL. You then need a personal access token for HTTPS.