25

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?

Majid Rajabi
  • 988
  • 4
  • 17
  • 30

8 Answers8

21

I was getting the same error.

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

Previously:

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

OR

ssh://git@github.com:userName/repo.git

New:

git@github.com:userName/repo.git

Saurabh
  • 2,423
  • 2
  • 22
  • 29
Shivam Sharma
  • 1,012
  • 8
  • 28
13

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.

Dhruv Joshi
  • 608
  • 7
  • 8
  • I try to delete the current HTTPS origin and then add a remote repository with SSH url. But I used your solution and it works too. thanks – Majid Rajabi Nov 03 '18 at 14:23
7

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.

ABGR
  • 4,052
  • 2
  • 19
  • 42
  • Actually, I am also working with VPN, `git fetch` not working in terminal but I can connect to the private bitbucket server in the browser. – fuat Dec 07 '20 at 08:32
  • Funny/sad such an easy solution is not coming right away to our heads. This was also the case for me right now. – devaga Mar 16 '21 at 08:40
  • 1
    The same thing! Reconnect to VPN resolved the issue. Thanks – ModX May 17 '21 at 10:13
1

For me it was a simple solution to a "dumb" problem:

After typing $ git push, I was prompted Are you sure you want to continue connecting (yes/no)?, and instead of just hitting enter, actually writing "yes" and then hitting enter solved it!

arilebedey
  • 33
  • 7
1

While all the answers address the possibility of authentication failing with gitlab, the problem could also be DNS on your local system. For example:

$ ssh -Tvvv git@gitlab.com
debug2: resolving "gitlab.com" port 22
ssh: Could not resolve hostname gitlab.com: Name or service not known
$ ping gitlab.com
$ ping: gitlab.com: Name or service not known

This shows an issue with DNS settings on your computer. In RedHat, try this:

systemctl status NetworkManager
dnsmasq[2328]: nameserver 192.168.100.1 refused to do a recursive query

Oh my, that's a problem.

Is your network connection up?

$ ip a s
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 28:cd:c4:80:b8:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.7/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp2s0
       valid_lft 86127sec preferred_lft 86127sec
    inet6 fe80::dd85:ef7b:1632:5975/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

$ ip route
default via 192.168.1.1 dev wlp2s0 proto dhcp metric 600 

$ ip link
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether 28:cd:c4:80:b8:0b brd ff:ff:ff:ff:ff:ff

For me, yes. Try restarting the connection:

$ sudo ip link set dev wlp2s0 down
$ sudo ip link set dev wlp2s0 up

Now try running:

sudo systemctl status NetworkManager

And if the previous error went away, you can now ping gitlab.com.

git push -u origin master
Daniel Viglione
  • 6,804
  • 7
  • 56
  • 89
0

I faced the same issue when working on azure.

ssh: Could not resolve hostname ssh.abc.azure.com: Temporary failure in name 
resolution
fatal: Could not read from remote repository.

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

Reason: This issue is due to the failure in WSL2.

Fix: Disable and enable the WSL.

0

I also found this error because I used backslashes "\" in the URL instead of slashes "/".

-1

Hope this would help someone :) In my case, I lost internet connectivity.

Prashant Abdare
  • 1,834
  • 13
  • 24