git-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Workflow Standards

Git 工作流规范

This document defines the project's Git usage standards, including commit message format, branch management strategy, workflows, merge strategies, and more. Following these standards improves collaboration efficiency, enables traceability, supports automation, and reduces conflicts.
本文档定义了项目的Git使用标准,包含提交信息格式、分支管理策略、工作流程、合并策略等内容。遵循这些规范可以提升协作效率、实现变更可追溯、支持自动化流程并减少冲突。

Commit Message Standards

提交信息规范

The project follows the Conventional Commits specification:
<type>(<scope>): <subject>

<body>

<footer>
项目遵循 Conventional Commits 规范:
<type>(<scope>): <subject>

<body>

<footer>

Type Reference

类型参考

TypeDescriptionExample
feat
New feature
feat(user): add user export functionality
fix
Bug fix
fix(login): fix captcha not refreshing
docs
Documentation update
docs(api): update API documentation
refactor
Refactoring
refactor(utils): refactor utility functions
perf
Performance improvement
perf(list): optimize list performance
test
Test related
test(user): add unit tests
chore
Other changes
chore: update dependency versions
类型描述示例
feat
新增功能
feat(user): add user export functionality
fix
修复Bug
fix(login): fix captcha not refreshing
docs
文档更新
docs(api): update API documentation
refactor
代码重构
refactor(utils): refactor utility functions
perf
性能优化
perf(list): optimize list performance
test
测试相关
test(user): add unit tests
chore
其他变更
chore: update dependency versions

Subject Rules

主题规则

  • Start with a verb: add, fix, update, remove, optimize
  • No more than 50 characters
  • No period at the end
For more detailed conventions and examples, see
references/commit-conventions.md
.
  • 以动词开头:add、fix、update、remove、optimize
  • 不超过50个字符
  • 末尾不加句号
更详细的约定和示例请查看
references/commit-conventions.md

Branch Management Strategy

分支管理策略

Branch Types

分支类型

Branch TypeNaming ConventionDescriptionLifecycle
master
master
Main branch, releasable statePermanent
develop
develop
Development branch, latest integrated codePermanent
feature
feature/feature-name
Feature branchDelete after completion
bugfix
bugfix/issue-description
Bug fix branchDelete after fix
hotfix
hotfix/issue-description
Emergency fix branchDelete after fix
release
release/version-number
Release branchDelete after release
分支类型命名规则描述生命周期
master
master
主分支,处于可发布状态永久存在
develop
develop
开发分支,包含最新集成代码永久存在
feature
feature/feature-name
功能分支开发完成后删除
bugfix
bugfix/issue-description
Bug修复分支修复完成后删除
hotfix
hotfix/issue-description
紧急修复分支修复完成后删除
release
release/version-number
发布分支发布完成后删除

Branch Naming Examples

分支命名示例

feature/user-management          # User management feature
feature/123-add-export          # Issue-linked feature
bugfix/login-error              # Login error fix
hotfix/security-vulnerability   # Security vulnerability fix
release/v1.0.0                  # Version release
feature/user-management          # 用户管理功能
feature/123-add-export          # 关联Issue的功能开发
bugfix/login-error              # 登录错误修复
hotfix/security-vulnerability   # 安全漏洞修复
release/v1.0.0                  # 版本发布

Branch Protection Rules

分支保护规则

master branch:
  • No direct pushes allowed
  • Must merge via Pull Request
  • Must pass CI checks
  • Requires at least one Code Review approval
develop branch:
  • Direct pushes restricted
  • Pull Request merges recommended
  • Must pass CI checks
For detailed branch strategies and workflows, see
references/branching-strategies.md
.
master分支:
  • 禁止直接推送
  • 必须通过Pull Request合并
  • 必须通过CI检查
  • 至少需要1个Code Review审批
develop分支:
  • 限制直接推送
  • 推荐通过Pull Request合并
  • 必须通过CI检查
详细的分支策略和工作流请查看
references/branching-strategies.md

Workflows

工作流程

Daily Development Workflow

日常开发工作流

bash
undefined
bash
undefined

1. Sync latest code

1. 同步最新代码

git checkout develop git pull origin develop
git checkout develop git pull origin develop

2. Create feature branch

2. 创建功能分支

git checkout -b feature/user-management
git checkout -b feature/user-management

3. Develop and commit

3. 开发并提交

git add . git commit -m "feat(user): add user list page"
git add . git commit -m "feat(user): add user list page"

4. Push to remote

4. 推送到远端

git push -u origin feature/user-management
git push -u origin feature/user-management

5. Create Pull Request and request Code Review

5. 创建Pull Request并申请Code Review

6. Merge to develop (via PR)

6. (通过PR)合并到develop分支

7. Delete feature branch

7. 删除功能分支

git branch -d feature/user-management git push origin -d feature/user-management
undefined
git branch -d feature/user-management git push origin -d feature/user-management
undefined

Hotfix Workflow

热修复工作流

bash
undefined
bash
undefined

1. Create fix branch from master

1. 从master创建修复分支

git checkout master git pull origin master git checkout -b hotfix/critical-bug
git checkout master git pull origin master git checkout -b hotfix/critical-bug

2. Fix and commit

2. 修复问题并提交

git add . git commit -m "fix(auth): fix authentication bypass vulnerability"
git add . git commit -m "fix(auth): fix authentication bypass vulnerability"

3. Merge to master

3. 合并到master

git checkout master git merge --no-ff hotfix/critical-bug git tag -a v1.0.1 -m "hotfix: fix authentication bypass vulnerability" git push origin master --tags
git checkout master git merge --no-ff hotfix/critical-bug git tag -a v1.0.1 -m "hotfix: fix authentication bypass vulnerability" git push origin master --tags

4. Sync to develop

4. 同步到develop分支

git checkout develop git merge --no-ff hotfix/critical-bug git push origin develop
undefined
git checkout develop git merge --no-ff hotfix/critical-bug git push origin develop
undefined

Release Workflow

发布工作流

bash
undefined
bash
undefined

1. Create release branch

1. 创建发布分支

git checkout develop git checkout -b release/v1.0.0
git checkout develop git checkout -b release/v1.0.0

2. Update version numbers and documentation

2. 更新版本号和相关文档

3. Commit version update

3. 提交版本更新

git add . git commit -m "chore(release): prepare release v1.0.0"
git add . git commit -m "chore(release): prepare release v1.0.0"

4. Merge to master

4. 合并到master

git checkout master git merge --no-ff release/v1.0.0 git tag -a v1.0.0 -m "release: v1.0.0 official release" git push origin master --tags
git checkout master git merge --no-ff release/v1.0.0 git tag -a v1.0.0 -m "release: v1.0.0 official release" git push origin master --tags

5. Sync to develop

5. 同步到develop分支

git checkout develop git merge --no-ff release/v1.0.0 git push origin develop
undefined
git checkout develop git merge --no-ff release/v1.0.0 git push origin develop
undefined

Merge Strategy

合并策略

Merge vs Rebase

Merge 与 Rebase 对比

FeatureMergeRebase
HistoryPreserves complete historyLinear history
Use casePublic branchesPrivate branches
Recommended forMerging to main branchSyncing upstream code
特性MergeRebase
提交历史保留完整历史线性历史
适用场景公共分支私有分支
推荐用途合并到主分支同步上游代码

Recommendations

使用建议

  • Feature branch syncing develop: Use
    rebase
  • Feature branch merging to develop: Use
    merge --no-ff
  • develop merging to master: Use
    merge --no-ff
bash
undefined
  • 功能分支同步develop代码:使用
    rebase
  • 功能分支合并到develop:使用
    merge --no-ff
  • develop合并到master:使用
    merge --no-ff
bash
undefined

✅ Recommended: Feature branch syncing develop

✅ 推荐:功能分支同步develop代码

git checkout feature/user-management git rebase develop
git checkout feature/user-management git rebase develop

✅ Recommended: Merge feature branch to develop

✅ 推荐:功能分支合并到develop

git checkout develop git merge --no-ff feature/user-management
git checkout develop git merge --no-ff feature/user-management

❌ Not recommended: Rebase on public branch

❌ 不推荐:在公共分支上执行rebase

git checkout develop git rebase feature/xxx # Dangerous operation

**Project convention**: Use `--no-ff` when merging feature branches to preserve branch history.

For detailed merge strategies and techniques, see `references/merge-strategies.md`.
git checkout develop git rebase feature/xxx # 危险操作

**项目约定**:合并功能分支时使用`--no-ff`保留分支历史。

详细的合并策略和技巧请查看 `references/merge-strategies.md`。

Conflict Resolution

冲突解决

Identifying Conflicts

识别冲突

<<<<<<< HEAD
// Current branch code
const name = 'Alice'
=======
// Branch being merged
const name = 'Bob'
>>>>>>> feature/user-management
<<<<<<< HEAD
// 当前分支代码
const name = 'Alice'
=======
// 待合并分支代码
const name = 'Bob'
>>>>>>> feature/user-management

Resolving Conflicts

解决冲突

bash
undefined
bash
undefined

1. View conflicting files

1. 查看冲突文件

git status
git status

2. Manually edit files to resolve conflicts

2. 手动编辑文件解决冲突

3. Mark as resolved

3. 标记为已解决

git add <file>
git add <file>

4. Complete the merge

4. 完成合并

git commit # merge conflict
git commit # merge场景冲突

or

git rebase --continue # rebase conflict
undefined
git rebase --continue # rebase场景冲突
undefined

Conflict Resolution Strategies

冲突解决命令

bash
undefined
bash
undefined

Keep current branch version

保留当前分支版本

git checkout --ours <file>
git checkout --ours <file>

Keep incoming branch version

保留待合并分支版本

git checkout --theirs <file>
git checkout --theirs <file>

Abort merge

终止合并操作

git merge --abort git rebase --abort
undefined
git merge --abort git rebase --abort
undefined

Preventing Conflicts

冲突预防

  1. Sync code regularly - Pull latest code before starting work each day
  2. Small commits - Commit small changes frequently
  3. Modular features - Implement different features in different files
  4. Communication - Avoid modifying the same file simultaneously
For detailed conflict handling and advanced techniques, see
references/conflict-resolution.md
.
  1. 定期同步代码 - 每天开始工作前拉取最新代码
  2. 小粒度提交 - 频繁提交小范围变更
  3. 功能模块化 - 不同功能在不同文件中实现
  4. 团队沟通 - 避免同时修改同一文件
详细的冲突处理和高级技巧请查看
references/conflict-resolution.md

.gitignore Standards

.gitignore 规范

Basic Rules

基础规则

undefined
undefined

Ignore all .log files

忽略所有.log文件

*.log
*.log

Ignore directories

忽略目录

node_modules/
node_modules/

Ignore directory at root

忽略根目录下的指定目录

/temp/
/temp/

Ignore files in all directories

忽略所有目录下的指定文件

**/.env
**/.env

Don't ignore specific files

不忽略特定文件

!.gitkeep
undefined
!.gitkeep
undefined

Common .gitignore

常用配置

node_modules/
dist/
build/
.idea/
.vscode/
.env
.env.local
logs/
*.log
.DS_Store
Thumbs.db
For detailed .gitignore patterns and project-specific configurations, see
references/gitignore-guide.md
.
node_modules/
dist/
build/
.idea/
.vscode/
.env
.env.local
logs/
*.log
.DS_Store
Thumbs.db
详细的.gitignore模式和项目专属配置请查看
references/gitignore-guide.md

Tag Management

标签管理

Uses Semantic Versioning:
MAJOR.MINOR.PATCH[-PRERELEASE]
采用 Semantic Versioning 规范:
MAJOR.MINOR.PATCH[-PRERELEASE]

Version Change Rules

版本变更规则

  • MAJOR: Incompatible API changes (v1.0.0 → v2.0.0)
  • MINOR: Backward-compatible new features (v1.0.0 → v1.1.0)
  • PATCH: Backward-compatible bug fixes (v1.0.0 → v1.0.1)
  • MAJOR:不兼容的API变更 (v1.0.0 → v2.0.0)
  • MINOR:向后兼容的新增功能 (v1.0.0 → v1.1.0)
  • PATCH:向后兼容的Bug修复 (v1.0.0 → v1.0.1)

Tag Operations

标签操作

bash
undefined
bash
undefined

Create annotated tag (recommended)

创建附注标签(推荐)

git tag -a v1.0.0 -m "release: v1.0.0 official release"
git tag -a v1.0.0 -m "release: v1.0.0 official release"

Push tags

推送标签

git push origin v1.0.0 git push origin --tags
git push origin v1.0.0 git push origin --tags

View tags

查看标签

git tag git show v1.0.0
git tag git show v1.0.0

Delete tag

删除标签

git tag -d v1.0.0 git push origin :refs/tags/v1.0.0
undefined
git tag -d v1.0.0 git push origin :refs/tags/v1.0.0
undefined

Team Collaboration Standards

团队协作规范

Pull Request Standards

Pull Request 规范

PRs should include:
markdown
undefined
PR需要包含以下内容:
markdown
undefined

Change Description

变更描述

<!-- Describe the content and purpose of this change -->
<!-- 描述本次变更的内容和目的 -->

Change Type

变更类型

  • New feature (feat)
  • Bug fix (fix)
  • Code refactoring (refactor)
  • 新增功能 (feat)
  • Bug修复 (fix)
  • 代码重构 (refactor)

Testing Method

测试方式

<!-- Describe how to test -->
<!-- 描述如何验证变更效果 -->

Related Issue

关联Issue

Closes #xxx
Closes #xxx

Checklist

检查清单

  • Code has been self-tested
  • Documentation has been updated
undefined
  • 代码已完成自测
  • 相关文档已更新
undefined

Code Review Standards

Code Review 规范

Review focus areas:
  • Code quality: Clear and readable, proper naming, no duplicate code
  • Logic correctness: Business logic correct, edge cases handled
  • Security: No security vulnerabilities, sensitive information protected
  • Performance: No obvious performance issues, resources properly released
For detailed collaboration standards and best practices, see
references/collaboration.md
.
评审重点:
  • 代码质量:清晰可读、命名规范、无重复代码
  • 逻辑正确性:业务逻辑正确、边缘场景已处理
  • 安全性:无安全漏洞、敏感信息已保护
  • 性能:无明显性能问题、资源已正确释放
详细的协作规范和最佳实践请查看
references/collaboration.md

Common Issues

常见问题

Amending the Last Commit

修改最后一次提交

bash
undefined
bash
undefined

Amend commit content (not yet pushed)

修改提交内容(未推送到远端时)

git add forgotten-file.ts git commit --amend --no-edit
git add forgotten-file.ts git commit --amend --no-edit

Amend commit message

修改提交信息

git commit --amend -m "new commit message"
undefined
git commit --amend -m "new commit message"
undefined

Push Rejected

推送被拒绝

bash
undefined
bash
undefined

Pull then push

先拉取再推送

git pull origin master git push origin master
git pull origin master git push origin master

Use rebase for cleaner history

使用rebase获得更整洁的历史

git pull --rebase origin master git push origin master
undefined
git pull --rebase origin master git push origin master
undefined

Rollback to Previous Version

回滚到之前的版本

bash
undefined
bash
undefined

Reset to specific commit (discards subsequent commits)

重置到指定提交(会丢弃后续提交)

git reset --hard abc123
git reset --hard abc123

Create reverse commit (recommended, preserves history)

创建反向提交(推荐,保留历史)

git revert abc123
undefined
git revert abc123
undefined

Stash Current Work

暂存当前工作

bash
git stash save "work in progress"
git stash list
git stash pop
bash
git stash save "work in progress"
git stash list
git stash pop

View File Modification History

查看文件修改历史

bash
git log -- <file>             # Commit history
git log -p -- <file>          # Detailed content
git blame <file>              # Per-line author
bash
git log -- <file>             # 提交历史
git log -p -- <file>          # 详细变更内容
git blame <file>              # 逐行修改人信息

Best Practices Summary

最佳实践总结

Commit Standards

提交规范

Recommended:
  • Follow Conventional Commits specification
  • Write clear commit messages describing changes
  • One commit for one logical change
  • Run code checks before committing
Prohibited:
  • Vague commit messages
  • Multiple unrelated changes in one commit
  • Committing sensitive information (passwords, keys)
  • Developing directly on main branch
推荐
  • 遵循Conventional Commits规范
  • 编写清晰描述变更的提交信息
  • 一个提交对应一个逻辑变更
  • 提交前执行代码检查
禁止
  • 提交信息模糊不清
  • 一个提交包含多个不相关的变更
  • 提交敏感信息(密码、密钥等)
  • 直接在主分支上开发

Branch Management

分支管理

Recommended:
  • Use feature branches for development
  • Regularly sync main branch code
  • Delete branches promptly after feature completion
  • Use
    --no-ff
    merge to preserve history
Prohibited:
  • Developing directly on main branch
  • Long-lived unmerged feature branches
  • Non-standard branch naming
  • Rebasing on public branches
推荐
  • 使用功能分支进行开发
  • 定期同步主分支代码
  • 功能完成后及时删除分支
  • 使用
    --no-ff
    合并保留历史
禁止
  • 直接在主分支上开发
  • 长期存在未合并的功能分支
  • 分支命名不规范
  • 在公共分支上执行rebase

Code Review

代码评审

Recommended:
  • All code goes through Pull Requests
  • At least one reviewer approval before merging
  • Provide constructive feedback
Prohibited:
  • Merging without review
  • Reviewing your own code
推荐
  • 所有代码都通过Pull Request提交
  • 合并前至少获得1个评审人审批
  • 提供有建设性的反馈意见
禁止
  • 未经评审直接合并
  • 评审自己提交的代码

Additional Resources

额外资源

Reference Files

参考文件

For detailed guidance on specific topics:
  • references/commit-conventions.md
    - Commit message detailed conventions and examples
  • references/branching-strategies.md
    - Comprehensive branch management strategies
  • references/merge-strategies.md
    - Merge, rebase, and conflict resolution strategies
  • references/conflict-resolution.md
    - Detailed conflict handling and prevention
  • references/advanced-usage.md
    - Git performance optimization, security, submodules, and advanced techniques
  • references/collaboration.md
    - Pull request and code review guidelines
  • references/gitignore-guide.md
    - .gitignore patterns and project-specific configurations
特定主题的详细指南:
  • references/commit-conventions.md
    - 提交信息详细约定和示例
  • references/branching-strategies.md
    - 完整的分支管理策略
  • references/merge-strategies.md
    - 合并、rebase和冲突解决策略
  • references/conflict-resolution.md
    - 详细的冲突处理和预防方法
  • references/advanced-usage.md
    - Git性能优化、安全、子模块等高级技巧
  • references/collaboration.md
    - Pull Request和代码评审指南
  • references/gitignore-guide.md
    - .gitignore模式和项目专属配置

Example Files

示例文件

Working examples in
examples/
:
  • examples/commit-messages.txt
    - Good commit message examples
  • examples/workflow-commands.sh
    - Common workflow command snippets
examples/
目录下的可运行示例:
  • examples/commit-messages.txt
    - 优秀提交信息示例
  • examples/workflow-commands.sh
    - 常用工作流命令片段

Summary

总结

This document defines the project's Git standards:
  1. Commit Messages - Follow Conventional Commits specification
  2. Branch Management - master/develop/feature/bugfix/hotfix/release branch strategy
  3. Workflows - Standard processes for daily development, hotfixes, and releases
  4. Merge Strategy - Use rebase to sync feature branches, merge --no-ff to merge
  5. Tag Management - Semantic versioning, annotated tags
  6. Conflict Resolution - Regular syncing, small commits, team communication
Following these standards improves collaboration efficiency, ensures code quality, and simplifies version management.
本文档定义了项目的Git标准:
  1. 提交信息 - 遵循Conventional Commits规范
  2. 分支管理 - master/develop/feature/bugfix/hotfix/release分支策略
  3. 工作流程 - 日常开发、热修复、发布的标准流程
  4. 合并策略 - 功能分支同步用rebase,合并用merge --no-ff
  5. 标签管理 - 语义化版本,使用附注标签
  6. 冲突解决 - 定期同步、小粒度提交、团队沟通
遵循这些规范可以提升协作效率、保障代码质量、简化版本管理。