To connect to GitHub with Git, you choose between HTTPS (using a personal access token) and SSH (using a key pair). Both work reliably; HTTPS is simpler for beginners, while SSH avoids re-entering credentials after setup. This guide explains the difference, how to set up either method, and how to switch between them.
Background: How Git Authentication Works on GitHub
Git itself doesn’t handle authentication to remote servers like GitHub. Instead, when you run git push or git pull, Git hands the request to the remote URL you’ve configured. That URL determines which protocol is used:
- HTTPS URLs look like
https://github.com/username/repo.git. They use standard web authentication (username plus a password or token). - SSH URLs look like
git@github.com:username/repo.git. They use cryptographic key pairs, so no password is sent over the network.
GitHub deprecated password authentication for HTTPS in August 2021. You must now use a personal access token (PAT) as the password, or switch to SSH. This change pushed many users to SSH, but HTTPS with a token remains fully supported and is often easier for those unfamiliar with key management.
Choosing Between HTTPS and SSH
Your choice depends on your workflow and comfort level. Consider these factors:
- Ease of setup: HTTPS requires creating a token in your GitHub settings (a few clicks) and entering it once. SSH requires generating a key pair, adding the public key to GitHub, and configuring your SSH agent.
- Credential storage: With HTTPS, Git can cache your token using a credential helper (e.g., Git Credential Manager on Windows,
osxkeychainon macOS). With SSH, your private key passphrase can be stored in an SSH agent, so you don’t type it repeatedly. - Firewall and proxy friendliness: HTTPS uses port 443, which is almost always open. SSH typically uses port 22, which some corporate networks block. GitHub also offers SSH over port 443 for such cases.
- Security model: SSH keys are more resistant to phishing than passwords/tokens because they never leave your machine. However, a stolen private key (without a passphrase) grants access, so protecting your key is critical.
Setting Up HTTPS Access
To use HTTPS with a personal access token:
- On GitHub, go to Settings → Developer settings → Personal access tokens → Tokens (classic).
- Click Generate new token, select scopes (at minimum
repofor private repositories), and set an expiration date. - Copy the token immediately — you won’t see it again.
- When cloning, use the HTTPS URL:
git clone https://github.com/username/repo.git. - When prompted for a username, enter your GitHub username. For password, paste the token (not your account password).
- To avoid re-entering the token, enable a credential helper:
git config --global credential.helper cache(caches in memory) orstore(saves to disk, less secure). On Windows, Git Credential Manager is often pre-installed and handles this automatically.
Setting Up SSH Access
SSH setup involves creating a key pair and adding the public key to GitHub:
- Open a terminal and run
ssh-keygen -t ed25519 -C "your_email@example.com". Press Enter to accept the default file location (~/.ssh/id_ed25519). You can set a passphrase for extra security. - Start the SSH agent:
eval "$(ssh-agent -s)"(Linux/macOS) or ensure the service is running on Windows. - Add your private key:
ssh-add ~/.ssh/id_ed25519. - Copy the public key:
cat ~/.ssh/id_ed25519.puband copy the output. - On GitHub, go to Settings → SSH and GPG keys → New SSH key, paste the key, and save.
- Test the connection:
ssh -T git@github.com. You should see a message like “Hi username! You’ve successfully authenticated…” - Clone using the SSH URL:
git clone git@github.com:username/repo.git.
Switching Between HTTPS and SSH for an Existing Repository
If you already have a local clone and want to change the remote URL, use git remote set-url:
- To switch to HTTPS:
git remote set-url origin https://github.com/username/repo.git - To switch to SSH:
git remote set-url origin git@github.com:username/repo.git
You can verify the change with git remote -v. This is useful if you initially used HTTPS but later prefer SSH, or vice versa.
Common Problems and Fixes
Here are typical issues users encounter:
- HTTPS asks for password repeatedly: Ensure the credential helper is configured. On Windows, run
git config --global credential.helper manager-core. On macOS, useosxkeychain. If you still get prompted, your token may be expired — generate a new one. - SSH permission denied (publickey): Check that your public key is added to GitHub and that your SSH agent is running. Run
ssh -T git@github.comto see the error. Also verify the key is loaded withssh-add -l. - Port 22 blocked: Use SSH over HTTPS port 443 by editing
~/.ssh/configand adding:Host github.com
Hostname ssh.github.com
Port 443 - Token in plaintext: Avoid storing tokens in the remote URL (e.g.,
https://token@github.com/...). Use a credential helper instead.
Security Best Practices
Both methods are secure if used correctly, but follow these guidelines:
- Use a passphrase for your SSH private key. If someone steals the key file, they still need the passphrase.
- Set an expiration date on personal access tokens and rotate them regularly.
- Use the least privilege scopes for tokens — only what you need (e.g.,
repofor private repos,public_repofor public only). - Never share your private key or token. GitHub will never ask for them via email.
- If you lose a device, revoke the SSH key or token in GitHub settings immediately.
Frequently Asked Questions
Which is faster: HTTPS or SSH?
There’s no meaningful speed difference for typical Git operations. Both use the same underlying Git protocol; the only difference is the transport layer. Network latency and repository size matter far more.
Can I use both HTTPS and SSH on the same machine?
Yes. You can have different repositories using different protocols. Git stores the remote URL per repository, so you can clone one with HTTPS and another with SSH. You can also switch a single repo’s remote URL at any time.
What is a personal access token and why do I need it?
A personal access token is a string that acts as a password for GitHub’s API and Git over HTTPS. It’s more secure than your account password because you can limit its scopes and set an expiration. Since GitHub removed password authentication, tokens are the only way to use HTTPS.
How do I know if my SSH key is working?
Run ssh -T git@github.com. If successful, you’ll see a welcome message with your username. If you get a permission denied error, check that your public key is added to GitHub and that your SSH agent has the private key loaded.
Do I need to set up SSH for every repository?
No. SSH setup is global — once your key is added to GitHub, any repository you clone with an SSH URL will work. You don’t need to repeat the key generation or upload process for each repo.
What happens if my token expires?
Git will start asking for credentials again. You’ll need to generate a new token and update your credential helper (or re-enter it when prompted). For SSH, keys don’t expire, but you can revoke them manually.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Samsung Smart Speaker Rollable Display: Facts
- HP 9730e: Wide Prints, Not A3 Scans
- What 8888 Does When You Use It as DNS Server
- Credential Manager: Seamless Sign-In Explained
- API Governance for Public Data: What Growing Portfolios Need
- Enterprise AI Hidden Cost: Data Architecture ROI