This time I will be explaining how to use Two-Factor Authentication from GitHub on git on a command prompt or any terminal emulator on Windows.
You probably have tried already to do something like:
> git clone https://github.com/flanaras/my-private-repository.git
entered your username, password and never asked for your 6 digit authentication code which led to authentication error similar to this one:
> fatal: Authentication failed for 'https://github.com/flanaras/my-private-repository.git/'
The solution to this is easy, use SSH! To use SSH you need to create a private/public key pair. One way to do that is to download PuTTYgen, run it and select the number of bits that you desire for your key, in this case 4096.
press generate and start moving your mouse around. Then you will have something like this.
Enter the passphrase you desire, this will be used when accessing your private key so do not forget it. As pointed out by @Sakrifor, you need to create the private key by the option Conversions -> Export OpenSSΗ key. Create a folder named .ssh under your user’s folder and save it as “id_rsa”, for instance:
Copy your public key, go to GitHub settings -> SSH and GPG keys, click on “New SSH Key”, give it a tittle to know where you use this key and paste the public key. After that, go to GitHub and get a SSH link to your repository by “Clone with SSH”. Continue on a command prompt and …
> git clone email@example.com:flanaras/my-private-repository.git Cloning into 'my-private-repository'... The authenticity of host 'github.com (188.8.131.52)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': yes Warning: Permanently added 'github.com,184.108.40.206' (RSA) to the list of known hosts. Enter passphrase for key '/c/Users/flanaras/.ssh/id_rsa': remote: Counting objects: 60, done. Receiviemote: Total 60 (delta 0), reused 0 (delta 0), pack-reused 60ng objects: Receiving objects: 100% (60/60), 22.97 KiB | 0 bytes/s, done. Resolving deltas: 100% (25/25), done. Checking connectivity... done.
It works, well done! Now you will be asked to enter your passphrase and not your GitHub credentials.
Note that we previously checked the validity of the RSA fingerprint, Github’s SSH keys.
See you around