git clone fails on a private GitHub repository
Permission denied (publickey).Short answer
A private repository needs proof of who you are, and the proof depends on the URL. SSH needs the git user and a public key attached to your account; HTTPS needs a personal access token, not a password. An organization with SAML SSO also needs the key or token authorized. Test SSH with ssh -T first.
Fix it fast
1Find out which cause you have
Check which URL form the repository uses, then test authentication without involving the repository.
git remote -v ssh -T git@github.com2Match what you see, then jump to its fix
- 1
Permission denied (publickey) while the key is on your account.The SSH URL uses your username, not gitFix - 2
ssh -T ends with Permission denied (publickey), or ssh-add -l prints nothing.The key in your agent is not the one on your GitHub accountFix - 3
GitHub refuses to add the key with a Key already in use message.The key is already attached to another account or repositoryFix - 4
Git asks for a password and rejects it, or the clone works for personal repositories but not the organization's.HTTPS with a password, or a token not authorized for the organizationFix
- 1
What does the error look like?
Permission denied (publickey).Key already in use
Cause 1 of 4
The SSH URL uses your username, not git
1Check
Connections to GitHub over SSH must be made as the git user, never your own username.
git remote -v2Fix
Correct the remote and test again.
git remote set-url origin git@github.com:OWNER/REPO.git
ssh -T git@github.com3Verify
The test answers with your username and says GitHub provides no shell access.
git ls-remote originConfirmed by docs.github.com
Cause 2 of 4
The key in your agent is not the one on your GitHub account
1Check
Compare the fingerprint of the loaded key with the list under Settings, SSH and GPG keys. An empty list means no key is loaded.
ssh-add -l -E sha256
ssh -i ~/.ssh/KEY-FILE -vT git@github.com2Fix
Load the key, or add its public half to your account if it is missing. If the list contains a key you do not recognize, delete it and contact GitHub Support.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/KEY-FILE3Verify
ssh -T greets you by username.
ssh -T git@github.comConfirmed by docs.github.com
Cause 3 of 4
The key is already attached to another account or repository
1Check
A deploy key belongs to one repository and cannot be reused on another. Ask GitHub who owns the key, using only that key.
ssh -T -i ~/.ssh/KEY-FILE -o IdentitiesOnly=yes git@github.com2Fix
Remove the key from the other account or repository and add it to yours. If you cannot, generate a new key pair.
ssh-keygen -t ed25519 -C "your_email@example.com"3Verify
The same test now names your account.
ssh -T -i ~/.ssh/KEY-FILE -o IdentitiesOnly=yes git@github.comConfirmed by docs.github.com
Cause 4 of 4
HTTPS with a password, or a token not authorized for the organization
1Check
GitHub removed password authentication for Git. Over HTTPS, the password prompt takes a personal access token. For organizations with SAML SSO, the token or SSH key must be authorized for that organization.
git remote -v2Fix
Create a personal access token, enter it as the password (a credential helper such as Git Credential Manager can store it), and authorize the token or key for the organization.
git clone https://github.com/OWNER/REPO.git3Verify
The clone completes after you enter your username and the token.
git ls-remote originConfirmed by docs.github.com
How do I stop it coming back?
- Prefer SSH with a key per machine, and name each key in GitHub so you can find stale ones.
- Do not run git with sudo; it may use a different set of keys.
- Use one deploy key per repository.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Related fixes
Security
SSH refuses your key and ends with Permission denied (publickey)
user@203.0.113.10: Permission denied (publickey,password).
The server accepted the connection but found no key it would take.
5 causes, each with a fixSecurity
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fix