HTTPS和SSH方式拉取github上项目(git clone)

185 阅读1分钟

HTTPS方式

  1. 鼠标右键待保存clone项目的文件夹,选择git Bash Here
  2. 输入git clone 复制的项目地址,回车。

可能出现下列错误:

$ git clone https://github.com/HongguLiu/MesoNet-Pytorch.git
Cloning into 'MesoNet-Pytorch'...
fatal: unable to access 'https://github.com/HongguLiu/MesoNet-Pytorch.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0

解决:

  1. 终端输入下列命令刷新DNS缓存:
ipconfig /flushdns
  1. 获取github的ip地址:
nslookup github.com
  1. 然后通过hosts文件手动映射,Hosts文件在 C:\Windows\System32\drivers\etc\hosts :
# hosts文件末尾添加
# GitHub mappings
140.82.113.4 github.com
140.82.113.4 www.github.com

例如:

# 原本hosts文件
# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# For example:
#      127.0.0.1       localhost
# ...略...
# End of section

# 在hosts文件结尾添加,即End of section之后
# 更新后hosts文件
# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# For example:
#      127.0.0.1       localhost
# ...略...
# End of section
# GitHub mappings
20.205.243.166 github.com
20.205.243.166 www.github.com
  1. 保存hosts文件,重新git clone,成功。

SSH方式

  1. 打开终端,输入ssh-keygen -t rsa -b 4096 -C "your_email@example.com回车生成SSH密钥(可ls ~/.ssh查看是否生成成功)
  2. 将公钥添加到github设置中:github中settings -> SSH and GPG keys -> New SSH key 保存即可(公钥通常保存在用户主文件夹/.ssh/id_rsa.pub,用户主文件夹即打开终端对话框初始文件夹,私钥通常保存在用户主文件夹/.ssh/id_rsa。也可终端输入cat ~/.ssh/id_rsa.pub查看公钥,cat ~/.ssh/id_rsa查看私钥)
  3. 终端输入ssh -T git@github.com测试SSH连接(如果没有建立连接,删除密钥,重复1-2步)。
  4. 若连接成功,则终端输入git clone github复制的SSH地址即可下载项目到本地。