Git Error: Could Not Read from Remote Repository — SSH Key Fix

Running git push and seeing “fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.” Almost always means the SSH key on the remote host is missing, expired, or not yet added to your Git hosting account (GitHub, Bitbucket, GitLab).

Problem: Running git push or git pull on a server returns "Could not read from remote repository. Please make sure you have the correct access rights" — the SSH connection to GitHub or Bitbucket fails.

Solution: Check whether an SSH key exists on the server with ls ~/.ssh/, generate a new key pair with ssh-keygen if one is missing, add the public key to the hosting provider's SSH key settings, and verify the connection with ssh -T git@github.com.

Step 1 — check whether an SSH key already exists on the server:

ls -la ~/.ssh/
# Look for id_rsa.pub or id_ed25519.pub

Step 2 — if no key exists, generate one:

# Recommended: Ed25519 key (more secure than RSA)
ssh-keygen -t ed25519 -C "deploy@example.com"

# Legacy RSA if Ed25519 is not supported
ssh-keygen -t rsa -b 4096 -C "deploy@example.com"

Step 3 — copy the public key to your clipboard:

cat ~/.ssh/id_ed25519.pub   # print to terminal, then copy
# or on macOS:
pbcopy < ~/.ssh/id_ed25519.pub

Step 4 — add the key to your Git hosting account:

Bitbucket: Account Settings → SSH Keys → Add key
GitHub: Settings → SSH and GPG keys → New SSH key
GitLab: Preferences → SSH Keys

Step 5 — test the connection:

ssh -T git@bitbucket.org    # Bitbucket
ssh -T git@github.com       # GitHub
ssh -T git@gitlab.com       # GitLab
# Expected response: "authenticated" or "Welcome to GitLab, @username!"

NOTE: If the test returns "Permission denied (publickey)" even after adding the key, check that the remote URL uses SSH (not HTTPS). Run git remote -v — the URL should start with git@. If it starts with https://, switch it: git remote set-url origin git@github.com:username/repo.git.