To access private remote repositories and modify remote repositories using the Git command line, you must be authenticated. If you try to access or modify a repository and enter the wrong authentication credentials, you’ll encounter the
remote: invalid username or password error.
In this guide, we discuss the most common causes of this error. We’ll walk through a few examples of how to fix this error so you can solve the problem you are facing.
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:
git clone https://firstname.lastname@example.org:career-karma-tutorials/ck-git.git
We have used an SSH URL to authenticate with our server.
Let’s run our command and see what happens:
Password for ‘https://email@example.com’:
remote: Invalid username or password.
fatal: Authentication failed for ‘https://firstname.lastname@example.org/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 email@example.com states we want to connect over SSH. When we run our command, Git tries to connect via HTTP to the firstname.lastname@example.org 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 email@example.com:career-karma-tutorials/ck-git.git (SSH)
git clone https://github.com:career-karma-tutorials/ck-git (HTTP)
git clone firstname.lastname@example.org: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.
The “remote: invalid username or password” error informs you that you have incorrectly authenticated to a Git server.
To solve this error, make sure that you have used the right username and password and that you are trying to access a Git repository using the correct URL. If you have 2FA enabled, make sure you authenticate using a personal access token instead of your password.