How to Fix Git Authentication Failed Error
In this tutorial, you'll learn about How to Fix Git Authentication Failed Error. We cover key concepts, practical examples, and best practices.
You run git push or git pull and get Authentication failed — your credentials are expired, incorrect, or the remote requires a different authentication method.
The Problem
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user/repo.git'
Or:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please use a personal access token instead.
Step-by-Step Fix
Step 1: Update your credentials
git credential reject
Then:
git push
You will be prompted for new credentials.
Step 2: Use a personal access token (GitHub/GitLab)
# Use token as password when prompted
git push
Username: your-username
Password: your-personal-access-token
Step 3: Configure credential helper to cache credentials
git config --global credential.helper cache
Or store permanently (less secure):
git config --global credential.helper store
Step 4: Switch to SSH authentication
git remote set-url origin git@github.com:user/repo.git
Ensure SSH key is added:
ssh -T git@github.com
Expected:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Step 5: Check for expired tokens
# If using GitHub CLI
gh auth status
gh auth refresh
Step 6: Check remote URL format
# WRONG — includes username in URL
git remote set-url origin https://username@github.com/user/repo.git
# RIGHT — standard HTTPS URL
git remote set-url origin https://github.com/user/repo.git
Prevention Tips
- Use SSH keys instead of passwords
- Generate personal access tokens with appropriate scopes
- Use a credential manager (Git Credential Manager Core)
- Rotate tokens regularly and keep them in a password manager
- Use
gh auth loginfor GitHub CLI-based authentication
Common Mistakes with authentication failed
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro