> 技术文档 > git 小白入门教学

git 小白入门教学


💻 Windows 从下载 Git 到使用全流程教程(含国内网络问题解决方案)

很多小伙伴在刚入门 Git 的时候都会遇到一个尴尬的问题:GitHub 打不开 / clone 超时 / push 失败。别急,本教程会手把手带你解决国内网络问题,并完成从 Git 安装到基本使用的全流程。


🚀 一、国内无法访问 GitHub 的解决办法

  1. 常见问题

    • GitHub 打不开 ❌
    • git clone 一直卡住 🌀
    • git push 超时报错 ⏱️
  2. 解决思路

    1. 🛰️ 更换 DNS

      • 打开 👉 控制面板 → 网络和共享中心 → 适配器 → 属性 → 选择 IPv4 → 修改 DNS。

      • 推荐:

        • 114.114.114.114
        • 223.5.5.5
    2. 📌 修改 hosts(短期方案)

      • 文件路径:

        C:\\Windows\\System32\\drivers\\etc\\hosts
      • 添加 GitHub 对应 IP(可去 ipaddress.com 查询)。

    3. 使用镜像站

      • https://hub.fastgit.org

      • https://gitclone.com

      • 示例:

        git clone https://hub.fastgit.org/username/repo.git

🛠️ 二、下载并安装 Git

  1. 下载

    • 官网地址:https://git-scm.com/download/win
    • 点击后自动下载 .exe 安装包。
  2. 安装

    • 一路 Next 就行 👉 推荐保持默认选项。
    • 环境变量选 Git from the command line and also from 3rd-party software
  3. 验证是否成功
    打开 CMD(Win + R → 输入 cmd):

    git --version

    显示版本号 ✅ 安装成功。


⚙️ 三、Git 初始配置

  1. 设置用户名和邮箱:

    git config --global user.name \"你的名字\"git config --global user.email \"你的邮箱@example.com\"
  2. 查看是否配置成功:

    git config --list

🔑 四、配置 SSH Key(推荐方式)

  1. 生成 SSH Key:

    ssh-keygen -t rsa -C \"你的邮箱@example.com\"

    一路回车,生成位置:

    C:\\Users\\你的用户名\\.ssh\\id_rsa.pub
  2. 将公钥添加到 GitHub

    • GitHub → Settings → SSH and GPG keys → New SSH key
    • 粘贴 id_rsa.pub 的内容。
  3. 测试连接:

    ssh -T git@github.com

    显示 Hi username! 就成功了 🎉


📂 五、Git 基础使用流程

  1. 初始化本地仓库

    git init
  2. 克隆远程仓库

    git clone git@github.com:username/repo.git
  3. 查看状态

    git status
  4. 添加文件

    git add 文件名git add . # 添加所有文件
  5. 提交修改

    git commit -m \"本次修改说明\"
  6. 推送到远程仓库

    git push origin main
  7. 拉取最新代码

    git pull origin main

❗ 六、常见问题解决

  1. push/pull 失败(SSL/TLS 错误)

    • 使用 SSH 地址替代 HTTPS 地址。
  2. 提交信息中文乱码

    • 执行以下命令:

      git config --global core.quotepath falsegit config --global i18n.commitencoding utf-8git config --global i18n.logoutputencoding utf-8

✅ 总结

到这里,你已经完成了:

  • Git 安装 ✔️
  • 国内访问 GitHub 问题解决 ✔️
  • Git 基础操作(init → add → commit → push)✔️

Recommendation: 使用 SSH 链接 GitHub,省去每次输入账号密码的麻烦。
Next step: 去 GitHub 新建一个仓库,试着把本地代码推上去,体验完整的 Git 工作流。 🚀