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.

Introduction & Quick Summary
A push fails with two lines, and the second one is the one people quote:
$ 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.
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.urlIf 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): thesshclient 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:
-ion the command line, orGIT_SSH_COMMAND.IdentityFilefor a matchingHostblock in~/.ssh/config.IdentityAgentfrom the config, thenSSH_AUTH_SOCK.- 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:
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 line | Meaning | Fix |
|---|---|---|
Permission denied (publickey) | no offered key is registered | add the public key to the account |
Repository not found (HTTPS) | wrong path, or no access | check owner/repo, token scope |
fatal: could not read Username | prompts disabled, no credential | configure a token or helper |
Host key verification failed | unknown host key | verify and add to known_hosts |
Connection timed out on 22 | network blocks SSH | use ssh.github.com:443 |
ERROR: Repository not found on push only | read access but no write | check collaborator role |
| Remote form | Auth used | Credential source |
|---|---|---|
git@github.com:o/r.git | SSH key | agent, ~/.ssh/config, IdentityFile |
ssh://git@host:2222/o/r.git | SSH on a custom port | same, host alias must match |
https://host/o/r.git | token or password | credential helper, GIT_ASKPASS |
gh CLI protocol | HTTPS + token | gh auth login stores it |
| Command | Answers |
|---|---|
ssh -vT git@github.com | which key was offered, whether it authenticated |
ssh-add -l | which keys the agent holds |
ssh-keygen -lf ~/.ssh/id_ed25519.pub | fingerprint to compare with the account |
git config --show-origin --get-all remote.origin.url | whether 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:
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_ed25519Clearing 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:
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 yesThen the remote can select an identity per repository:
git remote set-url origin git@github-personal:me/notes.gitIdentitiesOnly 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):
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:
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-gitgh 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:
Host github.com
HostName ssh.github.com
Port 443
User gitVerify before relying on it:
nc -vz github.com 22
nc -vz ssh.github.com 443If 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:
git config --show-origin --get-regexp 'url\..*\.insteadof'The same rule applies inside submodules, which do not inherit the parent's credentials automatically:
git submodule update --init --recursive
git config --file .gitmodules --get-regexp urlFor 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
-ibefore 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_ed25519andchmod 700 ~/.sshare 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 cloneto fail loudly on access. GitHub answersRepository not foundfor private repositories you cannot see, which reads like a typo. - Git 2.38 and later:
sshmay be configured viaGIT_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 documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.