Git Clone from GitHub over https with two-factor authentication

127,384

Solution 1

Find out how to fix this here:

https://github.com/blog/1614-two-factor-authentication#how-does-it-work-for-command-line-git

How does it work for command-line Git?

If you are using SSH for Git authentication, rest easy: you don't need to do anything. If you are using HTTPS Git, instead of entering your password, enter a personal access token. These can be created by going to your personal access tokens page.

Solution 2

As per @Nitsew's answer, Create your personal access token and use your token as your username and enter with blank password.

Later you won't need any credentials to access all your private repo(s).

Updates 2021: (It worked for my M1 Mac)

Create new Personal Access Tokens (Tick the appropriate boxes to grant permissions). Use GitHub username and password as your Personal Access Tokens

enter image description here

Solution 3

To everyone struggling, what worked for me was creating personal access token and then using it as a username AND password (in the prompt that opened).

Solution 4

It generally comes to mind that you have set up two-factor authentication, after a few password trials and maybe a password reset. So, how can we git clone a private repository using two-factor authentication? It is simple, using access tokens.

How to Authenticate Git using Access Tokens

  1. Go to https://github.com/settings/tokens
  2. Click Generate New Token button on top right.
  3. Give your token a descriptive name.
  4. Set all required permissions for the token.
  5. Click Generate token button at the bottom.
  6. Copy the generated token to a safe place.
  7. Use this token instead of password when you use git clone.

Wow, it works!

Solution 5

1st: Get personal access token. https://github.com/settings/tokens
2nd: Put account & the token. Example is here:

$ git push
Username for 'https://github.com':            # Put your GitHub account name
Password for 'https://{USERNAME}@github.com': # Put your Personal access token

Link on how to create a personal access token: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

Share:
127,384

Related videos on Youtube

peter-b
Author by

peter-b

Software Engineer

Updated on December 24, 2021

Comments

  • peter-b
    peter-b over 2 years

    I recently began using two-factor authentication on GitHub, and I am now unable to use git over https on private repos in the usual way:

    peter@computer:~$ git clone https://github.com/[...]/MyPrivateRepo
    Cloning into 'MyPrivateRepo'...
    Username for 'https://github.com': [...]
    Password for 'https://[...]@github.com': 
    remote: Invalid username or password.
    fatal: Authentication failed for 'https://github.com/[...]/MyPrivateRepo/'
    

    If I disable two-factor authentication I can use it as before:

    peter@computer:~$ git clone https://github.com/[...]/MyPrivateRepo
    Cloning into 'MyPrivateRepo'...
    Username for 'https://github.com': [...]
    Password for 'https://[...]@github.com': 
    remote: Counting objects: 147, done.
    remote: Total 147 (delta 0), reused 0 (delta 0), pack-reused 147
    Receiving objects: 100% (147/147), 22.70 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (87/87), done.
    Checking connectivity... done.
    

    I know I can use SSH and everything works, but is there a way I can keep two-factor authentication while still being able to use GitHub over HTTPS, for example by sending an auth token with my request?

  • Martin Woolstenhulme
    Martin Woolstenhulme over 8 years
    You can add the token to the .netrc file as I found in the following gist, to make authentication automatic too.
  • starkshang
    starkshang over 8 years
    But I'm using ssh,when pull from github,it also hints me to input password and username,why this?thanks for your reply!
  • ojus kulkarni
    ojus kulkarni over 6 years
    still its not working for me after entering the access token in mac after sudo git clone.... system is asking for username and password in password field I am entering token but its not working for me. getting same error
  • Kotie Smit
    Kotie Smit almost 6 years
    Use your token as your username and give a blank password.
  • gmargari
    gmargari over 4 years
    Mind the trailing extra space when manually copying the token from github site (i.e. if not using the github copy button). Took me a while to understand why this didn't work for me.
  • Ian Turton
    Ian Turton about 4 years
    only helps if you use windows
  • bdoubleu
    bdoubleu about 4 years
    Where would you recommend storing the access token on a remote server?