I set up 2 Factor Authentication on GitHub a while back. So, once I was working in the command line and wanted to push to my repo. I entered my username and password but it failed by giving an error like this
USERNAME@MYCOMPUTER:~/MyRepo$ git push
Username for 'https://github.com': GitHubUsername
Password for 'https://[email protected]': GitHubPassword
remote: Invalid username or password.
fatal: Authentication failed for '
https://github.com/GitHubUsername/MyRepo/'
So, I read this post and got the solution that I have to generate a Personal Access Token to push anything. Fine, but I think this is a little tough process because First I can't remember that massive Token and Second Storing an access key in a text file is not a safe thing to do.
So, it'll be very nice if someone can give an easy solution to push to my repo without disabling 2FA. - Thanks!
Instead of using Github over https, you should use github over ssh.
https://help.github.com/en/articles/connecting-to-github-with-ssh
While https setup is easy, setting up ssh connections is a little bit more difficult, and this is the reason why https is used as standard option.
When you want to connect to github, you need to follow the following steps:
Create an ssh key
When you connect over ssh, it works with ssh keys instead of normal passwords, this increases your security, as even a server compromise won't leak your password, and even a attacker compromising your connection cannot change the data you send or receive to/from Github. Ssh keys also have optional passwords, that you need to provide in order to use said key.
Usually, ssh-keys are combined with a program called a ssh-agent, and this program basically "caches" the decrypted key in memory, either forever, or with a timeout, so don't have to fill your password multiple times in a short period.
You can create a key as follows:
ssh-keygen -t ed25519 -C "[email protected]"
Tell Github about your new key
When you create your ssh key, it make 2 files, id_rsa
and id_rsa.pub
, the .pub
file contains the public key.
You can upload this key to Github by going to the settings, pressing "ssh keys", and adding the key there
Update the local reposirory to use ssh keys
Now that you told github about your new fance key, you need to configure your repository on disk to use this key.
You can use the git remote
command to do this:
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
Test your new settings by doing git fetch
, it should ask for your ssh key password, and then fetch any updates branches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With