Git 工作流完全指南

Git 是目前最流行的版本控制系统。本文将介绍从基础命令到高级工作流的完整 Git 使用指南,帮助你更高效地进行版本管理和团队协作。

基础配置

初始设置

# 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 配置默认编辑器
git config --global core.editor "vim"

# 配置别名(提高效率)
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit

# 查看配置
git config --list

忽略文件配置

# .gitignore 示例
# 依赖目录
node_modules/
vendor/

# 编译输出
dist/
build/
*.exe
*.dll

# IDE 配置
.idea/
.vscode/
*.swp
*.swo

# 日志文件
*.log
logs/

# 环境变量
.env
.env.local
.env.production

# 操作系统文件
.DS_Store
Thumbs.db

基础工作流

日常开发流程

# 1. 获取最新代码
git pull origin main

# 2. 创建功能分支
git checkout -b feature/new-feature

# 3. 开发并提交
git add .
git commit -m "feat: add new feature"

# 4. 推送到远程
git push origin feature/new-feature

# 5. 创建 Pull Request 并合并
# ...

# 6. 清理分支
git checkout main
git pull origin main
git branch -d feature/new-feature

提交规范

使用 Conventional Commits 规范:

# 格式: <type>(<scope>): <subject>

git commit -m "feat(auth): add login functionality"
git commit -m "fix(api): resolve null pointer exception"
git commit -m "docs(readme): update installation guide"
git commit -m "style(css): format indentation"
git commit -m "refactor(utils): simplify date formatting"
git commit -m "test(unit): add user service tests"
git commit -m "chore(deps): update dependencies"

类型说明:

  • feat: 新功能
  • fix: 修复
  • docs: 文档
  • style: 格式(不影响代码运行)
  • refactor: 重构
  • test: 测试
  • chore: 构建过程或辅助工具的变动

分支策略

Git Flow

# 主要分支
main        # 生产分支
develop     # 开发分支

# 临时分支
feature/*   # 功能分支
release/*   # 发布分支
hotfix/*    # 热修复分支

# 创建功能分支
git checkout develop
git checkout -b feature/user-authentication

# 完成功能
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication

# 创建发布分支
git checkout -b release/v1.0.0 develop

# 发布
git checkout main
git merge --no-ff release/v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git checkout develop
git merge --no-ff release/v1.0.0
git branch -d release/v1.0.0

GitHub Flow(简化版)

# 1. 从 main 创建分支
git checkout -b feature/description

# 2. 提交更改
git add .
git commit -m "Add feature"

# 3. 推送到远程
git push origin feature/description

# 4. 创建 Pull Request
# 5. 代码审查
# 6. 合并到 main

Trunk-Based Development

# 所有开发都在 main 分支
# 使用特性开关控制功能发布

# 创建短期分支(最多几天)
git checkout -b feature/short-lived

# 频繁合并回 main
git checkout main
git pull origin main
git merge feature/short-lived
git push origin main

高级操作

交互式变基

# 整理提交历史
git rebase -i HEAD~5

# 常用命令
# p, pick = 使用提交
# r, reword = 使用提交,但修改提交信息
# e, edit = 使用提交,但停止修改
# s, squash = 使用提交,但合并到前一个提交
# f, fixup = 类似 squash,但丢弃提交信息
# d, drop = 删除提交

暂存更改

# 暂存当前更改
git stash push -m "WIP: feature in progress"

# 查看暂存列表
git stash list

# 应用最近的暂存
git stash pop

# 应用特定暂存
git stash apply stash@{2}

# 删除暂存
git stash drop stash@{0}

# 清空所有暂存
git stash clear

Cherry Pick

# 将特定提交应用到当前分支
git cherry-pick abc1234

# 应用多个提交
git cherry-pick abc1234 def5678

# 应用一系列提交
git cherry-pick abc1234^..def5678

撤销操作

# 撤销工作区的修改
git checkout -- file.txt

# 撤销暂存区的文件
git reset HEAD file.txt

# 修改最后一次提交
git commit --amend -m "New message"

# 回退到指定版本(保留修改)
git reset --soft HEAD~1

# 回退到指定版本(丢弃修改)
git reset --hard abc1234

# 查看所有操作记录
git reflog

团队协作

解决冲突

# 拉取最新代码
git pull origin main

# 如果出现冲突,编辑冲突文件
# <<<<<<< HEAD
# 你的代码
# =======
# 别人的代码
# >>>>>>> branch-name

# 解决后标记为已解决
git add conflicted-file.txt

# 完成合并
git commit -m "Merge branch 'main' into feature"

代码审查

# 查看更改
git diff

# 查看特定文件的更改
git diff file.txt

# 查看某次提交的更改
git show abc1234

# 查看文件历史
git log -p file.txt

# 查看谁修改了某行代码
git blame file.txt

子模块管理

# 添加子模块
git submodule add https://github.com/user/repo.git libs/repo

# 克隆包含子模块的项目
git clone --recursive https://github.com/user/project.git

# 更新子模块
git submodule update --init --recursive

# 更新所有子模块到最新
git submodule update --remote

高级技巧

二分查找定位问题

# 开始二分查找
git bisect start

# 标记当前为有问题
git bisect bad

# 标记某个提交为正常
git bisect good v1.0

# Git 会自动切换到中间提交,测试后标记
git bisect good  # 或 git bisect bad

# 找到问题提交后重置
git bisect reset

签名提交

# 生成 GPG 密钥
gpg --full-generate-key

# 配置 Git 使用 GPG
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true

# 签名提交
git commit -S -m "Signed commit"

# 验证签名
git log --show-signature

大文件管理(Git LFS)

# 安装 Git LFS
git lfs install

# 追踪大文件
git lfs track "*.psd"
git lfs track "*.mp4"

# 提交追踪配置
git add .gitattributes
git commit -m "Track large files with LFS"

常用别名

# 添加到 ~/.gitconfig
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = !gitk
    amend = commit --amend --no-edit
    undo = reset HEAD~1 --mixed
    cleanup = !git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d

总结

掌握 Git 工作流的关键点:

  1. 选择合适的分支策略 - Git Flow、GitHub Flow 或 Trunk-Based
  2. 遵循提交规范 - 使用 Conventional Commits
  3. 保持提交历史整洁 - 使用交互式变基
  4. 善用暂存和别名 - 提高开发效率
  5. 重视代码审查 - 使用 Pull Request 工作流
  6. 学会撤销操作 - 了解各种撤销场景

通过合理使用 Git,你可以大大提高开发效率和代码质量。


本文首发于技术博客,转载请注明出处。