141

Initiating a push or any other action with GitHub from the command line (over https, not ssh) that calls for the username and password not only fails but, when it does, it returns

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/'

I do not have an @github.com address. The password and username are correct.

I know I could switch to SSH and use keys but that doesn't answer why the authentication is failing over https.

ele
  • 6,021
  • 5
  • 25
  • 35
  • Documentation **http://git-scm.com/book/en/Git-on-the-Server-Generating-Your-SSH-Public-Key** git-config - Get and set repository or global options **http://git-scm.com/docs/git-config** – John Smith Jan 03 '14 at 22:50

10 Answers10

177

GitHub's support determined the root of the issue right away: Two-factor authorization.

To use GitHub over the shell with https, create an OAuth token. As the page notes, I did have to remove my username and password credentials from Keychain but with osx-keychain in place, the token is stored as the password and things work exactly as they would over https without two-factor authorization in place.

ele
  • 6,021
  • 5
  • 25
  • 35
25

I do not have an @github.com address

You don't have to: the @ is the separator between the username:password and the domain.
It is not an email address.

A full GitHub https url would be:

# 2014:
https://username:password@github.com/username/reponame.git

# 2021+:
https://username:token@github.com/username/reponame.git

Without the token (which would then be asked on the command line), that would give:

https://username@github.com/username/reponame.git

But again, username@github.com isn't an email address, just the first part of the credentials.

Make sure the case of your username and reponame is correct: it is case-sensitive.


Since Aug. 2021, GitHub no longer accept account passwords when authenticating Git operations on GitHub.com.
Only PAT (Personal Access Token)


Note that you can store and encrypt your credentials in:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You can also leave the password out, typing the password every time. In that case it would be `https://username@github.com/username/reponame.git`. – Mark Hildreth Jan 03 '14 at 23:13
  • Oh, that make sense. Thank you. I knew about the case sensitivity and I am confident that the username and password I'm providing are correct. (They work fine to sign into the website.)a – ele Jan 04 '14 at 00:41
  • 2
    @ele yes, but does your password include special characters (like `! * ' ( ) ; : @ & = + $ , / ? # [ ]`)? They would need to be "percent-encoded" (http://en.wikipedia.org/wiki/Percent-encoding). – VonC Jan 04 '14 at 00:44
  • The password is alphanumeric. I'll see what 'git remote -v' gives when I'm next at the machine but sounds like I might need to contact GitHub support next. – ele Jan 04 '14 at 03:08
  • @ele try the .netrc approach first (don't even encrypt, just for testing). If that doesn't work, the GitHub support is indeed the next step. – VonC Jan 04 '14 at 06:47
21
  • Go to Credential Manager => Windows Manager
  • Delete everything related to tfs
  • Now click on Add a generic credential and provide the following values

    (1) Internet or network adress: git:https://tfs.donamain name (2) username: your username (3) password: your password

    this should fix it

Jemil Oyebisi
  • 633
  • 8
  • 10
  • 1
    wow. I love how it stores the incorrect password if you type it in wrong. Then you have to go manually update it or delete it – Kellen Stuart Nov 30 '18 at 20:01
10

It may happen in Windows if you stored a different credentials before. Go to Credential Manager and delete stored github credentials

Random
  • 4,519
  • 2
  • 38
  • 46
  • 1
    It is also possible to directly edit the password in the Windows Credential Manager to the new password. – Spenhouet Sep 17 '18 at 09:10
10

Same thing happened with me, when i have enabled 2-way authentication for github. Things i did to resolve:

  • Get you personal access token. This you have to check and generate if not available already. Link for this: https://github.com/settings/tokens
  • Go to your local and delete folder and re-clone branch from github.
  • Now try the command you were trying earlier i.e: git pull origin master
  • Enter username and In password paste the token generated and also don't forget to save that token somewhere, so you can re-use if required.

Doing this will solve your issue.

palak
  • 101
  • 1
  • 3
  • Good callout - this was happening to me on mac => [had to regenerate my PAT](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) – SliverNinja - MSFT Nov 14 '19 at 23:00
  • We have to generate the token if we need to permissions not present in previous generated tokens. Also if we have lost our previous token, we can generate this new token and use as a password to authenticate instead of our original git password. – Mohd Belal May 11 '20 at 21:21
9

[Mac only]

If you need to delete your authentication, use

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

on Mac.

See https://help.github.com/articles/updating-credentials-from-the-osx-keychain/

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
6

Just incase this helps anyone else also, I was signed into the mac app, command line working fine, but because I then turned on 2FA, my commands were returning the error. I had to sign out of the app, then I could use my Personal access token in my commands as per ele's answer here.

Hopefully that helps someone!

Community
  • 1
  • 1
Joel Balmer
  • 1,428
  • 2
  • 19
  • 21
2
  • Go to https://github.com/settings/tokens to generate your tokens
  • Generate your token and store it safely
  • Now try to push your code to github
  • Two times you have to give your credentials. First time provide your gmail and password and second time provide your username and the generated token instead of password(at the password field).
  • another thing is , if you go to your github profile then you will find your username at the url

It works for me , hoping it will save your time . Best wishes .

Chayan Roy
  • 21
  • 4
0
  • For mac and windows Go to Github token generator

    • Generate the token and copy it
  • Then in Terminal navigate to folder and perform git push

  • It will ask for username and password, Put personal access token in password

Note - If push does not happen use git push --force

Romil Jain
  • 305
  • 3
  • 5
-2

On Windows, you may be silently blocked by your Antivirus or Windows firewall. Temporarily turn off those services and push/pull from remote origin.

Vortex
  • 789
  • 12
  • 21