|||

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 your_email@example.com 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

If you have any feedback, or if this article was helpful, let me know on Twitter.

Sources:

Up next product development resources This is a work-in-progress list with articles, blog posts and videos on product development. Jobs-to-be-Done e-book by Intercom (link) Know Your Asynchronous communication for open source projects This is a suggestion for organising asynchronous communications for open source projects. I first drafted a modified version of this blog post as a
Latest posts how to ditch Facebook, Twitter, Instagram, and LinkedIn — and still follow people and organizations you like Tour of Queyras (GR58), 140 km, 7,000m elevation, 7 days Facebook’s “privacy notifications” and “co creation strategies” to protect people’s privacy Privacy-friendly alternatives to Google products Privacy 101: simple steps to protect your privacy online sailing twtxt Asynchronous communication for open source projects git - remote: Invalid username or password. fatal: Authentication failed for [remote’s URL] product development resources 10 Days in Silence: Vipassana Meditation We don’t care about personas “Building a more private web” by Google — comment on Reddit Replacing Facebook with newsletters Now Portugal Startup Ecosystem Iran Startup Ecosystem What’s a community for? Editorial on communities for Daphni’s newsletter How to put your expertise in a box and sell it France Blockchain Ecosystem « French Tech Communauté » : quelle opportunité pour l’État ? Don’t Let Your Browser Suck Up All Your Data 25 quotes from startup communities by Brad Feld Mapping of French Tech Community in Asia How to build a community at 33,000 feet in 80 hours WhatsApp chose convenience over privacy, here’s how you can fix this Why I Told My Friends To Stop Using WhatsApp and Telegram From Corporate to Startups