Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Git Clone Over SSH Failing When HTTPS Works

· 5 min read · Home Tech

In this article
  1. Why SSH and HTTPS Use Different Paths
  2. Step 1: Verify SSH Can Reach the Server
  3. Step 2: Check Your SSH Keys and Agent
  4. Step 3: Test the Port and Firewall
  5. Step 4: Check the Remote URL and Host Key
  6. Step 5: Use SSH Agent Forwarding (If Needed)
  7. Frequently Asked Questions

If git clone over SSH fails with a permission denied or connection error, but the same repository clones fine over HTTPS, the problem is almost always SSH key authentication or the SSH client configuration, not Git itself. This guide walks through the most common causes and the exact commands to diagnose and fix each one.

Why SSH and HTTPS Use Different Paths

Git over HTTPS authenticates with a username and password or a personal access token, which is sent over the network. Git over SSH uses the SSH protocol, which authenticates with a cryptographic key pair stored on your machine. The server checks your public key, and your client proves it holds the matching private key. If the key is missing, misnamed, or not offered to the server, the connection is rejected — even though HTTPS works fine because it uses a completely separate credential mechanism.

Another difference is the port. HTTPS uses port 443 (almost always open), while SSH uses port 22 (sometimes blocked by firewalls or corporate networks). A blocked port can cause a timeout or “connection refused” error even when your keys are perfect.

Step 1: Verify SSH Can Reach the Server

Before touching keys, test the SSH connection itself. Run this (replace github.com with your Git host):

ssh -T git@github.com

If you see a message like “Hi username! You’ve successfully authenticated,” your keys work and the issue is elsewhere. If you get Permission denied (publickey), the server didn’t accept your key. If you get Connection timed out or Connection refused, the problem is network or firewall related.

For GitHub, GitLab, and Bitbucket, the test command is the same but the response text differs slightly. Check your host’s documentation for the exact expected message.

Step 2: Check Your SSH Keys and Agent

If you get a publickey error, your key isn’t being offered or isn’t registered. Check what keys exist:

ls -la ~/.ssh

Look for files like id_ed25519 or id_rsa (private) and the matching .pub (public). If you don’t have a key pair, generate one:

ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept the default location. Then add the key to your SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Now copy the public key and add it to your Git host account (e.g., GitHub → Settings → SSH and GPG keys → New SSH key). The command to copy is:

cat ~/.ssh/id_ed25519.pub

If you have multiple keys, the agent might be offering the wrong one. You can specify which key to use for a specific host by editing ~/.ssh/config:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Step 3: Test the Port and Firewall

If SSH times out, the port is likely blocked. Test connectivity to port 22:

ssh -vT git@github.com

The verbose output shows where it hangs. If it stalls after “Connecting to github.com port 22,” the port is blocked. Many firewalls allow port 443, and most Git hosts offer SSH over port 443 as a fallback. For GitHub, edit ~/.ssh/config:

Host github.com
  HostName ssh.github.com
  Port 443
  User git

Then test again with ssh -T git@github.com. If that works, update your remote URL to use ssh.github.com instead of github.com (or use the config above to keep the hostname the same).

Step 4: Check the Remote URL and Host Key

Sometimes the remote URL is malformed. For SSH, the correct format is git@host:user/repo.git (not https://...). Check your remote:

git remote -v

If it shows an HTTPS URL, you can switch to SSH:

git remote set-url origin git@github.com:user/repo.git

Another common issue is a changed host key. If the server’s key has changed (e.g., after a reinstall), SSH will refuse to connect. The error will say “REMOTE HOST IDENTIFICATION HAS CHANGED.” To fix, remove the old key from ~/.ssh/known_hosts:

ssh-keygen -R github.com

Then reconnect and accept the new key.

Step 5: Use SSH Agent Forwarding (If Needed)

If you’re cloning on a remote server (like a VPS) and your keys are on your local machine, you need agent forwarding. On your local machine, add ForwardAgent yes to ~/.ssh/config for that server. Then when you SSH into the server, your local keys are available for Git. This is a common pitfall when working on a development server that has no keys of its own.

Frequently Asked Questions

Why does HTTPS work but SSH says “Permission denied (publickey)”?

HTTPS uses a username/password or token, while SSH requires a key pair. If the key isn’t added to your SSH agent or isn’t registered on the Git host, the server rejects the connection. Check your ~/.ssh directory and add the public key to your account.

How do I know if my SSH key is being used?

Run ssh -vT git@github.com (or your host). The verbose output shows which identity files are offered. If you see “Offering public key” and then “Permission denied,” the key isn’t accepted. If you see “Authenticated,” it worked.

Can I use the same SSH key for multiple Git hosts?

Yes, you can use one key for GitHub, GitLab, and Bitbucket. Add the same public key to each account. However, if you prefer separate keys, use the ~/.ssh/config file to assign different identity files to different hosts.

What if my SSH connection times out?

A timeout usually means port 22 is blocked. Try SSH over port 443 (see Step 3). If that fails, check your firewall or VPN settings. Corporate networks often block port 22 but allow 443.

Do I need to re-add my key after every reboot?

No, but the SSH agent doesn’t persist keys across reboots by default. On macOS, you can add UseKeychain yes to your ~/.ssh/config. On Linux, you can use ssh-add in your shell startup file. Alternatively, you can skip the agent and let SSH find the key directly — it will look for id_ed25519 or id_rsa in ~/.ssh automatically.

Why does my corporate laptop fail over SSH but work on my personal computer?

Corporate firewalls often block port 22, or your company may have a custom SSH configuration that interferes. Try port 443, and check if your company’s VPN or proxy is intercepting SSH traffic. Sometimes you need to use HTTPS instead, or ask your IT department to allow port 22.