GitLab prompts for gitlab.com username & password during git push even though already set up SSH key id_rsa.pub

Problem: GitLab SSH not working

Even though I have already set up SSH key according to GitLab official SSH guide (including adding the public key to Profile Settings > SSH Keys ), when I tried to push the repo to gitlab.com I got the following error:

$ git push origin master
Username for 'https://gitlab.com': 
Password for 'https://gitlab.com': 
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'https://gitlab.com/<username>/<project_name>.git

It shows that SSH was not working because if SSH is working correctly it should not prompt for https://gitlab.com username and password.

Solution: set the remote origin URL using SSH instead of HTTPS

$ git remote set-url origin git@gitlab.com:<username>/<project_name>.git

Found this solution from stack overflow:

Question: Git push requires username and password
Answer: A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this:

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