Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push to GitHub Repository without Personal Access Token when 2FA is enabled

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!

like image 899
TikolaNesla Avatar asked Oct 15 '22 16:10

TikolaNesla


1 Answers

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:

  1. 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:

    1. Run ssh-keygen -t ed25519 -C "[email protected]"
    2. Follow the standard options, and set a password.
    3. Optionally configure an ssh-agent
  2. 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

  3. 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.

like image 76
Ferrybig Avatar answered Oct 20 '22 06:10

Ferrybig