675

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?

Luc
  • 5,339
  • 2
  • 48
  • 48
Eurydice
  • 8,001
  • 4
  • 24
  • 37
  • 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 '17 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 – Abhay Shiro Jan 08 '21 at 04:40

35 Answers35

1483

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

Community
  • 1
  • 1
Oliver
  • 35,233
  • 12
  • 66
  • 78
  • 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 '16 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 '16 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?](//meta.stackoverflow.com/q/348873) – Martijn Pieters May 07 '17 at 09: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 08 '17 at 06:46
  • 2
    @MartijnPieters - Thanks for the link to the discussion. I didn't intend to create any drama. Just want to make things better for everyone. – Beau Smith May 08 '17 at 17:22
  • 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 08 '17 at 17:26
  • I used this to get access via GitBash after adding 2step to my account i couldn't push to master. My question is will i need to create a new token every time i want to use this. Isnt there a way of setting my machine up to be able to access Git without the twostep or prompt for the code? – Linda Lawton - DaImTo May 27 '17 at 15:44
  • 2
    Why github documentation doesn't mention this (token key required instead of password) when create new repository ? – 林果皞 Sep 29 '17 at 16:45
  • 8
    GitHub should be more clear on this notice when users enable 2FA on their accounts. THIS IS A FATAL ERROR. – Nick Song Nov 11 '17 at 04:27
  • Make sure to give yourself appropriate permissions. Not giving any hoping to get through this as fast as possible will result in a 403=) – joedragons Dec 18 '17 at 22:45
  • 1
    I had to allow the token full "repo" permissions before I could push to a public GitHub repository. Allowing only the "access to public repositories" did not work. – Akseli Palén Apr 19 '20 at 19:54
  • Thanks for this - using my Personal Access Token rather than my GitHub password worked for me. Upvoted. – David Pears Apr 20 '21 at 07:41
  • Confirming this using Personal Access Token instead of password in "password" section succeeds git commands. It's a one time procedure, thanks for the suggestion – Poli Jun 07 '21 at 14:46
  • I had to enable 2FA in order to use GitHub Co-pilot, then I ran into the problems described above. This answer here is very useful. – jed Jul 21 '21 at 11:29
  • Note: This solution worked for me in Git Bash but not in normal command promt. – chirag soni Oct 21 '21 at 17:35
  • this worked for me on normal command prompt as well, Thanks a lot that worked! – Prasad Kumarasinghe Apr 11 '22 at 09:15
416

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • oups ! You are right, making those changes in my local config made everything work. Thanks a lot. – Eurydice Mar 27 '15 at 10:19
  • 238
    Don't understand this answer at all – Cloud Apr 20 '18 at 11:13
  • 3
    @Clo sure, what part is lacking clarity, how may I improve this answer? – VonC Apr 20 '18 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. – Aaron Franke Jan 08 '20 at 21:25
  • @AaronFranke username/password means HTTPS URL, not SSH one. – VonC Jan 08 '20 at 21:41
  • I had to remove the git@ part to get this to work... so github.com:organization/repo.git – byronaltice Feb 03 '20 at 21:28
  • 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 03 '20 at 22:06
  • @VonC yes I do, it's for my ssh key so I don't have to put in a password every time. I think this should be part of the answer for anyone who wants to use an ssh key, seems like a lot more people than me would not want to type in their password every time. – byronaltice Feb 07 '20 at 16:38
  • @byronaltice I agree, but the answer is about HTTPS URL, not SSH. For SSH and config, see https://stackoverflow.com/a/14681936/6309 – VonC Feb 07 '20 at 16:48
  • After generating the access token, rewrite the same command and enter username and password, that's it. – Muhammad Afzal Apr 10 '23 at 06:31
142

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

Fergie
  • 5,933
  • 7
  • 38
  • 42
amoljdv06
  • 2,646
  • 1
  • 13
  • 18
  • 5
    I was having this problem with a company version of github (so using local username/password). This solution fixed it. – mkennedy Nov 26 '18 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. – Charles Tempo Oct 17 '19 at 06:20
  • 2
    Perfect solution for on premises github, where companies obliges you to use https – avi.elkharrat Apr 23 '20 at 12:20
  • I was following another thread to refresh the credentials, with no luck. You solution works perfectly – Heinz Mar 31 '21 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. – Thirsty Six Jul 09 '21 at 17:47
83

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.

Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
  • 3
    For windows: git credential-wincred erase host=github.com protocol=https – Juan Uribe Oct 29 '18 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 '19 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 '21 at 08: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 '21 at 15:37
  • Great, this exactly what I need in order to replace expired token with new one. thank you. – CharlesC Sep 02 '22 at 14:29
39

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

Paul
  • 3,920
  • 31
  • 29
38

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
poke
  • 369,085
  • 72
  • 557
  • 602
  • 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 '15 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 '21 at 21:54
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
Sagar Jethi
  • 2,313
  • 20
  • 20
28

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.

Shiva Rajkumar
  • 291
  • 3
  • 2
  • 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. – Digital Trauma Aug 28 '18 at 20:15
  • Not sure, what you meant, it is not asking for new credentials executing `git push origin master`... – Betlista May 14 '19 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 ` again and this time it will ask you to re-enter your username and password. – Kyle Vassella May 26 '21 at 17:07
  • very strange. Why it fails the first time but not the second time. (Vscode extension does the work) – KansaiRobot Jan 20 '23 at 07:49
18

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
Julián Oviedo
  • 564
  • 5
  • 19
  • 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). – Kerry Johnson Sep 22 '22 at 17:41
17

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
Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Anil Reddy
  • 181
  • 1
  • 2
  • 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 '21 at 19:58
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

barbsan
  • 3,418
  • 11
  • 21
  • 28
prachit
  • 365
  • 3
  • 10
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
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
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

Divyanshu Rawat
  • 4,421
  • 2
  • 37
  • 53
10

I did:

$git pull origin master

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

ZaMy
  • 498
  • 5
  • 12
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
Pierre
  • 91
  • 1
  • 2
9

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.

Arpan Saini
  • 4,623
  • 1
  • 42
  • 50
8

Try this:

# git remote set-url origin git@github.com:username/repository
Pang
  • 9,564
  • 146
  • 81
  • 122
Sushil
  • 121
  • 1
  • 7
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...

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • This is weird. Same happened with me. I just disabled two-factor authentication and it works now for me. :) – abhijeetps Jun 14 '19 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.

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.

BiAiB
  • 12,932
  • 10
  • 43
  • 63
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.

Ajay Deepak
  • 471
  • 5
  • 9
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
    You are trying to solve the problem using SSH link. I would prefer working with HTTPS and your answer can not help me. – Pritesh Gohil Feb 05 '21 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..

Arthur Bowers
  • 581
  • 5
  • 9
1

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

Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
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

Sarah Dwyer
  • 529
  • 5
  • 22
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

Bill Christo
  • 1,223
  • 9
  • 8
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.

Brijesh Ray
  • 779
  • 8
  • 15
  • 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 '22 at 07:39
1

If someone faces this issue on VS Code then changing the git protocol setting to ssh will solve the purpose. git protocol vs code setting

Mujeeb Shaikh
  • 169
  • 11
1

None of the answers were working for me. In my case I needed access for a given organisation. So renewing the token was not enough, I also had to grant access for that particular organisation.

This is the complete process I had to go through to restore access to the repositories of my organisation:

  1. Regenerate token (in my case I already had a token which had expired) enter image description here
  2. Copy the token
  3. Add permissions more my organisation: enter image description here
  4. Use the token to access your repo, for example by doing:
    % git pull
    Username for 'https://github.com': YOUR_USER
    Password for 'https://YOUR_USER@github.com': PASTE_TOKEN
    
de.
  • 7,068
  • 3
  • 40
  • 69
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.

Grim
  • 1,938
  • 10
  • 56
  • 123
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.

Lolo
  • 3,935
  • 5
  • 40
  • 50
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

Paul Kumchi
  • 211
  • 4
  • 7
0

after typing git pull origin (main or master) it gave me to type my username and password

after this I tried to use pushing using git push origin main

then it worked with the same approach it asked me to type my username then password I typed my generated token from GitHub then it started to push successfully

0

My exact error was

Remote: Invalid username or password.
Git failed with a fatal error.
Git failed with a fatal error.
Authentication failed for <repo name>

after doing a git Pull from the Visual Studio UI.


Solution

  1. Open a command line window
  2. Go to the root of the repo
  3. git pull
  4. Watch git output the changes it pulled!

A simple solution to a frustrating problem for Visual Studio users!

Super Jade
  • 5,609
  • 7
  • 39
  • 61
-3

This solution worked for me:

  1. open Control Panel
  2. Go to Credential Manager
  3. Click Window Credentials
  4. In Generic Credential section ,there would be git url, update username and password
  5. Restart Git Bash and try for clone
  • 1
    This answer duplicates an existing answer which is not useful. When you have enough reputation (15, I believe) you can vote up answers that you find useful. Voting would be the correct response rather than adding this duplicate answer. – AdrianHHH Feb 28 '19 at 18:15