Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

git clone fails on a private GitHub repository

ErrorPermission 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.

  • Applies to Git 2.4x, OpenSSH 9.x and 10.x, GitHub.com
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.com
  2. 2Match what you see, then jump to its fix

    1. 1Permission denied (publickey) while the key is on your account.The SSH URL uses your username, not gitFix
    2. 2ssh -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. 3GitHub refuses to add the key with a Key already in use message.The key is already attached to another account or repositoryFix
    4. 4Git 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

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 -v

2Fix

Correct the remote and test again.

git remote set-url origin git@github.com:OWNER/REPO.git
ssh -T git@github.com

3Verify

The test answers with your username and says GitHub provides no shell access.

git ls-remote origin

Confirmed by docs.github.com

Did this fix it?

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.com

2Fix

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-FILE

3Verify

ssh -T greets you by username.

ssh -T git@github.com

Confirmed by docs.github.com

Did this fix it?

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.com

2Fix

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.com

Confirmed by docs.github.com

Did this fix it?

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 -v

2Fix

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.git

3Verify

The clone completes after you enter your username and the token.

git ls-remote origin

Confirmed by docs.github.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.