Sub topic 3
Authenticating with HTTPS or SSH
This chapter assumes you can run basic Git commands, know what a remote is, and have used git remote -v. Every time Git talks to a remote, the server must decide who you are. The URL you gave the remote decides how that happens.
Two URL styles, two mechanisms
A remote URL starts with either https:// or an SSH form such as git@host:path. The scheme selects the authentication method.
| Property | HTTPS | SSH |
|---|---|---|
| Example URL | `https://example.com/team/project.git` | `[email protected]:team/project.git` |
| Credential | Username plus a token or password | A private key on your machine |
| Setup effort | Low | One-time key setup |
| Works behind strict firewalls | Usually | Sometimes blocked |
| Prompts each time | Only if no credential is cached | Only if the key has a passphrase and no agent is running |
Most hosting services no longer accept account passwords over HTTPS. They expect a personal access token, which is a long random string you generate in the service's settings and use in place of a password.
HTTPS with a credential helper
With HTTPS, Git asks for a username and token on the first request. Without help it asks again every time. A credential helper stores the answer for you.
git config --global credential.helper cache
git config --global credential.helper storeThe first line keeps credentials in memory for a short time. The second writes them in plain text to ~/.git-credentials, so use it only on a machine you fully control. Operating systems also ship secure helpers, such as the macOS Keychain helper or Git Credential Manager on Windows. Prefer those when available.
SSH with a key pair
SSH uses a key pair. The private key stays on your computer. The public key is uploaded to your account on the host. The server proves you own the private key without ever receiving it.
Generate a key, start the agent, and load the key:
ssh-keygen -t ed25519 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pubCopy the output of the last command into the SSH keys page of your hosting account. The agent holds your unlocked key in memory, so you type the passphrase once per session instead of once per push.
Then test the connection:
ssh -T [email protected]A successful test prints a greeting that includes your account name. The first connection also asks you to confirm the host's fingerprint. Compare it with the fingerprint the service publishes before you answer yes.
Switching an existing remote
You do not need to clone again to change method. Change the URL with set-url, which the previous chapter on configuring remotes introduced.
git remote -v
git remote set-url origin [email protected]:team/project.git
git remote -v
git fetch originThe final fetch confirms the new URL and credentials work. Your branches and remote-tracking branches are unchanged, because only the address changed.
Common errors
Authentication failedover HTTPS: the stored token is expired or wrong. Generate a new one and clear the cached entry, or remove the stale line from the helper's store.Permission denied (publickey)over SSH: the server does not recognize your key. Check thatssh-add -llists it and that the matching public key is uploaded.Repository not found: the credentials worked but your account lacks access, or the URL is misspelled. Authentication and authorization are separate checks.- Prompts on every command: no credential helper is configured, or the SSH key is not loaded in the agent.
Exercise
You run git push and get Permission denied (publickey). The remote URL is [email protected]:team/project.git. Name two checks you would make, and one command that switches the remote to HTTPS.
Show answer
Run ssh-add -l to see whether the key is loaded in the agent, and confirm the matching public key (~/.ssh/id_ed25519.pub) is uploaded to your account. You can also test with ssh -T [email protected]. To switch to HTTPS, run git remote set-url origin https://example.com/team/project.git.