How to Fix Authentication Failed Error in Git
The
authentication failed error in Git usually happens when your credentials are wrong or outdated. Fix it by updating your username and password or switching to SSH keys for authentication.Why This Happens
This error occurs when Git cannot verify your identity with the remote server. Common reasons include wrong username or password, expired tokens, or using HTTPS URLs without proper credentials.
bash
git clone https://github.com/username/repo.git
# When prompted, entering wrong password or token causes failureOutput
fatal: Authentication failed for 'https://github.com/username/repo.git/'
The Fix
Update your saved credentials or switch to SSH authentication. For HTTPS, clear old credentials and enter the correct username and personal access token (PAT). For SSH, generate and add your SSH key to GitHub or your Git server.
bash
git credential-manager reject https://github.com # Then try cloning again and enter correct credentials # Or switch to SSH: git clone git@github.com:username/repo.git
Output
Cloning into 'repo'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
Receiving objects: 100% (10/10), done.
Prevention
Use SSH keys instead of HTTPS for smoother authentication. Regularly update your personal access tokens before they expire. Store credentials securely using Git credential helpers to avoid repeated prompts.
Related Errors
- Permission denied (publickey): Happens when SSH keys are missing or not added to your Git server.
- Repository not found: Occurs if you use wrong URLs or lack access rights.
Key Takeaways
Authentication failed means Git can't verify your identity with the remote server.
Update or clear saved credentials and use correct username and token for HTTPS.
Switch to SSH keys for easier and more secure authentication.
Use Git credential helpers to store credentials safely and avoid repeated prompts.
Regularly renew personal access tokens to prevent expiration issues.