git-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Workflow
Git工作流
Complete git workflow patterns: GitHub Flow branching, atomic commits, and recovery operations. Essential for maintaining clean, reviewable history.
完整的Git工作流模式:GitHub Flow分支管理、原子提交以及恢复操作。是保持提交记录整洁、便于评审的核心指南。
Branch Naming Convention
分支命名规范
bash
undefinedbash
undefinedFeature branches (link to issue)
功能分支(关联议题)
issue/<number>-<brief-description>
issue/123-add-user-auth
issue/<编号>-<简要描述>
issue/123-add-user-auth
When no issue exists
无对应议题时
feature/<description>
fix/<description>
hotfix/<description>
**Branch Rules:**
1. `main` is always deployable
2. Branch from `main`, PR back to `main`
3. Branches live < 1-3 days
4. Delete branch after merge
---feature/<描述>
fix/<描述>
hotfix/<描述>
**分支规则:**
1. `main`分支始终处于可部署状态
2. 从`main`分支创建新分支,合并请求(PR)提交回`main`
3. 分支生命周期不超过1-3天
4. 合并后删除分支
---Atomic Commit Checklist
原子提交检查清单
[ ] Does ONE logical thing
[ ] Leaves codebase working (tests pass)
[ ] Message doesn't need "and" in title
[ ] Can be reverted independently
[ ] Title < 50 chars, body wraps at 72[ ] 仅包含一个逻辑单元的变更
[ ] 提交后代码库可正常运行(测试通过)
[ ] 提交信息标题中无需使用“和”连接内容
[ ] 可独立回滚
[ ] 标题不超过50字符,正文每行不超过72字符Interactive Staging
交互式暂存
bash
undefinedbash
undefinedStage changes hunk-by-hunk
分块暂存变更
git add -p
git add -p
Options:
选项说明:
y - stage this hunk
y - 暂存当前块
n - skip this hunk
n - 跳过当前块
s - split into smaller hunks
s - 将当前块拆分为更小的块
e - manually edit the hunk
e - 手动编辑当前块
q - quit
q - 退出
Review what's staged
查看已暂存内容
git diff --staged # What will be committed
git diff # What won't be committed
undefinedgit diff --staged # 即将提交的内容
git diff # 未暂存的内容
undefinedCommit Patterns
提交模式
bash
undefinedbash
undefinedSeparate concerns
分离关注点
git add -p && git commit -m "refactor: Extract database pool"
git add -p && git commit -m "feat(#456): Add query caching"
git add -p && git commit -m "refactor: Extract database pool"
git add -p && git commit -m "feat(#456): Add query caching"
Never combine unrelated changes
切勿合并不相关的变更
BAD: "feat: Add auth and fix formatting"
错误示例: "feat: Add auth and fix formatting"
GOOD: Two separate commits
正确做法:拆分为两个独立提交
---
---Recovery Quick Reference
快速恢复参考
The Safety Net
安全保障:Reflog
bash
undefinedbash
undefinedALWAYS check reflog first - it has everything
遇到问题先查看reflog - 它记录了所有操作
git reflog
git reflog
Shows ALL recent HEAD movements
显示所有最近的HEAD移动记录
Even "deleted" commits live here for 90 days
即使是“已删除”的提交也会在此保留90天
undefinedundefinedCommon Recovery Scenarios
常见恢复场景
| Scenario | Not Pushed | Already Pushed |
|---|---|---|
| Undo commit | | |
| Wrong branch | cherry-pick + reset | cherry-pick + revert |
| Lost commits | | N/A |
| Bad rebase | | reflog + force-with-lease |
| 场景 | 未推送到远程 | 已推送到远程 |
|---|---|---|
| 撤销提交 | | |
| 提交到错误分支 | cherry-pick + reset | cherry-pick + revert |
| 找回丢失的提交 | | 不适用 |
| 错误变基 | | reflog + force-with-lease |
Quick Recovery Commands
快速恢复命令
bash
undefinedbash
undefinedUndo last commit, keep changes staged
撤销最后一次提交,保留已暂存的变更
git reset --soft HEAD~1
git reset --soft HEAD~1
Find lost commits
查找丢失的提交
git reflog | grep "your message"
git reflog | grep "你的提交信息"
Recover to previous state
恢复到之前的状态
git reset --hard HEAD@{1}
git reset --hard HEAD@{1}
Safe force push (feature branches only)
安全强制推送(仅用于功能分支)
git push --force-with-lease
---git push --force-with-lease
---Standard Workflow
标准工作流
bash
undefinedbash
undefined1. Start fresh
1. 拉取最新代码
git checkout main && git pull origin main
git checkout -b issue/123-my-feature
git checkout main && git pull origin main
git checkout -b issue/123-my-feature
2. Work with atomic commits
2. 进行原子提交
git add -p
git commit -m "feat(#123): Add User model"
git add -p
git commit -m "feat(#123): Add User model"
3. Stay updated
3. 保持代码同步
git fetch origin && git rebase origin/main
git fetch origin && git rebase origin/main
4. Push and PR
4. 推送并创建PR
git push -u origin issue/123-my-feature
gh pr create --fill
git push -u origin issue/123-my-feature
gh pr create --fill
5. Cleanup after merge
5. 合并后清理
git checkout main && git pull
git branch -d issue/123-my-feature
---git checkout main && git pull
git branch -d issue/123-my-feature
---Anti-Patterns
反模式(需避免)
Avoid:
- Long-lived branches (> 1 week)
- Merging main into feature (use rebase)
- Direct commits to main
- Force push to shared branches
- Commits that need "and" in message
- Committing broken code避免以下做法:
- 长期存在的分支(超过1周)
- 将main分支合并到功能分支(应使用变基)
- 直接提交到main分支
- 对共享分支执行强制推送
- 提交信息标题中包含“和”的内容
- 提交无法运行的代码Best Practices Summary
最佳实践总结
- Branch from main - Always start fresh
- Stage interactively - Use
git add -p - One thing per commit - If you say "and", split it
- Rebase, don't merge - Keep history clean
- Check reflog first - When something goes wrong
- Force-with-lease - Safer than force push
- Delete after merge - No stale branches
- 从main分支创建新分支 - 始终基于最新代码开始工作
- 使用交互式暂存 - 采用命令
git add -p - 每次提交仅做一件事 - 如果需要用“和”描述,就拆分提交
- 使用变基而非合并 - 保持提交记录整洁线性
- 遇到问题先查reflog - 这是恢复的首选方式
- 使用--force-with-lease进行强制推送 - 比普通force push更安全
- 合并后删除分支 - 避免遗留 stale分支
Related Skills
相关技能
- - Create commits with conventional format and pre-commit validation
ork:commit - - Quick recovery from common git mistakes using reflog operations
git-recovery - - Multi-PR development for large features with dependent PRs
stacked-prs - - Comprehensive PR creation with proper formatting
ork:create-pr
- - 按照约定格式创建提交,并进行预提交验证
ork:commit - - 使用reflog操作快速恢复常见Git错误
git-recovery - - 针对大型功能的多PR开发,处理依赖PR
stacked-prs - - 创建格式规范的完整PR
ork:create-pr
Key Decisions
核心决策
| Decision | Choice | Rationale |
|---|---|---|
| Branching model | GitHub Flow | Simple single-branch workflow, main is always deployable |
| Merge strategy | Rebase over merge | Keeps history clean and linear, easier to bisect |
| Branch naming | issue/<number>-<desc> | Links work to tracking, enables automation |
| Commit granularity | Atomic (one thing) | Independent revert, clear history, easier review |
| Force push | --force-with-lease only | Prevents overwriting others' work on shared branches |
| 决策 | 选择 | 理由 |
|---|---|---|
| 分支模型 | GitHub Flow | 简单的单分支工作流,main始终可部署 |
| 合并策略 | 优先变基而非合并 | 保持提交记录整洁线性,便于二分查找问题 |
| 分支命名 | issue/<编号>-<描述> | 关联工作追踪系统,支持自动化操作 |
| 提交粒度 | 原子化(仅一件事) | 可独立回滚,提交记录清晰,便于评审 |
| 强制推送 | 仅使用--force-with-lease | 防止覆盖共享分支上他人的工作 |
Rules
规则
Each category has individual rule files in loaded on-demand:
rules/| Category | Rule | Impact | Key Pattern |
|---|---|---|---|
| Branch Protection | | CRITICAL | Protected branches, required PR workflow |
| Merge Strategy | | HIGH | Rebase-first, conflict resolution, force-with-lease |
| History Hygiene | | HIGH | Squash WIP, fixup commits, clean history |
| Recovery | | CRITICAL | Reflog recovery for lost commits and branches |
| Recovery | | CRITICAL | Safe vs dangerous reset modes |
| Recovery | | HIGH | Stash management and dropped stash recovery |
| Stacked PRs | | HIGH | Stack planning, PR creation, dependency tracking |
| Stacked PRs | | HIGH | Rebase management, force-with-lease, retargeting |
| Monorepo | | MEDIUM | --add-dir, per-service CLAUDE.md, workspace detection |
Total: 9 rules across 6 categories
每个分类的独立规则文件位于目录,按需加载:
rules/| 分类 | 规则 | 影响等级 | 核心模式 |
|---|---|---|---|
| 分支保护 | | 关键 | 受保护分支,强制PR工作流 |
| 合并策略 | | 高 | 优先变基,冲突解决,force-with-lease |
| 提交记录整洁性 | | 高 | 压缩WIP提交,fixup提交,保持记录整洁 |
| 恢复操作 | | 关键 | 使用Reflog恢复丢失的提交和分支 |
| 恢复操作 | | 关键 | 安全与危险的reset模式区分 |
| 恢复操作 | | 高 | Stash管理及丢失stash的恢复 |
| 堆叠PR | | 高 | 堆叠规划,PR创建,依赖追踪 |
| 堆叠PR | | 高 | 变基管理,force-with-lease,重新定向 |
| 单体仓库 | | 中 | --add-dir,按服务的CLAUDE.md,工作区检测 |
总计:6个分类下的9条规则
References
参考资料
- GitHub Flow Guide
- Interactive Staging
- Reflog Recovery
- Recovery Decision Tree
- GitHub Flow Guide
- Interactive Staging
- Reflog Recovery
- Recovery Decision Tree
Checklists
检查清单
- Branch Checklist - Pre-flight checks before creating branches
- Pre-Commit Checklist - Validation before committing
- Branch Checklist - 创建分支前的预检查清单
- Pre-Commit Checklist - 提交前的验证清单