This error occurs when Git cannot establish a connection to the remote repository. Common causes include SSH key issues, incorrect repository URLs, missing access permissions, or network problems preventing the connection.
The "fatal: Could not read from remote repository" error indicates that Git attempted to communicate with a remote repository (like one hosted on GitHub, GitLab, or Bitbucket) but failed to establish a connection. This is a generic error that can stem from several different underlying issues. The error message "Please make sure you have the correct access rights and the repository exists" provides two main hints: 1. **Access rights**: Your authentication method (SSH keys or HTTPS credentials) may not be configured correctly, or you may not have permission to access the repository 2. **Repository exists**: The remote URL might be incorrect, the repository may have been deleted or renamed, or it might be a private repository you cannot access This error commonly appears when: - Cloning a repository for the first time - Pushing or pulling changes - Fetching updates from upstream - Working with submodules that reference remote repositories
First, test if you can connect to the remote Git server via SSH:
For GitHub:
ssh -T [email protected]For GitLab:
ssh -T [email protected]For Bitbucket:
ssh -T [email protected]Expected success message (GitHub):
Hi username! You've successfully authenticated, but GitHub does not provide shell access.If you see "Permission denied (publickey)", your SSH key is not configured correctly. If you see a connection timeout, there may be network/firewall issues.
Verify that you have an SSH key and it's loaded into the SSH agent:
Check for existing SSH keys:
ls -la ~/.sshLook for files like id_ed25519, id_rsa, id_ecdsa (private keys) and their .pub counterparts.
Start the SSH agent and add your key:
# Start the agent
eval "$(ssh-agent -s)"
# Add your key (replace with your key name)
ssh-add ~/.ssh/id_ed25519
# or
ssh-add ~/.ssh/id_rsaVerify the key is loaded:
ssh-add -lIf no keys are loaded, you'll see "The agent has no identities."
If you don't have an SSH key, generate one:
# Generate a new ED25519 key (recommended)
ssh-keygen -t ed25519 -C "[email protected]"
# Or generate an RSA key (for older systems)
ssh-keygen -t rsa -b 4096 -C "[email protected]"When prompted:
- Press Enter to accept the default file location
- Enter a passphrase (recommended) or press Enter for no passphrase
Add the key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519Copy the public key to your clipboard:
# macOS
cat ~/.ssh/id_ed25519.pub | pbcopy
# Linux (with xclip)
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# Windows (Git Bash)
cat ~/.ssh/id_ed25519.pub | clipAdd the public key to your GitHub/GitLab/Bitbucket account:
GitHub:
1. Go to Settings > SSH and GPG keys > New SSH key
2. Give it a descriptive title
3. Paste your public key
4. Click "Add SSH key"
GitLab:
1. Go to Preferences > SSH Keys
2. Paste your public key
3. Set an expiration date (optional)
4. Click "Add key"
Bitbucket:
1. Go to Personal settings > SSH keys
2. Click "Add key"
3. Paste your public key
4. Click "Add key"
Verify it works:
ssh -T [email protected]Verify your remote URL is correct:
git remote -vExample output:
origin [email protected]:username/repo.git (fetch)
origin [email protected]:username/repo.git (push)Common issues to check:
- Typo in username or repository name
- Repository was renamed or moved to a different organization
- Using SSH URL ([email protected]:) but SSH isn't set up
- Using HTTPS URL (https://) but need to use SSH
Update the remote URL if needed:
# Change to SSH
git remote set-url origin [email protected]:username/repo.git
# Change to HTTPS
git remote set-url origin https://github.com/username/repo.gitVerify the repository exists by visiting it in your browser.
SSH requires strict permissions on key files. Fix permissions if they're too open:
# Set correct permissions on .ssh directory
chmod 700 ~/.ssh
# Set correct permissions on private key
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_rsa
# Set correct permissions on public key
chmod 644 ~/.ssh/id_ed25519.pub
chmod 644 ~/.ssh/id_rsa.pub
# Set correct permissions on config file
chmod 600 ~/.ssh/configIf permissions are too open, SSH will refuse to use the key and you'll see warnings like:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@If you have multiple SSH keys, configure SSH to use the right one for each host:
Create or edit ~/.ssh/config:
nano ~/.ssh/configAdd configuration for your Git host:
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
# GitLab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
IdentitiesOnly yes
# Work GitHub (using different key)
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesFor the work alias, update your remote:
git remote set-url origin git@github-work:company/repo.gitSome networks block SSH port 22. Use port 443 instead:
Test if port 443 works:
ssh -T -p 443 [email protected]Configure SSH to always use port 443 for GitHub:
Edit ~/.ssh/config:
Host github.com
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519Or use HTTPS with credential caching:
# Switch to HTTPS
git remote set-url origin https://github.com/username/repo.git
# Cache credentials for 1 hour
git config --global credential.helper 'cache --timeout=3600'
# Or store credentials permanently (less secure)
git config --global credential.helper storeUse verbose mode to diagnose SSH connection problems:
# Verbose SSH connection test
ssh -vT [email protected]Key things to look for in the output:
1. "Offering public key" - Shows which key SSH is trying to use
2. "Server accepts key" - Key was accepted
3. "Permission denied" - Authentication failed
4. "Connection timed out" - Network/firewall issue
Common debug patterns:
# Key not found
debug1: No more authentication methods to try.
Permission denied (publickey).
# Wrong key being offered
debug1: Offering public key: /home/user/.ssh/id_rsa
debug1: Authentications that can continue: publickey
# Key file permissions issue
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@If this is your first time connecting to a host, SSH will ask you to verify its authenticity:
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?Verify the fingerprint against the official documentation:
- GitHub: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints
- GitLab: https://docs.gitlab.com/ee/user/gitlab_com/#ssh-host-keys-fingerprints
If it matches, type `yes` to add to known_hosts.
In CI/CD environments, you may need to pre-populate known_hosts:
mkdir -p ~/.ssh
ssh-keyscan github.com >> ~/.ssh/known_hostsDeploy Keys vs User SSH Keys:
For CI/CD pipelines accessing private repositories, consider using deploy keys:
- Deploy keys are repository-specific SSH keys
- They provide read-only access by default (can be set to read-write)
- Safer than using personal SSH keys in CI/CD
Generate and add a deploy key:
# Generate a key specifically for the repository
ssh-keygen -t ed25519 -f ~/.ssh/deploy_myrepo -C "deploy-key-myrepo"Then add the public key as a deploy key in your repository settings.
Personal Access Tokens (PAT) for HTTPS:
If SSH doesn't work in your environment, use a Personal Access Token with HTTPS:
git clone https://oauth2:[email protected]/username/repo.git
# Or configure credential helper to store the token
git config --global credential.helper store
git clone https://github.com/username/repo.git
# Enter your username and PAT when promptedGitHub CLI Alternative:
The GitHub CLI (gh) can handle authentication automatically:
# Install and authenticate
gh auth login
# Clone repositories
gh repo clone username/repoSubmodule Issues:
If the error occurs with submodules:
# Update submodule URLs to match your access method
git config --file=.gitmodules submodule.mysubmodule.url [email protected]:org/submodule.git
git submodule sync
git submodule update --init --recursiveSSH Agent Forwarding:
When working on remote servers and needing access to private repos:
# Connect with agent forwarding
ssh -A user@server
# On the server, verify your key is available
ssh-add -lWindows-Specific Issues:
On Windows, ensure you're using the Git Bash SSH, not Windows OpenSSH:
# Check which SSH Git is using
git config --global core.sshCommand
# Force Git to use Git Bash SSH
git config --global core.sshCommand "C:/Program Files/Git/usr/bin/ssh.exe"kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git