May 6, 2020

git - remote: Invalid username or password. fatal: Authentication failed for [remote’s URL]

This is a troubleshooting article for the following message:

$ git commit -m "your message"
$ git push origin master
> Username for 'https://github.com': USERNAME
> Password for 'https://username@github.com': 
> remote: Invalid username or password.
> fatal: Authentication failed for 'https://github.com/USERNAME/REPOSITORY.git/'

  1. Check if you have write” access to the repository
  2. Check the remote repository’s URL
  3. Add/update the remote repository’s URL
  4. Check for existing SSH keys
  5. Generate SSH key
  6. Add SSH key to the ssh-agent
  7. Add SSH key to your GitHub account

You staged your files by running git add.

You committed your changes by running git commit -m "your message".

And you attempted to push to a remote repository by running $ git push origin [remote] but this message pops up:

> Username for 'https://github.com': 
> Password for 'https://username@github.com': 

Not what you expected.

Still, you give it a try:

> Username for 'https://github.com': [enter username]
> Password for 'https://username@github.com': [enter password]

And get an error message:

> remote: Invalid username or password.
> fatal: Authentication failed for 'https://github.com/USERNAME/REPOSITORY.git/'

Let’s troubleshoot.


NOTE

Don’t get confused by articles suggesting to create a personal access token. That is for repositories cloned with HTTPS, not SSH. In this article, we troubleshoot for repositories cloned with SSH.


Check if you have write” access to the repository

Check that you have write” access to the remote repository you are trying to push to. You can check so by trying to edit the README.md on the remote repository (i.e. directly on GitHub) to which you want to push.

If you see that blue text box above the markdown editor of README.md, that means you don’t have access. You’ll have to ask for write” access to the admin of the remote repository you want to push to.

If you have access, let’s check if you are pushing to the right remote repository.

Check the remote repository’s URL

In your terminal run:

$ git remote -v
> origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin  https://github.com/USERNAME/REPOSITORY.git (push)

Reminder: we are troubleshooting for repositories cloned with SSH.

If those URLs use HTTPS protocol you want to update the remote repository

If git remote -v is empty, you want to add a remote repository.

Add/update the remote repository’s URL

Go to the remote repository you want to push to:

https://github.com/USERNAME/REPOSITORY

Click the Clone or download” button (pink frame) and then copy the remote repository’s URL by clicking on the clipboard icon (green frame.)

In your terminal run:

$ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Verify the remote repository’s URL:

$ git remote -v
> origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
> origin  git@github.com:USERNAME/REPOSITORY.git (push)

See, the remote repository, has been updated and is no longer using HTTPS protocol. It is using SSH protocol.

Now try to push to the remote repository. If that still does not work you want to check where you are at with SSH keys.

Reminder: you don’t need to create a personal access token. That is for remote repositories cloned with HTTPS. We troubleshoot for remote repositories cloned with SSH.

Check for existing SSH keys

In your terminal run:

$ ls -al ~/.ssh

If you have SSH keys locally, you should see the files in your .ssh directory:

config.txt
id_rsa
id_rsa.pub

If you don’t, you want to generate new SSH keys.

Generate SSH key

In your terminal run:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Substitute with the email address associated with your GitHub account.

You should see:

> Generating public/private rsa key pair.
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Press enter for the default location. Next enter a password:

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Once you’ve generated a SSH key you want to add it to the ssh-agent.

Add SSH key to the ssh-agent

Start the ssh-agent. In your terminal run:

$ eval "$(ssh-agent -s)"

You should see:

> Agent pid [number]

If you’re using macOS Sierra 10.12.2 or later, go to Finder, look for ~/.ssh/config and copy/paste the following to the SSH config file:

(Cmd + Shift + . (dot) to see hidden files on Mac.)

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

Now you want to add your SSH private key to the ssh-agent and store your passphrase in the keychain. In your terminal run:

$ ssh-add -K ~/.ssh/id_rsa

Last but not least, add the SSH key to your GitHub account.

Add SSH key to your GitHub account

Copy the SSH key to your clipboard by running:

$ pbcopy < ~/.ssh/id_rsa.pub

Go to your GitHub account add the SSH key:

Settings > SSH and GPG keys > New SSH key

Add the name of your computer, paste the SSH key and click:

Add SSH key

Sources:


git command-line interface (cli)

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88