Table of Content

After reinstalled git, git push always asked Username and password even ssh key already deployed to github account.

$ git push -u origin master
Username for ‘https://github.com‘:

if you type in user and password, for sure it is working but wonder why ssh key not working.

ssh git test looks fine,

ssh -T git@github.com 
Hi /! You've successfully authenticated, but GitHub does not provide shell access.

Quick go through github help ssh related page, found the issue is on misunderstanding of type of github connectivity.

There are two type of github connectivity available, either works:

git@github.com:/.git
https://github.com//.git

which is current url setting, can check like this:

$ git remote --verbose
origin  https://github.com//.git (fetch)
origin  https://github.com//.git (push)

this means current url setting is for https, that is why asking username/password.

Need to change to git url setting for ssh ,

$ git remote set-url origin git@github.com:/.git

verify again,

$ git remote --verbose
origin  git@github.com:/.git (fetch)
origin  git@github.com:/.git (push)

Before do git push, let’s quick go through ssh key setting:

1) generate new ssh key,

$ ssh-keygen -t rsa -C "email_address"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users//.ssh/id_rsa): /home//.ssh/id_rsa
/home//.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home//.ssh/id_rsa.
Your public key has been saved in /home//.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:P7SXebaZuIeIDdLencXoKThnljNIQejBY5pW8MiWmDM 
The key's randomart image is:
+---[RSA 2048]----+
|    .o .         |
|   + +B .        |
|  E =*.+         |
|   ++ . .        |
|   .   .S..  o   |
|      . +o ..oo  |
|       + B+=+=o  |
|        * %o*+.+ |
|         = +oo+  |
+----[SHA256]-----+

2) deploy public key to github account, this is per repo, you can add as much as you can

3) add private key to ssh agent

$ eval $(ssh-agent -s)
Agent pid 13480

$ ssh-add /home//.ssh/id_rsa
Identity added: /home//.ssh/id_rsa (/home//.ssh/id_rsa)

4) test ssh to github

ssh -T git@github.com 
Hi /! You've successfully authenticated, but GitHub does not provide shell access.

5) then try git push, it won’t ask user/password anymore,
this is first time to push to remote master,

$ git push -u origin master                                                                    
The authenticity of host 'github.com (192.30.253.113)' can't be established.                   
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.                     
Are you sure you want to continue connecting (yes/no)? yes                                     
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.       
Counting objects: 3, done.                                                                     
Delta compression using up to 4 threads.                                                       
Compressing objects: 100% (3/3), done.                                                         
Writing objects: 100% (3/3), 397 bytes | 0 bytes/s, done.                                      
Total 3 (delta 1), reused 0 (delta 0)                                                          
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.                          
To github.com:/.git                                                  
   86e9e32..d21f30e  master -> master                                                          
Branch master set up to track remote branch master from origin. 

then you can push in short command later,

$ git push origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.11 KiB | 0 bytes/s, done.
Total 9 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 1 local objects.
To github.com:/.git
   d21f30e..4445dc1  master -> master