650

I have a project hosted on GitHub. I fail when trying to push my modifications on the master. I always get the following error message

Password for 'https://git@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://git@github.com/eurydyce/MDANSE.git/'

However, setting my ssh key to github seems ok. Indeed, when I do a ssh -T git@github.com I get

Hi eurydyce! You've successfully authenticated, but GitHub does not provide shell access.

Which seems to indicate that everything is OK from that side (eurydyce being my github username). I strictly followed the instructions given on github and the recommendations of many stack discussion but no way. Would you have any idea of what I may have done wrong?

2
  • 2
    I'm not sure I should post an answer or not(high chance get downvote), since I solved this error message by changed the password in github settings after tried many attempts with the "correct"(check by inspect element) password.
    – 林果皞
    Aug 30, 2017 at 17:22
  • 2
    Some organizations have SAML SSO enabled. In this case, best is to use personal access token. If you are already logged in, please re-login and give Github Desktop/Sourcetree permission Jan 8, 2021 at 4:40

32 Answers 32

1468

After enabling Two Factor Authentication (2FA), you may see something like this when attempting to use git clone, git fetch, git pull or git push:

$ git push origin master
Username for 'https://github.com': your_user_name
Password for 'https://your_user_name@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/your_user_name/repo_name.git/'

Why this is happening

From the GitHub Help documentation:

After 2FA is enabled you will need to enter a personal access token instead of a 2FA code and your GitHub password.

...

For example, when you access a repository using Git on the command line using commands like git clone, git fetch, git pull or git push with HTTPS URLs, you must provide your GitHub username and your personal access token when prompted for a username and password. The command line prompt won't specify that you should enter your personal access token when it asks for your password.

How to fix it

  1. Generate a Personal Access Token. (Detailed guide on Creating a personal access token for the command line.)
  2. Copy the Personal Access Token.
  3. Re-attempt the command you were trying and use Personal Access Token in the place of your password.

Related question: https://stackoverflow.com/a/21374369/101662

16
  • 28
    Kudo's and upvotes; the new token generation is what i needed. Also; the "Copy code" included additional (invalid) characters so I had to manually enter my .... 20[?] digit random key.
    – th3byrdm4n
    Apr 27, 2016 at 23:03
  • 23
    This was my issue, after adding 2 factor auth, which i suspected, however the documentation on this was lacking. Thanks for the answer!
    – Pogrindis
    Dec 21, 2016 at 13:34
  • 13
    @BeauSmith: That's way more substantive an edit than we normally allow. You basically completely rewrote this answer. Please post that as your own, separate answer instead. Also see What do you do if someone basically re-writes your whole answer?
    – Martijn Pieters
    May 7, 2017 at 9:09
  • 8
    Let's all please allow the decision to rollback or not to be decided by @Oliver here - there's a perfectly sound argument either way
    – Flexo
    May 8, 2017 at 6:46
  • 13
    Editor here… As it seems that people marking this answer as correct (like myself) arrive on this page for a problem which is slightly different than the asker intended, I was trying to add some "google juice" terms and make the answer more visually appealing such that others seeking this solution can find it. Trying to make things better for the community. 😀
    – Beau Smith
    May 8, 2017 at 17:26
411

https://git@github.com/eurydyce/MDANSE.git is not an ssh url, it is an https one (which would require your GitHub account name, instead of 'git').

Try to use ssh://git@github.com:eurydyce/MDANSE.git or just git@github.com:eurydyce/MDANSE.git

git remote set-url origin git@github.com:eurydyce/MDANSE.git

The OP Pellegrini Eric adds:

That's what I did in my ~/.gitconfig file that contains currently the following entries [remote "origin"] url=git@github.com:eurydyce/MDANSE.git

This should not be in your global config (the one in ~/).
You could check git config -l in your repo: that url should be declared in the local config: <yourrepo>/.git/config.

So make sure you are in the repo path when doing the git remote set-url command.


As noted in Oliver's answer, an HTTPS URL would not use username/password if two-factor authentication (2FA) is activated.

In that case, the password should be a PAT (personal access token) as seen in "Using a token on the command line".

That applies only for HTTPS URLS, SSH is not affected by this limitation.

9
  • oups ! You are right, making those changes in my local config made everything work. Thanks a lot.
    – Eurydice
    Mar 27, 2015 at 10:19
  • 233
    Don't understand this answer at all
    – Cloud
    Apr 20, 2018 at 11:13
  • 3
    @Clo sure, what part is lacking clarity, how may I improve this answer?
    – VonC
    Apr 20, 2018 at 11:16
  • When I did this (replacing the username and repo name with mine), I get "Permission denied (publickey)." I would like to log in with username and password if possible. Jan 8, 2020 at 21:25
  • 1
    @byronaltice If this is SSH, that means you have a ~/.ssh/config or %USERPROFILE%\.ssh\config file, with a github.com entry in it.
    – VonC
    Feb 3, 2020 at 22:06
139

Solution steps for Windows users:

  1. Control Panel
  2. Credential Manager
  3. Click Windows Credentials
  4. In Generic Credential section ,there would be git url, update username and password
  5. Restart Git Bash and try for clone

Note: If you didn't find git url in Generic Credential section then follow below answer https://stackoverflow.com/a/55858690/7372432

5
  • 5
    I was having this problem with a company version of github (so using local username/password). This solution fixed it.
    – mkennedy
    Nov 26, 2018 at 22:02
  • 1
    In new windowses, you can search for "Windows credentials" or similar in your language. It'll pop up a result and you can try this solution. Oct 17, 2019 at 6:20
  • 2
    Perfect solution for on premises github, where companies obliges you to use https Apr 23, 2020 at 12:20
  • I was following another thread to refresh the credentials, with no luck. You solution works perfectly
    – Heinz
    Mar 31, 2021 at 17:06
  • Same issue: git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. Jul 9, 2021 at 17:47
79

If like me you just updated your password and ran git push to run into this issue, then there's a super easy fix.

For Mac users only. You need to delete your OSX Keychain access entries for GitHub. You can do it via terminal by running the following commands.

Deleting your credentials via the command line

Through the command line, you can use the credential helper directly to erase the keychain entry.

To do this, type the following command:

git credential-osxkeychain erase
host=github.com
protocol=https

# [Now Press Return]

If it's successful, nothing will print out. To test that it works, try and clone a repository from GitHub or run your previous action again like in my case git push. If you are prompted for a password, the keychain entry was deleted.

5
  • 3
    For windows: git credential-wincred erase host=github.com protocol=https
    – Juan Uribe
    Oct 29, 2018 at 19:14
  • I encountered this problem on a BitBucket repo. This answer with host=bitbucket.org instead of GitHub fixed it for me.
    – gutch
    Oct 24, 2019 at 22:41
  • I had changed my Github password after which git pull wasn't working for me. This worked for me.
    – Gokul
    Mar 27, 2021 at 8:06
  • That's the right answer in case someone get stuck after the last Github authentication update to enforce login with a username/token instead of username/password.
    – Ibrahim.H
    Oct 23, 2021 at 15:37
  • Great, this exactly what I need in order to replace expired token with new one. thank you.
    – CharlesC
    Sep 2, 2022 at 14:29
37

When using the https:// URL to connect to your remote repository, then Git will not use SSH as authentication but will instead try a basic authentication over HTTPS. Usually, you would just use the URL without a username, e.g. https://github.com/username/repository.git, and Git would then prompt you to enter both a username (your GitHub username) and your password.

If you use https://something@github.com/username/repository.git, then you have preset the username Git will use for authentication: something. Since you used https://git@github.com, Git will try to log in using the git username for which your password of course doesn’t work. So you will have to use your username instead.

The alternative is actually to use SSH for authentication. That way you will avoid having to type your password all the time; and since it already seems to work, that’s what you should be using.

To do that, you need to change your remote URL though, so Git knows that it needs to connect via SSH. The format is then this: git@github.com:username/repository. To update your URL use this command:

git remote set-url origin git@github.com:username/repository
2
  • when trying your command I get the following message warning: remote.origin.url has multiple values. This may be due to the fact that I already have an remote tag in my ~/.gitconfig file which contains the value [remote "origin"] url=git@github.com:eurydyce/MDANSE.git.
    – Eurydice
    Mar 27, 2015 at 10:10
  • I've been searching for hours already on why I couldn't push my changes, and this answer finally gives a perfect explanation and helped me fix it, thanks!
    – Sander Lam
    Sep 15, 2021 at 21:54
34

Instead of git pull also try git pull origin master

I changed password, and the first command gave error:

$ git pull
remote: Invalid username or password.
fatal: Authentication failed for ...

After git pull origin master, it asked for password and seemed to update itself

2
  • 1
    This is what I needed!
    – Yogesh D
    Mar 8, 2021 at 15:26
  • 1
    This is the simplest way to do it and it works! Aug 5, 2021 at 19:41
32

2FA is enabled and getting error remote: Invalid username or password. fatal: Authentication failed for

If you set 2FA is enabled in GitHub you will need to enter a personal access token instead of a 2FA code and your GitHub password.

How to fix it

  1. https://github.com/settings/tokens generated token
  2. Copy the Personal Access Token
  3. Now enter Personal Access Token in the place of your password during git operation
2
  • This was it! Could you explain what's going on here? Would I need the token for any Github related use from nowon?
    – Yashank
    Jun 15, 2021 at 19:49
  • 1
    After this, then follow @Anil_Reddy's answer.
    – Magne
    Jun 22, 2021 at 19:57
26

just try to push it to your branch again. This will ask your username and password again, so you can feed in the changed password. So that your new password will be stored again in the cache.

4
  • 5
    Not sure why this got a downvote. In my case my credentials had been updated, and the second git push prompted for the appropriate update, as this answer indicates. Aug 28, 2018 at 20:15
  • Not sure, what you meant, it is not asking for new credentials executing git push origin master...
    – Betlista
    May 14, 2019 at 21:21
  • Nice! Same thing if you're trying to git clone something but it tells you your username/password are incorrect. If either have recently changed, just run git clone <myprojecturl> again and this time it will ask you to re-enter your username and password. May 26, 2021 at 17:07
  • very strange. Why it fails the first time but not the second time. (Vscode extension does the work) Jan 20 at 7:49
15

This is the answer.

Set the github token:

https://github.com/settings/tokens

And then:

git remote set-url origin https://[token]@github.com/your_repository
1
  • This worked for me, however, after running git remote -v I noticed my repository was using HTTPS for fetch and push whereas I thought it was using SSH. If you already have SSH auth working (test with ssh -T git@github.com), then you should just be able to set-url to the SSH one. Easy way to find the SSH url is to go to your repository on GitHub.com and click on the green "Code" button's "SSH" tab and copy the link. Then run the command in your repository: git remote set-url origin git@github.com:your-user-or-org/your-repo-name.git (replace the last bit with your copied link). Sep 22, 2022 at 17:41
12

I am getting this while cloning app from bitbucket:

Cloning into 'YourAppName'...
Password for 'https://youruser id': 
remote: Invalid username or password

I solved it. Here you need to create password for your userid

  1. Click on Your profile and settings Click on Your profile and settings

  2. Then Create app password choose your name password will generated ,paste that password to terminal Create app password

12

That problem happens sometimes due to wrong password. Please check if you are linked with AD password (Active Directory Password) and you recently changed you AD password but still trying git command with old password or not.

Update old AD password

Control Panel > Credential Manager > Windows Credential > change github password with my new AD password
0
12

I have got the success using the following commands.

git config --unset-all credential.helper
git config --global --unset-all credential.helper
git config --system --unset-all credential.helper

Try and let me know if these are working for you.

1
  • After this, doing git push origin master in any repo you have should prompt you again for your username (same as before) and password (use the Personal Access Token here).
    – Magne
    Jun 22, 2021 at 19:58
10

No need to rely on Generating a Personal Access Token and then trying and use Personal Access Token in the place of your password.

Quick fix is to set your remote URL to point to ssh not https.

Do this git remote set-url origin git@github.com:username/repository

0
10

I did:

$git pull origin master

Then it asked for the [Username] & [Password] and it seems to be working fine now.

3
  • This worked for me. I've used fetch instead pull but anyway it doesn't matter. Cool!
    – atereshkov
    May 30, 2020 at 14:11
  • Beginning August 13, 2021 this will not supported
    – K.Amanov
    Mar 6, 2021 at 11:13
  • Feel free to edit and add what works @ZEHINZ! haven't run into this issue for a while so I am not even sure what the updated command is!
    – ZaMy
    Mar 8, 2021 at 0:10
9

If you have just enabled 2FA :

Modify hidden config file in ./git hidden folder as follow :

[remote "origin"]
    url = https://username:PUT_YOUR_2FA_TOKEN_HERE@github.com/project/project.git
3
  • but what if the folder did not exist yet? I am unable to do even git clone of a private repo in github
    – Volatil3
    Jan 15, 2021 at 9:20
  • Thank you. I spent more than two hours on this issue. Your answer saved my day Nov 15, 2021 at 12:34
  • Me too. I wasn't prompted to reenter the new token. This was the only way to get it working.
    – Starx
    Sep 4, 2022 at 7:47
8

Try this:

# git remote set-url origin git@github.com:username/repository
7

Run Below command, and after than on every push and pull it will ask you to enter the username and password.

  git config credential.helper ""

enter image description here

now when you pull/push you will be asked for git credentials. weather you are running through command prompt or Intellij Git.

4

Disabling 2 factor authentication at github worked for me.

I see that there is a deleted answer that says this, with the deletion reason as "does not answer the question". If it works, then I think it answers the question...

1
  • This is weird. Same happened with me. I just disabled two-factor authentication and it works now for me. :)
    – abhijeetps
    Jun 14, 2019 at 12:36
3

You might be getting this error because you have updated your password. So on Terminal first make sure you clear your GitHub credentials from the keychain and then push your changes to your repo, terminal will ask for your username and password.

0
3

In case you get this error message in this situation:

  • using github for entreprise
  • using credential.helper=wincred in git config
  • using your windows credentials which you changed recently

Then look at this answer: https://stackoverflow.com/a/39608906/521257

Windows stores credentials in a credentials manager, clear it or update it.

3
  1. Control panel
  2. Credential manager
  3. Look for options webcredentials and windows credentials
  4. in either one you will find github credentials fix it with correct credentials
  5. open new instance of git bash you should be able to perform your git commands.

This worked for me, I was able to pull and push into my remote repo.

3

I had the same issue. And I solved it by changing the remote branch's path from https://github.com/YourName/RepoName to git@github.com:YourName/RepoName.git in the repo's settings of the client app.

1
  • 1
    You are trying to solve the problem using SSH link. I would prefer working with HTTPS and your answer can not help me. Feb 5, 2021 at 11:09
3

I'm constantly running into this problem. Make sure you set git --config user.name "" and not your real name, which I've done a few times..

1

I just disable the Two-factor authentication and try again. It works for me.

0
1

Since you probably want to keep 2FA enabled for your account, you can set up a ssh key and that way you won't need to type your Github credentials every time you want to push work to Github.

You can find all the ssh setup steps in the documentation. First, make sure you don't currently have any ssh keys (id_rsa.pub, etc.) with $ ls -al ~/.ssh

1

I fixed my issue by installing GitHub CLI and running gh auth login

See: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git#github-cli

1

I had the same issue

$ git clone https://github.com/sample-url.git
Cloning into 'Project'...
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/sample-url.git/'

I just git init first and then git clone <clone-url>

git init
git clone https://github.com/your-clone-Url

It worked for me.

1
  • Strange. It should not. All you did was to clone a repository inside another local repository. That should have no effect on the authentication needed by your git clone command.
    – VonC
    Jul 28, 2022 at 7:39
0

There is a issue on Windows using cmd-Greetings

There is a issue on Windows using cmd-Greetings who will not let you clone private repositories. Remove that cmd-greeting described in this documentation (keyword Command Processor):

Known-Issues

I can confirm that other clients like SourceTree, GitKraken, Tower and TortoiseGit affected to this issue too.

0

There are many reasons why this might happen. In my case, none of the solutions worked. In particular, git pull origin master did not ask me for my username and password.

I was on Windows with a github password recently changed. I was using the credential manager to manage my password. Here is what worked for me:

  1. Confirm you are using the credential manager for git:

    git config --list … credential.helper=manager

  2. Run a new command prompt as administrator

  3. List all stored credential with cmdkey /list from C:\WINDOWS\system32>

  4. Remove the github target with cmdkey /delete:<target name>. In my case, the target name was github.<companyname>.com

  5. Open a new prompt and run a git command. You should get a popup asking for your usernmame and password. After providing the new credentials, it won't ask you for it again.

0

When I faced this issue all I did to resolve it was to Generate new token from my github dashboard and paste the following code in my terminal

$ git remote set-url origin https://your-github-username:your-github-token@github.com/your-github-username/your-github-repo.git

Not the answer you're looking for? Browse other questions tagged or ask your own question.