# Git Windows 安装与配置 > 📚 Git 教程系列 - 第 2 篇 > 📅 最后更新:2026-03-15 > 💻 适用系统:Windows 10/11 --- ## 📥 第一步:下载 Git ### 方法 1:官网下载(推荐) 1. 打开浏览器访问:https://git-scm.com/download/win 2. 点击 **Download for Windows** 按钮 3. 下载最新版本(约 50-60MB) ### 方法 2:使用包管理器 如果你已安装 **Chocolatey** 或 **Scoop**: ```powershell # Chocolatey choco install git # Scoop scoop install git ``` --- ## 🔧 第二步:安装 Git ### 安装步骤详解 1. **运行安装程序** - 双击下载的 `.exe` 文件 - 点击 **Next** 2. **选择安装位置** - 默认:`C:\Program Files\Git` - 可自定义,建议保持默认 3. **选择组件** ``` ✅ Git Bash Here(右键菜单) ✅ Git GUI Here(右键菜单) ✅ 关联 .git* 文件 ✅ 关联 .sh 文件 ``` 建议全选 4. **选择默认编辑器** - 推荐:**Visual Studio Code** - 或选择:Vim / Notepad++ / Nano 5. **选择命令行工具** - 选择:**Git from the command line and also from 3rd-party software** - 这样可以在 CMD/PowerShell 中使用 git 命令 6. **选择 HTTPS 传输后端** - 选择:**Use the OpenSSL library** 7. **配置行尾转换** - 选择:**Checkout as-is, commit as-is** - 避免自动转换行尾 8. **选择终端模拟器** - 选择:**Use MinTTY**(默认) 9. **配置额外选项** - ✅ 启用文件系统缓存 - ✅ 启用 symbolic links 10. **启用实验性功能** - 可选:启用伪控制台支持 11. **完成安装** - 点击 **Finish** --- ## ✅ 第三步:验证安装 ### 打开 Git Bash 1. 在桌面或任意文件夹右键 2. 选择 **Git Bash Here** 3. 输入以下命令: ```bash # 检查 Git 版本 git --version # 输出示例 git version 2.44.0.windows.1 ``` 如果显示版本号,说明安装成功!🎉 --- ## ⚙️ 第四步:配置 Git ### 全局配置(必须) ```bash # 配置用户名(用你的真实姓名或昵称) git config --global user.name "Deshill" # 配置邮箱(用你的真实邮箱) git config --global user.email "your-email@example.com" # 配置默认分支名(Git 2.28+) git config --global init.defaultBranch main # 配置自动换行(Windows 重要) git config --global core.autocrlf true # 配置编辑器(推荐 VS Code) git config --global core.editor "code --wait" ``` ### 查看配置 ```bash # 查看所有配置 git config --list # 查看某个配置 git config user.name git config user.email ``` ### 配置文件位置 全局配置文件位于: ``` C:\Users\你的用户名\.gitconfig ``` 可以用记事本打开查看: ```bash notepad ~/.gitconfig ``` --- ## 🎨 第五步:美化 Git Bash(可选) ### 安装 Oh My Posh ```powershell # 在 PowerShell 中运行 winget install JanDeDobbeleer.OhMyPosh -s winget ``` ### 配置主题 1. 打开 PowerShell 配置文件: ```powershell notepad $PROFILE ``` 2. 添加以下内容: ```powershell oh-my-posh init pwsh --theme jandedobbeleer | Invoke-Expression ``` 3. 重启 PowerShell --- ## 🚀 第六步:常用 Git Bash 命令 ### 基本导航 ```bash # 查看当前目录 pwd # 列出文件 ls ls -la # 显示隐藏文件 # 切换目录 cd /c/Users/你的用户名/Desktop cd .. # 返回上级 cd ~ # 返回家目录 # 创建目录 mkdir my-project cd my-project ``` ### 文件操作 ```bash # 创建文件 touch README.md # 编辑文件(使用 VS Code) code README.md # 复制文件 cp file1.txt file2.txt # 移动/重命名文件 mv old.txt new.txt # 删除文件 rm file.txt rm -rf folder # 删除文件夹(谨慎!) ``` --- ## 🔑 第七步:配置 SSH 密钥(用于 GitHub/Gitee) ### 生成 SSH 密钥 ```bash # 生成新密钥(用你的邮箱) ssh-keygen -t ed25519 -C "your-email@example.com" # 按提示操作: # 1. 选择保存位置(直接回车用默认) # 2. 设置密码(可选,直接回车跳过) ``` ### 查看公钥 ```bash # 显示公钥内容 cat ~/.ssh/id_ed25519.pub ``` 复制输出的内容(以 `ssh-ed25519` 开头) ### 添加到 GitHub/Gitee **GitHub:** 1. 访问:https://github.com/settings/keys 2. 点击 **New SSH key** 3. 粘贴公钥内容 4. 点击 **Add SSH key** **Gitee:** 1. 访问:https://gitee.com/profile/sshkeys 2. 点击 **添加 SSH 密钥** 3. 粘贴公钥内容 4. 点击 **确定** ### 测试连接 ```bash # 测试 GitHub ssh -T git@github.com # 测试 Gitee ssh -T git@gitee.com # 成功输出 Hi Deshill! You've successfully authenticated... ``` --- ## 📁 第八步:第一个 Git 项目 ### 创建项目 ```bash # 在桌面创建项目文件夹 cd /c/Users/你的用户名/Desktop mkdir my-first-git-project cd my-first-git-project # 初始化为 Git 仓库 git init # 创建 README 文件 echo "# My First Git Project" > README.md # 查看状态 git status ``` ### 提交代码 ```bash # 添加到暂存区 git add README.md # 或添加所有文件 git add . # 提交 git commit -m "初始化项目" # 查看日志 git log ``` ### 连接远程仓库 ```bash # 添加远程仓库(替换为你的仓库地址) git remote add origin git@github.com:Deshill/my-first-git-project.git # 推送 git push -u origin main ``` --- ## 🛠️ 常见问题解决 ### 问题 1:中文乱码 ```bash # 配置 Git 显示中文 git config --global core.quotepath false git config --global gui.encoding utf-8 git config --global i18n.commitencoding utf-8 git config --global i18n.logoutputencoding utf-8 ``` ### 问题 2:权限错误 ```bash # 以管理员身份运行 Git Bash # 右键 Git Bash → 以管理员身份运行 ``` ### 问题 3:SSL 证书验证失败 ```bash # 临时禁用 SSL 验证(不推荐) git config --global http.sslVerify false # 或更新 CA 证书 git config --global http.sslCAInfo "C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt" ``` ### 问题 4:行尾转换问题 ```bash # Windows 推荐配置 git config --global core.autocrlf true # 如果已有项目,重置行尾 git rm --cached -r . git add . git commit -m "fix: 统一行尾格式" ``` --- ## 📋 配置检查清单 安装完成后,检查以下项目: - [ ] Git 已安装(`git --version`) - [ ] 用户名已配置(`git config user.name`) - [ ] 邮箱已配置(`git config user.email`) - [ ] SSH 密钥已生成(`ls ~/.ssh/`) - [ ] SSH 密钥已添加到 GitHub/Gitee - [ ] SSH 连接测试成功(`ssh -T git@github.com`) - [ ] 中文显示正常 - [ ] 能正常创建和提交项目 --- ## 🎯 小结 | 步骤 | 内容 | 命令 | |------|------|------| | 1 | 下载 Git | git-scm.com | | 2 | 安装 Git | 双击 .exe | | 3 | 验证安装 | `git --version` | | 4 | 配置用户 | `git config --global user.name/email` | | 5 | 配置 SSH | `ssh-keygen -t ed25519` | | 6 | 创建项目 | `git init` | | 7 | 提交代码 | `git add` + `git commit` | --- > 💡 **下一步**:继续学习 [03-Linux 安装与配置](./03-Linux安装与配置.md)