Sub topic 2
Finding the Clone URL: HTTPS or SSH
This chapter assumes you have Git installed, a GitHub account, and a terminal. You should know what a commit and a branch are. Authentication setup is covered in the next chapter, so here you only choose and find the URL.
What a clone URL is
A clone URL is the address Git uses to reach a repository on a remote server. git clone needs one argument: this address. GitHub gives every repository an HTTPS URL and an SSH URL. Both point to the same repository. They differ in how Git connects and proves who you are.
Where to find the URL
- Open the repository page on GitHub.
- Click the green Code button above the file list.
- Pick the HTTPS or SSH tab.
- Copy the URL shown, using the copy icon.
The GitHub CLI tab also appears there. It is covered in a later chapter.
The two URL formats
| Protocol | Format | Example |
|---|---|---|
| HTTPS | `https://github.com/OWNER/REPO.git` | `https://github.com/octocat/Hello-World.git` |
| SSH | `[email protected]:OWNER/REPO.git` | `[email protected]:octocat/Hello-World.git` |
OWNER is a user or organization name. REPO is the repository name. In the SSH form, git is the user name on the GitHub server. It is always git, not your own GitHub name. A colon separates the host from the path, not a slash.
git clone https://github.com/octocat/Hello-World.gitgit clone [email protected]:octocat/Hello-World.gitHow the two differ
With HTTPS, Git talks over the standard web port. GitHub does not accept your account password for Git operations. You authenticate with a personal access token, or through a credential helper that stores one for you. Public repositories can be cloned over HTTPS with no credentials at all.
With SSH, you create a key pair and register the public key with your GitHub account. After that, Git authenticates with the private key and does not prompt for a password each time, unless you protected the key with a passphrase.
Choosing between them
- Choose HTTPS if you want the quickest start, only clone public repositories, or work on a network that blocks SSH traffic. Firewalls often allow HTTPS when they block SSH.
- Choose SSH if you push often and want to avoid repeated credential prompts, or if you already manage SSH keys.
The choice is not permanent. The URL is stored as the origin remote, and you can change it later:
git remote set-url origin [email protected]:octocat/Hello-World.gitCommon mistakes
- Using the browser address, such as
https://github.com/octocat/Hello-World. This often works because GitHub accepts it, but copying from the Code button is safer. - Writing the SSH URL with a slash, as in
[email protected]/octocat/Hello-World.git. The colon is required. - Replacing
git@with your own user name.
Exercise
A teammate gives you [email protected]/acme/tools.git and the clone fails. What is wrong, and what is the correct URL?
Show answer
The SSH URL uses a slash between the host and the path. It needs a colon, so the correct URL is [email protected]:acme/tools.git.
Exercise
You cloned over HTTPS and now want to push with SSH. Which command switches the existing clone, and do you need to clone again?
Show answer
Run git remote set-url origin [email protected]:OWNER/REPO.git. You do not need to clone again, because only the stored address of the origin remote changes.