
git push authentication failed: 3 Quick Fixes for SSH & HTTPS
If you’re a developer, chances are you’ve run into the dreaded message:
bash
fatal: Authentication failed
It usually appears when you try to run:
bash
git push origin main
This error can be frustrating, especially when it blocks your ability to push code to your remote repository. Whether you’re using SSH or HTTPS, this guide will help you fix the git push authentication failed
error with 3 quick and effective solutions.
✅ Why Does git push authentication failed
Happen?
Git requires authentication to ensure only authorized users can push changes to a repository. The error usually stems from:
- Expired or invalid credentials
- SSH key mismatch or misconfiguration
- Personal Access Token (PAT) not used correctly
- Changes in authentication method (e.g., GitHub disabling password authentication)
Let’s look at how to fix this fast.
🔧 Fix 1: Use SSH Instead of HTTPS (or Fix Your SSH Key)
🧠 Why it Works
If you’ve cloned your repo using HTTPS and you’re asked to log in every time — or you get an authentication failure — switching to SSH can save you a lot of hassle.
🔄 Steps to Fix
- Check if you already have an SSH key:
bash
ls ~/.ssh
Look for files like id_rsa
or id_ed25519
.
- If you don’t have one, generate an SSH key:
bash
ssh-keygen -t ed25519 -C "your_email@example.com"
When prompted, press Enter to accept defaults.
- Add the key to your SSH agent:
bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
- Copy your public key and add it to GitHub/GitLab:
bash
cat ~/.ssh/id_ed25519.pub
Then go to GitHub > Settings > SSH and GPG Keys > New SSH Key, and paste it there.
- Switch your remote from HTTPS to SSH:
bash
git remote set-url origin git@github.com:username/repo.git
- Test the connection:
bash
ssh -T git@github.com
If it says you’re authenticated, you’re good to go.
🔧 Fix 2: Use a Personal Access Token (For HTTPS Authentication)
🧠 Why it Works
GitHub and other platforms no longer support password-based authentication over HTTPS. Instead, they require a Personal Access Token (PAT).
🔄 Steps to Fix
- Go to your GitHub settings:
- Profile > Settings > Developer Settings > Personal Access Tokens
- Click on “Generate new token (Classic)”
- Give it a name, set expiration, and select scopes:
- At minimum, check:
repo
scope - Generate the token and copy it (you won’t see it again!)
- At minimum, check:
- Update your Git credentials with the token: When you push again, and Git asks for a username and password:
- Username: your GitHub username
- Password: paste the PAT you just generated
- (Optional) Store credentials using a helper:
bash
git config --global credential.helper cache
Or for a more permanent store:
bash
git config --global credential.helper store
Then push again:
bash
git push origin main
🔧 Fix 3: Re-authenticate or Clear Git Credentials
🧠 Why it Works
Sometimes cached or corrupted credentials cause problems — especially if you recently changed your password or token.
🔄 Steps to Fix
For macOS (Keychain Access):
- Open Keychain Access
- Search for
git
orgithub.com
- Delete old credentials
For Windows:
- Open Credential Manager
- Go to Windows Credentials
- Find entries for GitHub or Git and remove them
For Linux or CLI Users:
You can also manually clear stored credentials:
bash
git credential-cache exit
Or use:
bash
git config --global --unset credential.helper
After clearing, try pushing again:
bash
git push origin main
It will prompt you to enter your new credentials or token.
🧪 Bonus: Check Your Remote URL Format
You can always check your current remote URL using:
bash
git remote -v
Make sure it’s using the correct format:
- SSH:
git@github.com:username/repo.git
- HTTPS:
https://github.com/username/repo.git
Use this to troubleshoot if you’re accidentally mixing protocols.
🛠️ When to Use SSH vs HTTPS
Choosing between SSH and HTTPS depends on your workflow and environment. SSH is generally preferred for frequent contributors because it’s more secure and avoids having to enter your credentials repeatedly. Once configured, SSH keys make authentication seamless. On the other hand, HTTPS is easier for beginners and works better behind strict corporate firewalls, but it now requires personal access tokens for authentication, which adds a bit of setup overhead.
🧩 Common Pitfall: Cloning with One Protocol, Pushing with Another
A sneaky reason for git push authentication failed
is accidentally mixing SSH and HTTPS in the same project. You might clone using HTTPS, then try pushing with SSH—or vice versa—without realizing it. This causes mismatches during authentication. Always double-check your remote origin with git remote -v
and make sure you’re using one consistent protocol across all operations in your repository.
🧪 Advanced Tip: Use .gitconfig
to Set Protocols Globally
For users managing multiple repositories, setting global preferences in your .gitconfig
file can save time. For example, you can force Git to always use SSH by setting the URL rewrite rule globally:
bash
git config --global url."git@github.com:".insteadOf "https://github.com/"
This ensures any future clones or remotes default to SSH even if you accidentally paste an HTTPS URL. It’s a small trick that can prevent authentication issues from popping up in the future.
🧠 Final Thoughts
The git push authentication failed
error is common, but luckily, the fixes are straightforward. Whether you prefer SSH or HTTPS, the key is to:
- Use SSH keys properly
- Use a Personal Access Token (PAT) for HTTPS
- Clear and re-authenticate your Git credentials when things break
With these three fixes, you’ll be back to pushing code in no time.