Authentication: SSH and HTTPS
Introduction
Pushing and pulling requires proving you own the account. SSH keys and HTTPS with Personal Access Tokens (PAT) are the two standard methods on GitHub, GitLab, and Gitee. This chapter sets up SSH, configures credential storage for HTTPS, and lists security habits so tokens never land in Git history.
Prerequisites
- Remote Repositories
- Terminal access on your machine
- Account on your Git hosting platform
SSH vs HTTPS
| SSH | HTTPS | |
|---|---|---|
| URL form | git@github.com:user/repo.git | https://github.com/user/repo.git |
| Auth | SSH key pair | Username + PAT (not password on GitHub) |
| Setup | One-time key upload | Token + credential helper |
| Firewall | Port 22 (sometimes blocked) | Port 443 (usually open) |
Pick one per machine—SSH is common for daily dev.
Generate an SSH Key
ssh-keygen -t ed25519 -C "your.email@example.com"Press Enter for default path ~/.ssh/id_ed25519; optional passphrase adds extra security.
Code explanation:
- ed25519 is modern default—use
-t rsa -b 4096only if old server requires RSA
Start agent and add key (macOS/Linux):
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519Add Public Key to GitHub
Copy public key:
cat ~/.ssh/id_ed25519.pubGitHub → Settings → SSH and GPG keys → New SSH key → paste → save.
Test:
ssh -T git@github.comSuccess message:
Hi username! You've successfully authenticated...Clone and Push with SSH
git clone git@github.com:username/repo.git
git push origin mainNo password prompt when key is loaded.
HTTPS with Personal Access Token
GitHub disabled password auth for Git operations—use a PAT.
Create token:
- Settings → Developer settings → Personal access tokens
- Fine-grained or classic token with
reposcope (adjust minimally) - Copy token once—it will not show again
Clone:
git clone https://github.com/username/repo.gitWhen prompted:
- Username: GitHub username
- Password: paste PAT (not account password)
Credential Helper
Avoid retyping PAT:
git config --global credential.helper storeOr on macOS:
git config --global credential.helper osxkeychainWindows Git for Windows often sets manager automatically.
Code explanation:
- Helper saves credentials securely on disk or keychain—still protect your machine
Warning
Never Commit Tokens
Do not put PATs in .env, scripts, or repos. Use environment variables in CI (chapter 22).
Switch Remote URL
From HTTPS to SSH:
git remote set-url origin git@github.com:username/repo.gitVerify:
git remote -vMultiple GitHub Accounts (Awareness)
Use different SSH keys and ~/.ssh/config:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_workClone as git@github-work:org/repo.git.
Gitee / GitLab
Same SSH pattern—upload id_ed25519.pub in account SSH settings. HTTPS uses platform-specific tokens with equivalent scopes.
Security Checklist
- Passphrase on SSH key for laptops
- PAT scoped to minimum permissions
- Rotate token if leaked
- Enable 2FA on hosting account
-
.gitignoresecrets; use.gitignorechapter patterns
FAQ
Permission denied (publickey)?
Agent not running, wrong key, or public key not uploaded—run ssh-add -l and retest ssh -T.
Token worked once then failed?
Expired or revoked token—generate new PAT.
Corporate proxy blocks SSH?
Use HTTPS URL + credential helper.
Shared CI machine?
Use deploy keys or CI secrets—not your personal PAT in repo files.
git@github.com vs https?
Same repo, different transport—choose based on auth preference.
Commit email vs login?
Git commit uses user.email from config—can differ from GitHub noreply email.