managing-git

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Managing Git

Git管理

When to Load

适用场景

  • Trigger: Branching strategies, commit workflows, pull requests, merge conflicts, version control questions
  • Skip: Tasks that do not involve git operations
  • 触发条件:分支策略、提交流程、pull request、合并冲突、版本控制相关问题
  • 不适用:不涉及Git操作的任务

Feature Development Workflow

功能开发工作流

Copy this checklist and track progress:
Feature Development Progress:
- [ ] Step 1: Create feature branch from main
- [ ] Step 2: Make changes with atomic commits
- [ ] Step 3: Rebase on latest main
- [ ] Step 4: Push and create PR
- [ ] Step 5: Address review feedback
- [ ] Step 6: Merge after approval
复制以下清单跟踪进度:
Feature Development Progress:
- [ ] Step 1: Create feature branch from main
- [ ] Step 2: Make changes with atomic commits
- [ ] Step 3: Rebase on latest main
- [ ] Step 4: Push and create PR
- [ ] Step 5: Address review feedback
- [ ] Step 6: Merge after approval

Branching Strategies

分支策略

GitHub Flow (Recommended for most projects)

GitHub Flow(多数项目推荐)

main ──●────●────●────●────●── (always deployable)
        \          /
feature  └──●──●──┘
  • main
    is always deployable
  • Feature branches from main
  • PR + review + merge
  • Deploy after merge
main ──●────●────●────●────●── (always deployable)
        \          /
feature  └──●──●──┘
  • main
    分支始终处于可部署状态
  • 功能分支从main分支拉出
  • 提交PR + 代码评审 + 合并
  • 合并后完成部署

Git Flow (For release-based projects)

Git Flow(适用于基于版本发布的项目)

main     ──●─────────────●────── (releases only)
            \           /
release      └────●────┘
                 /
develop  ──●──●────●──●──●──
            \     /
feature      └──●┘
main     ──●─────────────●────── (releases only)
            \           /
release      └────●────┘
                 /
develop  ──●──●────●──●──●──
            \     /
feature      └──●┘

Commit Conventions

提交规范

Conventional Commits Format

Conventional Commits 格式

<type>(<scope>): <description>

[optional body]

[optional footer(s)]
<type>(<scope>): <description>

[optional body]

[optional footer(s)]

Types

提交类型

TypeDescription
feat
New feature
fix
Bug fix
docs
Documentation only
style
Formatting, no logic change
refactor
Code change that neither fixes bug nor adds feature
perf
Performance improvement
test
Adding/updating tests
chore
Build process, dependencies
ci
CI configuration
类型说明
feat
新增功能
fix
修复Bug
docs
仅文档变更
style
代码格式调整,无逻辑变更
refactor
既不修复Bug也不新增功能的代码变更
perf
性能优化
test
新增/更新测试用例
chore
构建流程、依赖项相关变更
ci
CI配置相关变更

Examples

示例

bash
feat(auth): add OAuth2 login support

Implements Google and GitHub OAuth providers.
Closes #123

BREAKING CHANGE: Session tokens now expire after 24h
bash
fix(api): handle null response from payment gateway

Previously caused 500 error when gateway returned null.
Now returns appropriate error message to user.
bash
feat(auth): add OAuth2 login support

Implements Google and GitHub OAuth providers.
Closes #123

BREAKING CHANGE: Session tokens now expire after 24h
bash
fix(api): handle null response from payment gateway

Previously caused 500 error when gateway returned null.
Now returns appropriate error message to user.

Branch Naming

分支命名

<type>/<ticket-id>-<short-description>
<type>/<ticket-id>-<short-description>

Examples

Examples

feature/AUTH-123-oauth-login fix/BUG-456-null-pointer chore/TECH-789-upgrade-deps
undefined
feature/AUTH-123-oauth-login fix/BUG-456-null-pointer chore/TECH-789-upgrade-deps
undefined

Pull Request Workflow

Pull Request 工作流

Copy this checklist when creating PRs:
PR Checklist:
- [ ] Code follows project conventions
- [ ] Tests added/updated for changes
- [ ] All tests pass locally
- [ ] No merge conflicts with main
- [ ] Documentation updated if needed
- [ ] No security vulnerabilities introduced
- [ ] PR description explains the "why"
创建PR时可复制以下清单:
PR Checklist:
- [ ] Code follows project conventions
- [ ] Tests added/updated for changes
- [ ] All tests pass locally
- [ ] No merge conflicts with main
- [ ] Documentation updated if needed
- [ ] No security vulnerabilities introduced
- [ ] PR description explains the "why"

PR Template

PR模板

markdown
undefined
markdown
undefined

Summary

摘要

[Brief description of changes]
[变更内容简要说明]

Changes

变更内容

  • [Change 1]
  • [Change 2]
  • [变更1]
  • [变更2]

Testing

测试说明

  • Unit tests added/updated
  • Manual testing performed
  • E2E tests pass
  • 已新增/更新单元测试
  • 已完成手动测试
  • E2E测试全部通过

Screenshots (if UI changes)

截图(如有UI变更)

[Before/After screenshots]
undefined
[变更前/变更后截图]
undefined

PR Size Guidelines

PR大小规范

SizeLines ChangedReview Guidance
XS< 50Quick review
S50-200Standard review
M200-500Thorough review
L500+Split if possible
大小改动行数评审建议
XS< 50快速评审
S50-200标准评审
M200-500全面评审
L500+尽量拆分提交

Common Git Commands

常用Git命令

Daily Workflow

日常工作流

bash
undefined
bash
undefined

Start new feature

Start new feature

git checkout main git pull git checkout -b feature/TICKET-123-description
git checkout main git pull git checkout -b feature/TICKET-123-description

Commit changes

Commit changes

git add -p # Stage interactively git commit -m "feat: description"
git add -p # Stage interactively git commit -m "feat: description"

Keep up with main

Keep up with main

git fetch origin main git rebase origin/main
git fetch origin main git rebase origin/main

Push and create PR

Push and create PR

git push -u origin HEAD
undefined
git push -u origin HEAD
undefined

Fixing Mistakes

错误修正

bash
undefined
bash
undefined

Amend last commit (before push)

Amend last commit (before push)

git commit --amend
git commit --amend

Undo last commit (keep changes)

Undo last commit (keep changes)

git reset --soft HEAD~1
git reset --soft HEAD~1

Undo last commit (discard changes)

Undo last commit (discard changes)

git reset --hard HEAD~1
git reset --hard HEAD~1

Revert a pushed commit

Revert a pushed commit

git revert <commit-hash>
git revert <commit-hash>

Interactive rebase to clean up

Interactive rebase to clean up

git rebase -i HEAD~3
undefined
git rebase -i HEAD~3
undefined

Advanced Operations

高级操作

bash
undefined
bash
undefined

Cherry-pick specific commit

Cherry-pick specific commit

git cherry-pick <commit-hash>
git cherry-pick <commit-hash>

Find which commit broke something

Find which commit broke something

git bisect start git bisect bad HEAD git bisect good <known-good-commit>
git bisect start git bisect bad HEAD git bisect good <known-good-commit>

Stash with message

Stash with message

git stash push -m "WIP: feature description" git stash list git stash pop
undefined
git stash push -m "WIP: feature description" git stash list git stash pop
undefined

Commit Validation

提交校验

Before pushing, validate commits:
Commit Validation:
- [ ] Each commit has a clear, descriptive message
- [ ] Commit type matches the change (feat, fix, etc.)
- [ ] No WIP or temporary commits
- [ ] No secrets or credentials committed
- [ ] Changes are atomic (one logical change per commit)
If validation fails, use
git rebase -i
to clean up commit history before pushing.
推送前请校验提交内容:
Commit Validation:
- [ ] Each commit has a clear, descriptive message
- [ ] Commit type matches the change (feat, fix, etc.)
- [ ] No WIP or temporary commits
- [ ] No secrets or credentials committed
- [ ] Changes are atomic (one logical change per commit)
如果校验不通过,可在推送前使用
git rebase -i
清理提交历史。