0.环境准备

步骤命令 / 操作说明
安装 Githttps://git-scm.com/,Windows 选 “Git Bash Here”以后右键即开终端
配置身份git config --global user.name "你的名字"
git config --global user.email "邮箱"
提交历史里显示
(可选)PATGitHub → Settings → Developer settings → Personal access tokens (classic) → 生成并保存HTTPS 推送当“密码”用
(可选)SSHssh-keygen -t ed25519 -C "邮箱" → 把 id_ed25519.pub 粘到 GitHub → SSH keys避免每次输 PAT

1.在 GitHub 建空仓库

  1. ➕ → New repository

  2. 填仓库名,选 Public / Private

  3. 不要勾选 README / .gitignore / License(保持空)

  4. Create 后记下远程地址:

    1. https://github.com/<user>/<repo>.gitgit@github.com:<user>/<repo>.git

2. 本地把工程变 Git 仓库

cd /path/to/your/project
git init                    # 初始化
curl -L https://gitignore.io/api/python,venv > .gitignore   # 快速忽略
git add .
git commit -m "Initial commit"

3. 关联远程 & 首次推送

git remote add origin https://github.com/<user>/<repo>.git   # HTTPS
# 或 git remote add origin git@github.com:<user>/<repo>.git  # SSH
 
git branch -M main          # 确保分支同名
git push -u origin main     # -u 建立跟踪
 

👉 遇到公司网络拦截:Could not resolve host: github.com

3.1现象排查

nslookup github.com         # 解析失败
curl -I https://github.com  # 解析失败 / 超时
 
  • 说明 CLI 没走代理

3.2 解决方案一(最快)——给 Git 配本地 HTTP 代理

  1. Clash / Surge / V2RayN 里确认 HTTP 代理端口(默认为 7890)。
  2. 设置:
git config --global http.proxy  http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
  1. 测试:
git ls-remote https://github.com/github/gitignore.git
# 能显示 commit 列表即成功
  1. 重新执行 git push -u origin main —— 成功上传。

想关闭时:
git config --global --unset http.proxy
git config --global --unset https.proxy

3.3 解决方案二——SSH + 443 端口(更稳)

# ~/.ssh/config
Host github.com
  Hostname ssh.github.com
  Port 443
  User git
git remote set-url origin git@github.com:<user>/<repo>.git
ssh -T git@github.com   # 出现 "Hi <user>" 表示 OK
git push -u origin main
 

4.日常三件套

git add .                         # 或指定文件
git commit -m "描述改动"
git push                          # 已有 -u,无需写分支名