What's Actually Happening
You're trying to interact with a remote Git repository (push, fetch, clone, or pull), but your request is rejected because Git cannot authenticate you or you lack the necessary permissions for the operation. The error prevents any data transfer with the remote.
The Error You'll See
For HTTPS URLs:
remote: Permission denied to user/username.
fatal: unable to access 'https://github.com/user/repo.git/': The requested URL returned error: 403For SSH URLs:
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.For organization repositories:
remote: Repository not found.
fatal: repository 'https://github.com/org/repo.git/' not foundOr more generically:
fatal: Authentication failed for 'https://github.com/user/repo.git/'Why This Happens
Permission denied errors occur when: - SSH key is not added to your GitHub/GitLab/Bitbucket account - SSH key has wrong permissions on the file system - Using HTTPS without a credential helper or with expired credentials - Two-factor authentication is enabled but not handled properly - Repository doesn't exist or you don't have access - Deploy key doesn't have write access - SSH agent isn't running or key isn't added - Using the wrong SSH key for the repository
Step 1: Identify Your Remote URL Type
Check what URL you're using:
git remote -vOutput shows either HTTPS:
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)Or SSH:
origin git@github.com:user/repo.git (fetch)
origin git@github.com:user/repo.git (push)Step 2: For SSH - Test Your Connection
Test SSH connectivity to GitHub:
ssh -T git@github.comFor GitLab:
ssh -T git@gitlab.comFor Bitbucket:
ssh -T git@bitbucket.orgSuccessful output:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.Step 3: Check SSH Key Permissions
SSH requires specific file permissions:
```bash # Check permissions ls -la ~/.ssh
# Fix permissions chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub ```
Step 4: Ensure SSH Agent is Running
Start the SSH agent and add your key:
```bash # Start agent eval "$(ssh-agent -s)"
# Add your key ssh-add ~/.ssh/id_rsa ```
Verify the key is added:
ssh-add -lStep 5: Verify SSH Key is Added to Provider
For GitHub, check your keys:
# View your public key
cat ~/.ssh/id_rsa.pubGo to GitHub.com → Settings → SSH and GPG keys, and verify this key is listed.
For multiple keys, configure SSH to use the right one:
``` # ~/.ssh/config Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_work
Host github-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_personal ```
Step 6: For HTTPS - Set Up Credentials
Install a credential helper:
```bash # macOS git config --global credential.helper osxkeychain
# Windows git config --global credential.helper manager
# Linux git config --global credential.helper cache ```
For GitHub with 2FA, create a personal access token:
- 1.Go to GitHub → Settings → Developer settings → Personal access tokens
- 2.Generate new token with
reposcope - 3.Use the token as your password when prompted
Step 7: Switch Between HTTPS and SSH
If one method doesn't work, try the other:
Switch to SSH:
git remote set-url origin git@github.com:user/repo.gitSwitch to HTTPS:
git remote set-url origin https://github.com/user/repo.gitStep 8: Check Repository Access
Verify the repository exists and you have access:
```bash # List your accessible repositories (GitHub CLI) gh repo list --limit 100
# Or check access via API curl -u username:token https://api.github.com/user/repos ```
Step 9: Check Deploy Key Permissions
If using a deploy key (read-only by default):
# Check if the key has write access
ssh -T git@github.comDeploy keys show as "read-only" unless given write access during setup.
Step 10: Use the Correct Account
If you have multiple accounts, ensure Git uses the right one:
git config user.name "Correct Name"
git config user.email "correct@email.com"For per-repository configuration:
git config user.name "Work Name"
git config user.email "work@company.com"Verify the Fix
Test pushing to the remote:
git push origin mainOr test fetching:
git fetch originFor SSH, verify with verbose output:
GIT_SSH_COMMAND="ssh -v" git push origin mainSuccessful output shows:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
...
To github.com:user/repo.git
abc123..def456 main -> main