If you are using two-factor authentication on your Github account, chances are that you will be puzzled when you try to git clone your private repository from GitHub. Because it won’t ask you two-factor authentication input, instead it will give you an error message like this:

C:\Users\you\projects>git clone https://github.com/you/private-repo
Cloning into 'private-repo'...
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/you/private-repo/'

C:\Users\you\projects>

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.
  8. Voila, it works!

So, this is how to git clone a private repo with 2fa (two-factor authentication). I hope you found this tip useful. Cheers!