Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git: fatal: could not read from remote repository

Git could not authenticate or reach the remote. Check the remote URL, test SSH with -T, verify the key is offered, and use HTTPS on 443 when 22 is blocked.

~5 min readGit
#git#ssh#remotes#authentication
Git remote rail cut off from a repository crate with a key and a blocked port gate

Introduction & Quick Summary

A push fails with two lines, and the second one is the one people quote:

code
$ git push origin main git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

Quick answer: Git could not establish an authenticated transport to the remote. The second line is generic — it appears for a wrong SSH key, a missing key, a typo in the URL, a repository you cannot access, and a network that blocks port 22. The line above it (Permission denied (publickey), Repository not found, terminal prompts disabled) is the actual diagnosis.

bash
git remote -v # what transport is configured ssh -T git@github.com -v # which key is offered and what the server says git config --get remote.origin.url

If ssh -T prints Hi <user>! You've successfully authenticated, SSH is fine and the problem is the repository or the HTTPS/SSH mix. If it prints Permission denied (publickey), no offered key is registered on the remote account.

This guide covers the two transports Git uses, the SSH key-selection rules that decide which identity reaches the server, the HTTPS and token case that produces the same fatal line, and the network workarounds when port 22 is blocked.

Core Mechanics, Signatures & Reproducible Setup

Git delegates authentication to a transport:

  • SSH (git@host:owner/repo.git): the ssh client picks a key and the server matches its public part against the account's keys.
  • HTTPS (https://host/owner/repo.git): Git asks a credential helper for a username and password or token, or fails when prompts are disabled.

The remote URL decides which one runs. That is why ssh -T can succeed while git push still fails: the remote may be HTTPS.

SSH key selection follows a strict order:

  1. -i on the command line, or GIT_SSH_COMMAND.
  2. IdentityFile for a matching Host block in ~/.ssh/config.
  3. IdentityAgent from the config, then SSH_AUTH_SOCK.
  4. Default files ~/.ssh/id_rsa, id_ecdsa, id_ed25519.

The server accepts the first key that matches, and with IdentitiesOnly unset the client may offer several before finding one. A key in the agent from a previous job can therefore shadow the key you just created.

A clean reproduction of the failure:

bash
mkdir -p /tmp/ssh-demo && cd /tmp/ssh-demo git init -q repo && cd repo && git commit --allow-empty -m init git remote add origin git@github.com:nonexistent-owner-xyz/none.git ssh -T git@github.com -v 2>&1 | grep -E "Offering|Authenticated|denied" git push origin master

-v (or -vvv) is what turns this into a solvable problem: it lists every key offered (Offering public key: ...) and the server's reply for each.

Key Comparison Table

Message above the fatal lineMeaningFix
Permission denied (publickey)no offered key is registeredadd the public key to the account
Repository not found (HTTPS)wrong path, or no accesscheck owner/repo, token scope
fatal: could not read Usernameprompts disabled, no credentialconfigure a token or helper
Host key verification failedunknown host keyverify and add to known_hosts
Connection timed out on 22network blocks SSHuse ssh.github.com:443
ERROR: Repository not found on push onlyread access but no writecheck collaborator role
Remote formAuth usedCredential source
git@github.com:o/r.gitSSH keyagent, ~/.ssh/config, IdentityFile
ssh://git@host:2222/o/r.gitSSH on a custom portsame, host alias must match
https://host/o/r.gittoken or passwordcredential helper, GIT_ASKPASS
gh CLI protocolHTTPS + tokengh auth login stores it
CommandAnswers
ssh -vT git@github.comwhich key was offered, whether it authenticated
ssh-add -lwhich keys the agent holds
ssh-keygen -lf ~/.ssh/id_ed25519.pubfingerprint to compare with the account
git config --show-origin --get-all remote.origin.urlwhether insteadOf rewrote the URL

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Prove which key is used

ssh -vT prints Offering public key: /home/me/.ssh/id_rsa RSA SHA256:... and then the server's answer for each. If the offered fingerprint is not the one listed in GitHub's Settings → SSH keys, that is the whole problem:

bash
ssh -vT git@github.com 2>&1 | grep -E "Offering|Server accepts|Authenticated|denied" ssh-add -l ssh-add -D # clear the agent when an old key shadows the new one ssh-add ~/.ssh/id_ed25519

Clearing the agent is safe: keys remain on disk. Clearing ~/.ssh is not — see the mistakes below.

2. Multiple identities and ~/.ssh/config

With a work key and a personal key, the client offers them in an order you do not control unless you say so:

sshconfig
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_work_ed25519 IdentitiesOnly yes Host github-personal HostName github.com User git IdentityFile ~/.ssh/id_personal_ed25519 IdentitiesOnly yes

Then the remote can select an identity per repository:

bash
git remote set-url origin git@github-personal:me/notes.git

IdentitiesOnly yes is the line that makes the choice deterministic; without it, the agent's keys are tried first.

3. Deploy keys are per repository

A deploy key grants access to exactly one repository and is read-only unless "Allow write access" was checked when it was added. Using it for a second repository produces the same Permission denied (publickey):

bash
ssh -i ~/.ssh/deploy_key -o IdentitiesOnly=yes -T git@github.com # Hi o/r! You've successfully authenticated, but GitHub does not provide shell access.

The greeting names the target repository (Hi o/r!), which is the fastest confirmation that the key is scoped the way you expect.

4. HTTPS remotes and CI prompts

With an HTTPS remote, Git needs a credential. In CI there is no terminal, so the failure becomes fatal: could not read Username for 'https://github.com': No such device or address:

bash
git config --global url."https://x-access-token:${GH_TOKEN}@github.com/".insteadOf "https://github.com/" # or, better, keep the token out of the URL GIT_ASKPASS=/bin/echo GIT_TERMINAL_PROMPT=0 gh auth setup-git

gh auth setup-git configures a helper that supplies the token without putting it in .git/config, which survives in logs and shell history. GIT_TERMINAL_PROMPT=0 makes the failure immediate instead of hanging a pipeline.

5. Port 22 blocked

Corporate networks and some CI runners block outbound SSH. GitHub serves SSH on ssh.github.com:443:

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

Verify before relying on it:

bash
nc -vz github.com 22 nc -vz ssh.github.com 443

If 22 is blocked and 443 works, the config block above fixes every existing git@github.com: remote without touching them.

6. URL rewrites and submodules

A global insteadOf rule can silently point a repository at the wrong host:

bash
git config --show-origin --get-regexp 'url\..*\.insteadof'

The same rule applies inside submodules, which do not inherit the parent's credentials automatically:

bash
git submodule update --init --recursive git config --file .gitmodules --get-regexp url

For submodules over SSH in CI, rewriting git@github.com: to an HTTPS token URL is often the only workable option, since the deploy key belongs to the parent repository.

The regular expression tester is handy for pulling the offered key fingerprints out of a long -vvv trace, and the curl tool confirms that an HTTPS remote is reachable and answering before you debug credentials.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Regenerating keys to "start clean". A new key pair removes the working one; add the public key to the account first and test with -i before replacing anything.
  • Copying a private key into CI. Use a deploy key or a token secret; a copied private key usually has wrong permissions and fails with bad permissions.
  • Wrong file permissions. chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh are required; the client refuses group-readable private keys.
  • Using the account password with HTTPS. Password authentication is disabled for Git operations; a personal access token is required.
  • Expecting git clone to fail loudly on access. GitHub answers Repository not found for private repositories you cannot see, which reads like a typo.
  • Git 2.38 and later: ssh may be configured via GIT_SSH_COMMAND. A wrapper set in the environment overrides ~/.ssh/config, which explains "works in one terminal only".
  • Case and hyphen differences. GitHub paths are case-insensitive for the owner but not for the repository on every forge; a single wrong character produces the same fatal line.

Official documentation: git clone and transport URLs and GitHub: error permission denied publickey.

Official references

primary documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.