GitLab prompts for username & password during git push even though already set up SSH key


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 I got the following error:

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

It shows that SSH was not working because if SSH is working correctly it should not prompt for username and password.

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

$ git remote set-url origin<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