[Git/GitHub] Error: fatal: Authentication failed for 'https://github.com/…' — How to Fix It

Summary

fatal: Authentication failed for 'https://github.com/...' occurs when Git cannot authenticate your connection to GitHub. Since August 2021, GitHub has disabled password-based authentication — you must now use a Personal Access Token (PAT) or SSH key. The fix involves generating a new token, replacing stored credentials, and ensuring your remote URL is correct.

Context

This error appears when pushing, pulling, or cloning a repository using HTTPS. If Git is configured with old credentials, GitHub rejects the login. Common cases include switching computers, enabling two-factor authentication (2FA), or tokens expiring. The solution depends on your setup: HTTPS users must use tokens; SSH users must verify their keys.

Probable Cause

  • Outdated credentials: Your system still uses a stored GitHub password instead of a Personal Access Token.
  • Revoked or expired token: The PAT was deleted or expired in GitHub settings.
  • Wrong remote URL: Using HTTPS when SSH is configured (or vice versa).
  • Two-factor authentication: Requires a token even if credentials seem valid.
  • Credential caching: The OS is providing an invalid password automatically.

Quick Fix

  1. Generate a new GitHub Personal Access Token (PAT): Visit https://github.com/settings/tokens → click Generate new token → select scopes like repo and workflow.
  2. Copy the token (it’s shown only once) and use it instead of your password when prompted by Git.
  3. Clear old credentials:
    • Windows: Control Panel → Credential Manager → Windows Credentials → remove all github.com entries.
    • macOS: Open Keychain Access → search “github.com” → delete outdated items.
    • Linux: Remove manually from ~/.git-credentials or clear cache with git credential-cache exit.
  4. Re-run your Git command: e.g., git push origin main. When asked for credentials: - Username → your GitHub username - Password → your Personal Access Token.
  5. Optional: Save token permanently: git config --global credential.helper store

Example

Before: Old authentication attempt

$ git push origin main Username for 'https://github.com': lmg Password for 'https://github.com': remote: Support for password authentication was removed. fatal: Authentication failed for 'https://github.com/lmg/repo.git' 

After: Using Personal Access Token

$ git push origin main Username for 'https://github.com': lmg Password for 'https://github.com': [paste your PAT here] Enumerating objects: 4, done. Counting objects: 100% (4/4), done. 

Alternative: Use SSH Instead of HTTPS

# Generate SSH key if not already done ssh-keygen -t ed25519 -C "your_email@example.com"
Add SSH key to agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Add public key to GitHub: Settings → SSH and GPG keys
Then switch remote:

git remote set-url origin git@github.com
:username/repo.git

Pitfalls & Debug

  • Symptom → Repeated credential prompts in VS Code or SourceTree. Fix → Remove cached credentials in system manager and reauthenticate.
  • Symptom → Works on one device but not another. Fix → Tokens are device-specific — generate a new one per machine.
  • Symptom → Token accepted once, fails after restart. Fix → Credential helper not persisting — use git config --global credential.helper manager (Windows) or osxkeychain (Mac).
  • Symptom → “fatal: could not read Username for…” Fix → Remote URL missing credentials; re-run git remote set-url properly.

Verification & Next Steps

# Check remote configuration git remote -v
Test authentication

git ls-remote https://github.com/username/repo.git

If it lists repository refs, your authentication works. Otherwise, recheck token scope and expiration in GitHub → Settings → Developer settings → Tokens.

Best Practices

  • Use fine-grained tokens for minimal access.
  • Rotate tokens periodically and revoke unused ones.
  • For automation (CI/CD), use GitHub Actions secrets instead of personal tokens.
  • Prefer SSH for frequent development — it avoids reauthentication overhead.

Sources

GitHub Docs — Authentication Changes

Create a Personal Access Token

Connecting to GitHub with SSH

Labels

Tool/Git, OS/Cross-platform, Topic/Authentication & Tokens