Git Clone SSH: Clone With an SSH Key, Command and Fixes
To git clone with SSH, generate a key with ssh-keygen -t ed25519, add the public half (~/.ssh/id_ed25519.pub) to your GitHub, GitLab or Bitbucket account, and clone with the repository’s SSH URL:
git clone git@github.com:OWNER/REPO.git
The SSH URL is under the Code button on GitHub (SSH tab) and GitLab (Clone with SSH). It starts with git@, not https://. The rest of this guide covers each step on macOS, Linux and Windows, cloning with a specific key, and the errors you are likely to hit.
The git clone SSH command
The URL has the form git@HOST:OWNER/REPO.git:
# GitHub
git clone git@github.com:OWNER/REPO.git
# GitLab
git clone git@gitlab.com:GROUP/PROJECT.git
# Bitbucket
git clone git@bitbucket.org:WORKSPACE/REPO.git
# Clone into a folder with a different name
git clone git@github.com:OWNER/REPO.git my-folder
ssh://git@github.com/OWNER/REPO.git is the same address in URL form, and it’s the form to use for a custom SSH port, which git@host:path can’t carry:
git clone ssh://git@git.example.com:2222/OWNER/REPO.git
On GitHub the SSH user is always git: connecting with your GitHub username fails, and your key tells GitHub which account you are. If the clone fails with Permission denied (publickey), your key isn’t set up yet: work through the steps below, or jump to Troubleshooting.
1. Generate an SSH key
Check whether you already have one:
ls -al ~/.ssh
Files named id_ed25519 and id_ed25519.pub mean you do. Otherwise, create a key. Use your Git account’s email as the comment:
ssh-keygen -t ed25519 -C "you@example.com"
Press Enter to accept the default path (~/.ssh/id_ed25519), then set a passphrase. The passphrase encrypts the private key on disk, so a copied key file is useless without it. You will rarely type it, because the ssh-agent remembers it (step 2).
You now have two files:
~/.ssh/id_ed25519: the private key. It never leaves your machine; don’t copy it anywhere.~/.ssh/id_ed25519.pub: the public key. This is what you give to GitHub, GitLab or Bitbucket.
Ed25519 is GitHub’s recommended key type. If a legacy system can’t use it, fall back to ssh-keygen -t rsa -b 4096 -C "you@example.com". With a hardware security key, use -t ed25519-sk.
On Windows, Windows 10 and 11 include the OpenSSH client, so the same ssh-keygen command works in PowerShell. Git Bash, which comes with Git for Windows, works too.
2. Add the key to the ssh-agent
The agent holds your unlocked key in memory, so you enter the passphrase once instead of on every git pull.
macOS: add this to ~/.ssh/config (create the file if it doesn’t exist):
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Then store the passphrase in the macOS keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Linux and Git Bash:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Windows (PowerShell): start the agent service once, from an administrator window, then add the key from a normal one:
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
These steps follow GitHub’s guide to generating a key.
3. Add the public key to your Git host
Copy the public key:
# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Linux (or just cat the file and copy the line)
xclip -selection clipboard < ~/.ssh/id_ed25519.pub
# Git Bash on Windows
clip < ~/.ssh/id_ed25519.pub
In PowerShell, use Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard.
- GitHub: Settings › SSH and GPG keys › New SSH key. Choose Authentication Key, give it a title that names the machine (“Work laptop 2026”), and paste the key. With the GitHub CLI:
gh ssh-key add ~/.ssh/id_ed25519.pub --title "Work laptop". - GitLab: your avatar › Edit profile › SSH Keys › Add new key. GitLab lets you set an expiry date.
- Bitbucket: Personal settings › SSH keys › Add key.
Name every key after the machine it lives on. When a laptop is lost, you’ll know which key to delete.
4. Test the connection
ssh -T git@github.com
The first time, SSH asks you to confirm the host. Compare the fingerprint it shows with the one GitHub publishes (GitHub’s SSH key fingerprints; for Ed25519 it’s SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU), then type yes. Success looks like:
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
For the other hosts, run ssh -T git@gitlab.com (it answers “Welcome to GitLab, @username!”) or ssh -T git@bitbucket.org.
5. Clone the repository with SSH
git clone git@github.com:OWNER/REPO.git
The command is the same on macOS, Linux and Windows, where it runs in PowerShell as well as Git Bash. Private repositories work the same way, as long as your account has access. git push, git pull and git fetch in that clone now use SSH too. If you were looking for git checkout over SSH, this is the command you want: git checkout only works inside a repository you have already cloned.
Switch an existing HTTPS clone to SSH
You don’t need to clone again. Point the remote at the SSH URL:
git remote -v
git remote set-url origin git@github.com:OWNER/REPO.git
To make Git use SSH for every GitHub URL, including the https:// ones in scripts and submodules you don’t control, rewrite them in your global config. git config --global --unset url."git@github.com:".insteadOf undoes it.
git config --global url."git@github.com:".insteadOf "https://github.com/"
Clone with a specific SSH key
SSH offers the keys in your agent and the default key files in turn, and GitHub accepts the first one that belongs to any account. With a personal and a work key, that is often the wrong one. Two ways to choose the key yourself:
One repository: GIT_SSH_COMMAND and core.sshCommand
To specify the key for one clone, pass its identity file with ssh -i. That’s the private key, the file without .pub; the public half stays on the host:
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes' \
git clone git@github.com:company/project.git
Then save the setting in that repository, so later pulls and pushes use the same key:
cd project
git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes"
git clone -c does both in one command, because it writes the setting into the new repository before the first fetch. The GIT_SSH_COMMAND=... prefix above is Bash and zsh syntax that PowerShell doesn’t accept, while -c is a Git option and doesn’t depend on the shell:
git clone -c core.sshCommand="ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes" git@github.com:company/project.git
IdentitiesOnly=yes makes SSH offer only the key you named, not every key in the agent.
Several accounts: host aliases in ~/.ssh/config
# Personal GitHub account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# Work GitHub account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
Clone with the alias in place of github.com:
git clone git@github.com-work:company/project.git
GitHub won’t let you add the same public key to two accounts (“Key is already in use”), so each account needs its own key.
SSH vs HTTPS
| SSH | HTTPS | |
|---|---|---|
| Setup | Generate a key, add the .pub to the host |
Sign in through a credential helper or create a personal access token |
| What sits on your machine | A private key, ideally passphrase-protected | A token, usually in a credential helper |
| Expiry | GitHub keys last until you delete them; GitLab keys can expire | Tokens usually carry an expiry date, and organizations can enforce a maximum |
| Several accounts | Host aliases or core.sshCommand |
Separate credentials per host or path |
| Firewalls | Port 22 is sometimes blocked; GitHub also accepts SSH on 443 | Port 443, almost never blocked |
GitHub stopped accepting account passwords for Git operations on August 13, 2021, so the real choice is an SSH key or a token. With SSH, only a signature crosses the network; a token can end up in shell history, CI logs or a remote URL. HTTPS with a credential helper such as Git Credential Manager is also a fine choice, especially behind strict firewalls.
Troubleshooting
Permission denied (publickey)
The server didn’t accept any key your machine offered. Check, in this order:
-
Is the key on the right account?
ssh -T git@github.comgreets you by username. If it names a different account, SSH offered another key: useIdentitiesOnlyas shown above. -
Does the agent have the key?
ssh-add -llists loaded keys. “The agent has no identities” means runssh-add ~/.ssh/id_ed25519again; this often happens after a reboot. -
Which key is SSH offering? Run the test verbosely and look for
Offering public keyandServer accepts key:ssh -vT git@github.comIf
git cloneis not using your SSH key, first check that the URL starts withgit@orssh://, because anhttps://URL never touches SSH. Then get the same verbose output from a real clone:GIT_SSH_COMMAND='ssh -v' git clone git@github.com:OWNER/REPO.git -
Are the file permissions too open? SSH ignores private keys that other users can read:
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_ed25519
If ssh -T works but the clone says ERROR: Repository not found, the URL is wrong or your account has no access to that repository.
Host key verification failed
The host key in your ~/.ssh/known_hosts doesn’t match what the server sent. GitHub replaced its RSA host key in March 2023, so older machines still hit this. Remove the old entry, reconnect and check the fingerprint against GitHub’s published list:
ssh-keygen -R github.com
ssh -T git@github.com
On GitLab or Bitbucket, use that host’s name in both commands.
Connection timed out on port 22
Some networks block port 22. GitHub accepts SSH on port 443 at ssh.github.com:
ssh -T -p 443 git@ssh.github.com
If that works, make it permanent in ~/.ssh/config, and your usual git@github.com: URLs keep working:
Host github.com
Hostname ssh.github.com
Port 443
User git
Git keeps asking for the passphrase
The agent isn’t running or doesn’t have the key. On macOS, check the UseKeychain block from step 2. On Windows, Git for Windows ships its own ssh, which doesn’t talk to the Windows ssh-agent service; point Git at the Windows one:
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"
The clone works in your terminal but fails in a coding agent
Our Claude Code analytics plugin is installed by Claude Code itself, and on Windows machines without Git that install used to stop at the step that clones the plugin. So we tested what SSH does in the same seat. We ran git clone from Claude Code’s Bash tool with Git 2.50 and OpenSSH 10.2, against a throwaway SSH server on localhost and, for a host it had never seen, against github.com too. A command run that way has no terminal, so SSH can’t ask either of its two questions, and the errors point at the wrong problem:
| Situation | No terminal | A pseudo-terminal nothing types into |
|---|---|---|
The host isn’t in known_hosts yet |
Host key verification failed. |
Waits at Are you sure you want to continue connecting |
| The key has a passphrase and isn’t in the ssh-agent | Permission denied (publickey). |
Waits at Enter passphrase for key |
The same key, loaded with ssh-add first |
Clones | Clones |
The second row is the easy one to misread, because it looks exactly like a key that was never added to your account, and the same command works when you paste it into your own terminal. A wait is worse, since nothing reports a failure at all. We killed ours after 8 seconds.
Set up both before you start the agent. Run ssh -T git@github.com once in your own terminal to accept the host, then ssh-add the key in the shell you start the agent from. To check from inside the session, have the agent run ssh-add -l: both The agent has no identities. and Could not open a connection to your authentication agent. mean the key isn’t available there. In a script, BatchMode turns every wait into an immediate failure and still clones when the key is loaded:
GIT_SSH_COMMAND='ssh -o BatchMode=yes' git clone git@github.com:OWNER/REPO.git
Frequently asked questions
Can I use the same SSH key for GitHub, GitLab and Bitbucket?
Yes. A key identifies your machine, so one id_ed25519.pub can go on all three. The one limit is within a host: GitHub won’t accept the same key on two different accounts.
How do I clone a private repository with SSH?
The same way as a public one: git clone git@github.com:OWNER/REPO.git. Your account needs read access to the repository. For a server that only needs one repository, such as a self-hosted runner on one of these CI/CD platforms, add a read-only deploy key to that repository instead of a personal key.
How do I git pull with an SSH key?
Git uses your SSH key whenever the remote is an SSH URL, so if git remote -v shows git@github.com:, a plain git pull already does it. If it shows https://, switch the remote to SSH first. To use a particular key for one pull, run GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes' git pull.
Can I git clone over SSH with a username and password?
Not on GitHub: SSH connections use a key, and account passwords stopped working for Git operations in August 2021. A personal access token won’t work over SSH either, because GitHub tokens are for https:// URLs only. On your own server, if it allows password logins, put the user in the URL (git clone ssh://user@host/path/repo.git) and SSH asks for the password.
What happens if I lose my private SSH key?
Delete its public key from every service where you added it (GitHub, GitLab, servers’ authorized_keys). That revokes access at once. Then generate a new key and add it.
Is an SSH key more secure than a password?
Yes. The private key never leaves your machine; the server checks a signature, so there’s nothing to phish or sniff. A passphrase protects the key file if your laptop is stolen.
Do I need a passphrase on my SSH key?
It’s optional but recommended. Without one, anyone who copies the key file can use it. With the ssh-agent or the macOS keychain, you type it once per session, not on every push. For more on what happens after the clone, see our guide to pulling and merging on GitHub.
Zest is AI coding analytics for engineering teams: it shows which coding agents, models and skills each engineer uses, what they cost, and which sessions end in merged pull requests. See how Zest works.