Git remote: invalid username or password solution
Содержание:
remote: invalid username or password
The most common cause of this error is that you have inserted an invalid username or password. Before you try any other solution, make sure you have correctly typed in your username and password to authenticate to the Git server.
You may see this error when you clone a repository that is private or when you try to pull from or push to an existing repository without being correctly authenticated.
There are other causes to this error. In many cases, Git does not provide specific explanations as to why authentication has failed. Git often just tells you that your credentials are invalid, like this error message suggests.
These causes include:
- Setting the wrong URL
- Using 2FA on a GitHub account
Let’s discuss these causes individually.
Cause #1: Setting the Wrong URL
We’re going to clone one of our Git repositories using SSH. The repository is called career-karma-tutorials/ck-git. This repository is private so we need to authenticate to clone the repository. We can clone the repository using the git clone command:
Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!
Venus, Software Engineer at Rockbot
Find Your Bootcamp Match
Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!
Venus, Software Engineer at Rockbot
Find Your Bootcamp Match
We have used an SSH URL to authenticate with our server.
Let’s run our command and see what happens:
Password for ‘https://git@github.com’:
remote: Invalid username or password.
fatal: Authentication failed for ‘https://git@github.com/career-karma-tutorials/ck-git.git/’
Our command does not run successfully. The issue is that we’ve tried to authenticate using both HTTP and SSH authentication. This is clear if we look at the start of our URL:
The HTTPS states we want to authenticate over the web. The git@github.com states we want to connect over SSH. When we run our command, Git tries to connect via HTTP to the git@github.com server, which does not accept HTTP authentication.
We can fix this error by using either HTTP or SSH authentication:
git clone https://github.com:career-karma-tutorials/ck-git (HTTP) git clone git@github.com:career-karma-tutorials/ck-git.git (SSH)
git clone https://github.com:career-karma-tutorials/ck-git (HTTP)
git clone git@github.com:career-karma-tutorials/ck-git.git (SSH)
In our case, we are using HTTPS authentication to access our repository, so we’ll run the first command. Let’s see what happens:
Cloning into 'ck-git'... remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (6/6), done. Our repository is successfully cloned.
Cause #2: Using 2FA on a GitHub Account
Once you have enabled 2 Factor Authentication (2FA) on GitHub, you cannot use your GitHub password on the command line. Instead, you have to use a personal access token.
Personal access tokens are used to authenticate you for personal applications and on the command line. The command line does not tell us we need to generate a personal access token, which is why this solution is often overlooked by programmers.
To generate a personal access token, we need to open GitHub and do the following:
- Navigate to the “Settings” page
- Click “Developer settings” in the sidebar
- Click “Personal access tokens” in the sidebar
- Click “Generate new token”
- Fill in the form to create a new token
This will give us a token we can copy to the clipboard. This token is only visible once so you should take a note of your token.
Once we have this token, we can use it as our password. We’re going to use the HTTP URL we wrote earlier to access our repository:
git clone https://github.com:career-karma-tutorials/ck-git
Git asks us to authenticate when we clone our repository:
Username for ‘https://github.com’:
Password for ‘https://github.com’:
In the password field, we have pasted the personal access token we generated earlier. We are then authenticated and our repository is cloned to our local machine.