Recently I have needed to use my GitHub username and password whenever I do a push. I can't remember if it was due to the hardware swapout I recently did with my Mac or some other reason.
╰ ➤ git push Username for 'https://github.com': clijockey Password for 'https://email@example.com': Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 349 bytes | 0 bytes/s, done. Total 3 (delta 2), reused 0 (delta 0) To https://github.com/clijockey/shipped-python 5cf4d29..79635e7 master -> master
It's been a bit of a pain so I decided to resolve the issue so that my GitHub account makes use of SSH keys simimlar to how I log onto servers (previous post on that).
Generate SSH Key
If you have already got yourself some SSH Keys you don't need to do this again you could always reuse what is already setup. However if never been done before first of all create a new SSH key and follow the onscreen instructions;
ssh-keygen -t rsa
Now that the keys are generated you will want to copy the public key to your clipboard so that we can add them to your GitHub account. This can either be using pbcopy to put straight into your clipboard or the traditional way for using cat and cut & paste functions.
pbcopy < ~/.ssh/id_rsa.pub
Add to GitHub account
Now that we have the public key we need to log onto out GitHub account and go to settings (top righthand side, click on profile picture) and navigate to the SSH keys section;
Now if you click 'Add SSH Key' you can populate your public key and associate a name with it;
Now that you have added your key to your GitHub account we should now test everything is working;
╰ ➤ ssh -T firstname.lastname@example.org The authenticity of host 'github.com (188.8.131.52)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspDomTxdCARLvlKw6E5SY8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,184.108.40.206' (RSA) to the list of known hosts. Hi clijockey! You've successfully authenticated, but GitHub does not provide shell access.
If you get the successful message you should be good to now do a test push back to you repo and see if you are challenged for a username/password again or if it works using your keys.
If you are still prompted it could be that the repo was originally setup using HTTPS method rather than SSH so you will need to alter your local git configuration. Its a lot easier than it sounds, first of all navigate to the repo in question and copy the SSH destination;
Now from the local machine issue the following to change from using HTTPS to SSH;
git remote set-url origin email@example.com:clijockey/shipped-python.git
Now when I do a
git push I dont need to enter a username/password and my SSH keys are used;
╰ ➤ git push Everything up-to-date