git push出现“ssh: connect to host github.com port 22: Connection timed out ”

A few days ago git push, the following problem occurred when using uploading: After
insert image description here
Baidu search, there are two solutions in total: 1. Change the ssh method to https method, that is, change the method when connecting to the remote warehouse. The configuration of this method is in ~/.git/configchanges in the file. 2. Change the port of ssh, that is, ~/.ssh/configchange it in the file.

Of these two methods, only the second one can succeed on my computer, and the first one still causes errors. Here I simply record:
The first method: change the ssh method to the https method.

  • Found ~/.git/config, you can use the command: git config --local -e, the following content appears:
[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = [email protected]:Norah2/norah2.github.io.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	name = nora
	email = [email protected]
  • url = [email protected]:Norah2/norah2.github.io.gitChanging the url = https://github.com/Norah2/norah2.github.io.git, is actually changing the original git@to https://, and then :changing the colon to a slash /. If you really don't want to change it, you can log in to github, enter the remote warehouse, click on the code and select https, and directly copy the above link.

After the changes were made, save and exit, the problem still occurred when I tried to upload again, so I tried the second method. (The failure of the first method may be because the configuration was not refreshed, and the second method was used later, so I did not continue to test)

The second: change the port of ssh.

  • Create a new ~/.git/configfile: vim ~/.git/config.
  • Add content inside:
Host github.com
User [email protected]
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

Fill in your email after User on the second line, save and exit after filling.

  • You need to re-use the command git config user.name xxxand git config user.email xxxset it, in order to refresh the configuration.

insert image description here

If you don't have this step, git pushyou will still have problems:
insert image description here

  • You can use the command ssh -T [email protected]to check whether ssh is normal:
    insert image description here
    after the above operations are completed, git pushit will return to normal after uploading again:
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_42992706/article/details/128753025