ssh: Could not resolve hostname git: Name or service not known fatal: Could not read from remote repository

After I switched from HTTPS to SSH for my repo then I received this error when pushing to origin master:

ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I also add my ssh in the gitlab. What should I do?


Solution 1:

I was getting the same error.

Change the [remote "origin"] url value in .gitconfig or .config/git/config file

Previously:

https://[email protected]:userName/repo.git

OR

ssh://[email protected]:userName/repo.git

New:

[email protected]:userName/repo.git

Solution 2:

HTTPS URLs provides an easy access to your repository, either private or public, even when you are working behind a firewall or proxy. And it is the recommended way.

On the other hand, SSH URLs use the secure ssh protocol in order to access a repository.

Coming back to your question, the error is likely because of improper configuration. The git remote add command is used to add a new remote to your repository, that you have already tried. However, switching from HTTPS to SSH url, means that your remote origin is already set to an http url and that you want to change.

Therefore, first check what url your current remote origin is referring to:

$ git remote -v

If it is referring to the HTTPS url, then you have to use

$ git remote set-url origin mySSH_url

command to change it to the SSH url.

Now, try git remote -v, it would display SSH urls configured for origin.

Do make sure that while working with SSH urls, you have generated and added the ssh key to the ssh-agent as well on GitLab/GitHub account.

Here is a very good article on how to change a remote's url.

Also, you can learn more about which remote url to use here.

Solution 3:

Well, in my case my local system was not connected to the VPN and hence was getting this error. So this could be one of the reasons apart from the above answers.