【TeamFlow】4.1 Git使用指南

发布于:2025-04-22 ⋅ 阅读:(33) ⋅ 点赞:(0)

以下是 Git 在 Windows 系统上的配置和使用指南,包含详细步骤和注意事项:

安装 Git for Windows

下载与安装
  1. 前往 Git 官网 下载 Windows 版安装包

  2. 双击安装,关键选项建议:

  • 选择 Use Git from Git Bash only(推荐)

  • 勾选 Enable symbolic links

  • 选择 Checkout as-is, commit Unix-style line endings

  • 选择 Use Windows’ default console window(如果习惯CMD)

验证安装
git --version
# 应显示类似:git version 2.xx.x.windows.1
  1. 初始配置(必需)
设置用户信息(全局配置)
git config --global user.name "你的姓名"
git config --global user.email "你的邮箱@example.com"

检查配置

git config --list
# 重点检查:
# user.name=...
# user.email=...
  1. 初始化仓库 (git init)
    在项目目录中初始化

  2. 打开 Git Bash(推荐)或 CMD/PowerShell

  3. 进入项目文件夹:

cd /e/path/to/your/project  # 示例路径
  1. 执行初始化:
git init

成功后显示:Initialized empty Git repository in E:/path/to/your/project/.git/

重要文件说明
  • 生成的 .git 隐藏文件夹包含所有版本控制数据

  • 如需重新初始化,先删除此文件夹

  1. 基础工作流
首次提交示例
# 1. 添加所有文件到暂存区
git add .

# 2. 提交到本地仓库
git commit -m "Initial commit"

# 3. 查看状态
git status
常用操作速查
操作 命令
查看变更 git status
添加文件 git add 文件名
提交 git commit -m “描述”
查看历史 git log --oneline
  1. Windows 特别配置
解决换行符问题(重要!)
git config --global core.autocrlf true
  • 作用:在检出时转换为CRLF,提交时转换为LF
显示中文文件名
git config --global core.quotepath false
配置默认编辑器(如VS Code)
git config --global core.editor "code --wait"
  1. 连接远程仓库(如GitHub)
生成SSH密钥
ssh-keygen -t ed25519 -C "your_email@example.com"
  • 密钥默认保存在:C:\Users\你的用户名.ssh\
添加公钥到GitHub
  1. 复制 id_ed25519.pub 文件内容

  2. 在 GitHub Settings → SSH and GPG keys 中添加

关联远程仓库
git remote add origin git@github.com:用户名/仓库名.git
git push -u origin main
  1. 图形化工具推荐(可选)
    Git GUI(Git自带):
git gui
  1. VS Code Git集成:
  • 内置可视化差异比较和提交界面
  1. GitKraken:
  • 更直观的图形化操作
  1. 常见问题解决
问题:warning: LF will be replaced by CRLF
  • 原因:Windows换行符转换

  • 解决:这是正常提示,已通过 core.autocrlf 配置处理

问题:权限被拒绝(Permission denied)
# 修复.ssh目录权限
icacls "%USERPROFILE%\.ssh" /reset
icacls "%USERPROFILE%\.ssh" /inheritance:r
icacls "%USERPROFILE%\.ssh" /grant:r "%USERNAME%":(F)
问题:中文乱码
# 设置控制台编码为UTF-8
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8
  1. 最佳实践建议

  2. 频繁提交:小步提交,保持提交记录的原子性

  3. 规范消息:

git commit -m "feat: 添加用户登录功能"

推荐使用 Conventional Commits 规范

  1. 分支策略:
  • main 分支保持稳定

  • 新功能在 feature/xxx 分支开发

通过以上步骤,你已在 Windows 系统上完成 Git 的配置和基础使用。如需更复杂的工作流(如分支管理、标签发布),可进一步学习 git branch 和 git tag 命令。


网站公告

今日签到

点亮在社区的每一天
去签到