两种连接方式对比

模式 URL 格式 身份验证方式 优势对比
🌐 HTTPS https://github.com/user/repo.git 访问令牌 (PAT) 或 Git 凭据 网络兼容性好,设置简单
💻 SSH git@github.com:user/repo.git SSH 密钥对 持久身份认证,无需重复输入凭据

🏆 结论: SSH 更“稳定”

SSH 在技术层面上的稳定性与 HTTPS 相差无几,但在用户体验层面,SSH 的“稳定性”体现在:

  1. 无中断性 (Uninterrupted Workflow): HTTPS 模式最大的不稳定因素是 PAT 的周期性过期。SSH 密钥一旦配置,可提供永久稳定的认证,确保您的 push/pull 操作永远不会因为“凭证过期”而中断。
  2. 抗网络限制性 (Firewall Bypass): 通过在本地配置 .ssh/config 文件,将 GitHub SSH 端口重定向到 443 端口,SSH 模式可以像 HTTPS 一样穿越企业防火墙,从而在任何网络环境中都能保持连接的稳定

最佳实践推荐: 采用 SSH 模式,并将其端口配置为 443。这将结合 SSH 的持久认证优势和 HTTPS 的网络兼容性优势,提供最稳定、最流畅的开发体验。

系统生成SSH密钥

  • 这里win11系统为例, 基本上大同小异, 有的系统没有OpenSSL命令, 就自行安装一个
  1. 生成密钥命令
1
ssh-keygen -t rsa -C "你的邮箱"
  1. 复制密钥

进入到C:/用户/<用户名>/.ssh/

使用编辑器打开id_rsa.pub, 复制公钥内容

image-20251128091446191

管理GitHub密钥

  • 这里推荐一个油猴插件, 叫GitHub 中文化插件, 非常好用, 可以直接翻译GitHub的网页端
  1. 登录github网页端

  2. 点击头像-> setting -> SSH and GPG Keys -> New SSH key -> 起名+粘贴内容

image-20251128091124420

启动插件翻译后是这样

image-20251128091644072

新增一个密钥, 点击添加, GitHub就配置完了

image-20251128091803884

配置vscode

先进入到github仓库, 复制仓库的ssh地址

image-20251128092935632

然后打开vscode的源代码管理–>可隆仓库–> 输入SSH克隆地址, 不要选择从GitHub克隆

image-20251128094046794

如果没有报错, 可隆会会提示打开仓库, 选择打开, 就可以编写提交代码了, 也可以使用命令验证一下连通性和克隆方式

image-20251128094625463

GitHub常用命令

  1. 配置查看
1
2
3
git config -l
git config --system -l
git config --global -l
  1. 常用配置
1
2
3
git config --global user.name xxxx
git config --global user.email xxxx@gmail.com
git config --global credential.helper store
  1. 连接测试
1
2
3
4
5
# 连通性测试
ssh -T git@github.com

# 查看远程仓库
git ls-remote https://github.com/xxx/xxx.git
  1. 克隆方式
1
2
3
4
5
6
7
8
9
10
11
git remote -v
# 1. SSH 模式的输出:
origin git@github.com:user/my-project.git (fetch)
origin git@github.com:user/my-project.git (push)

# 2. HTTPS 模式的输出:
origin https://github.com/user/my-project.git (fetch)
origin https://github.com/user/my-project.git (push)

# 模式切换(http方式切换到SSH方式)
git remote set-url origin git@github.com:user/my-project.git
  1. 代理设置
1
2
3
4
5
6
7
8
9
10
11
# 查看代理
git config --global --get http.proxy
git config --global --get https.proxy

# 配置代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy