How to Establish a Git Connection: SSH, HTTPS, and What You Need to Know
Git is the backbone of modern version control, but before you can push code, clone repositories, or collaborate with a team, you need to establish a working connection between your local machine and a remote Git host. That setup step trips up more people than it should — not because it's complicated, but because there are multiple methods, each with its own requirements.
Here's what each approach involves and what determines which one fits your workflow.
What "Establishing a Git Connection" Actually Means
When you work with Git locally, everything stays on your machine. A Git connection is what links your local repository to a remote — typically a hosted service like GitHub, GitLab, Bitbucket, or a self-hosted Git server.
That connection lets you:
- Push local commits to the remote
- Pull or fetch updates from the remote
- Clone an entire repository to your machine
The remote is usually defined by a URL, and how that URL is structured tells Git which protocol to use when communicating.
The Two Main Protocols: HTTPS vs SSH
Most Git hosting services support two connection protocols. They both get the job done, but they work differently under the hood.
| Feature | HTTPS | SSH |
|---|---|---|
| URL format | https://github.com/user/repo.git | [email protected]:user/repo.git |
| Authentication | Username + password or token | SSH key pair |
| Firewall friendliness | High (port 443) | Lower (port 22 may be blocked) |
| Credential storage | Requires credential manager | Key stored locally |
| Initial setup effort | Lower | Moderate |
| Long-term convenience | Requires token management | Seamless once keys are in place |
HTTPS is easier to get started with. You authenticate with your username and a personal access token (PAT) — most services have moved away from plain passwords for security reasons. If you're on a corporate network or working behind a restrictive firewall, HTTPS is often the only option that works reliably.
SSH uses a public/private key pair. You generate the pair on your machine, add the public key to your Git hosting account, and from that point on Git authenticates you automatically without prompting for credentials. For developers who push and pull dozens of times a day, SSH tends to feel much smoother.
How to Set Up an HTTPS Connection
- Install Git on your machine if you haven't already
- Clone or initialize a repository
- When prompted, provide your username and a personal access token (generated in your hosting account's security settings — not your login password)
- Use a credential manager (Git Credential Manager is built into many installations) to store credentials so you're not prompted every time
The remote URL looks like:
https://github.com/yourusername/your-repo.git You can set or update the remote with:
git remote add origin https://github.com/yourusername/your-repo.git Or check what remote is already configured:
git remote -v How to Set Up an SSH Connection 🔑
Step 1 — Generate an SSH key pair
Run this in your terminal:
ssh-keygen -t ed25519 -C "[email protected]" This creates two files: a private key (stay on your machine) and a public key (ends in .pub).
Step 2 — Add the public key to your Git host
Copy the contents of the .pub file and paste it into the SSH Keys section of your hosting account settings. Every major Git host has this option under account security or developer settings.
Step 3 — Test the connection
ssh -T [email protected] A successful response confirms the key is recognized.
Step 4 — Use the SSH remote URL
git remote add origin [email protected]:yourusername/your-repo.git SSH Agent: Keeping Things Running Smoothly
If you protected your private key with a passphrase (recommended), you'll need to enter it once per session. The SSH agent stores your decrypted key in memory so subsequent Git operations don't require re-entry:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 On macOS, the system Keychain can handle this automatically. On Windows, OpenSSH is built into newer versions, and Git for Windows includes its own SSH agent. Linux users typically configure this via .bashrc or .zshrc.
Variables That Affect Which Setup Works for You 🖥️
Not every method works equally well for every person or environment. The right approach depends on:
- Network environment — corporate proxies and strict firewalls often block port 22, making SSH impractical without workarounds
- Operating system — SSH agent behavior, credential manager availability, and key storage all vary between Windows, macOS, and Linux
- Technical comfort level — generating key pairs and managing an SSH agent is a one-time setup, but it assumes some familiarity with the command line
- Number of accounts — managing SSH connections to multiple GitHub accounts (personal and work, for example) requires additional SSH config file setup
- CI/CD or automation contexts — automated pipelines often use deploy keys (a restricted SSH key tied to a single repository) or short-lived tokens rather than personal credentials
- Team or organization policies — some teams standardize on one method for consistency and auditing purposes
When Things Go Wrong
Common connection failures often come down to a handful of causes:
- Expired or revoked tokens (HTTPS) — personal access tokens have configurable expiry dates
- Wrong remote URL — mixing up HTTPS and SSH URLs, or a typo in the path
- SSH key not added to the agent — the key exists but isn't loaded in the current session
- Firewall blocking port 22 — GitHub and some other hosts offer SSH over port 443 as a workaround
- Multiple SSH keys without a config file — Git defaults to
~/.ssh/id_rsaorid_ed25519; if you have multiple keys, an~/.ssh/configfile directs the right key to the right host
Diagnosing connection issues usually starts with git remote -v to confirm the URL, then ssh -vT [email protected] for verbose SSH debugging output.
The Part That Depends on Your Setup
The mechanics of both HTTPS and SSH connections are well-documented and consistent. What varies is everything around them — your OS, your network, how many repositories you're managing, whether you're working solo or in a team, and how much command-line setup you're comfortable with upfront versus ongoing credential management later.
Both methods are legitimate and widely used. The difference between them only becomes meaningful when measured against your actual working environment and habits.