git
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Skills & Best Practices
Git技能与最佳实践
Git workflow standards, commit conventions, hooks, and pull request practices.
Git工作流标准、提交规范、钩子以及拉取请求实践。
Table of Contents
目录
Git Workflow
Git工作流
Conventional Commits
Conventional Commits
MUST use Conventional Commits format - this is a strict requirement. Follow the Conventional Commits specification:
Format:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]Types:
- : A new feature
feat - : A bug fix
fix - : Documentation only changes
docs - : Changes that do not affect the meaning of the code
style - : A code change that neither fixes a bug nor adds a feature
refactor - : A code change that improves performance
perf - : Adding missing tests or correcting existing tests
test - : Changes to the build process or auxiliary tools
chore
Examples:
feat(auth): add user login functionality
fix(api): resolve race condition in data fetching
docs: update README with setup instructions必须使用Conventional Commits格式——这是硬性要求。遵循Conventional Commits规范:
格式:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]类型:
- : 新增功能
feat - : 修复bug
fix - : 仅修改文档
docs - : 不影响代码含义的修改(如格式调整)
style - : 既不是修复bug也不是新增功能的代码变更
refactor - : 提升性能的代码变更
perf - : 添加缺失的测试或修正现有测试
test - : 构建流程或辅助工具的变更
chore
示例:
feat(auth): add user login functionality
fix(api): resolve race condition in data fetching
docs: update README with setup instructions.gitignore
.gitignore
Include standard exclusions for React/React Native projects:
Essential entries:
undefined包含React/React Native项目的标准排除项:
核心条目:
undefinedEnvironment variables
Environment variables
.env
.env.local
.env.*.local
.env
.env.local
.env.*.local
Dependencies
Dependencies
node_modules/
.pnp
.pnp.js
node_modules/
.pnp
.pnp.js
Build outputs
Build outputs
dist/
build/
.next/
out/
dist/
build/
.next/
out/
Testing
Testing
coverage/
.nyc_output/
coverage/
.nyc_output/
IDE
IDE
.vscode/
.idea/
*.swp
*.swo
*~
.vscode/
.idea/
*.swp
*.swo
*~
OS
OS
.DS_Store
Thumbs.db
.DS_Store
Thumbs.db
Logs
Logs
.log
npm-debug.log
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
pnpm-debug.log*
.log
npm-debug.log
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
pnpm-debug.log*
pnpm
pnpm
.pnpm-store/
.pnpm-store/
Misc
Misc
.cache/
.temp/
.turbo/
---.cache/
.temp/
.turbo/
---Git Hooks
Git钩子
Lefthook Integration
Lefthook集成
PREFER using Lefthook for managing git hooks (guideline). Other tools are acceptable if they meet the strict hook requirements below.
Configuration ():
lefthook.ymlyaml
pre-commit:
parallel: true
commands:
lint:
run: pnpm run lint
stage_fixed: true
format:
run: pnpm run format:check
stage_fixed: true
test:
run: pnpm run test:ci
stage_fixed: true
pre-push:
parallel: true
commands:
test:
run: pnpm run test
typecheck:
run: pnpm run typecheckPre-commit hooks (Strict Requirements):
- MUST run linting checks
- MUST run formatting checks
- MUST run unit tests
- Auto-fix issues when possible
Pre-push hooks (Strict Requirements):
- MUST run full test suite
- MUST run TypeScript type checking ()
tsc - MUST prevent pushing if checks fail
优先推荐使用Lefthook管理Git钩子(指导原则)。如果其他工具能满足以下严格的钩子要求,也可以使用。
配置文件 ():
lefthook.ymlyaml
pre-commit:
parallel: true
commands:
lint:
run: pnpm run lint
stage_fixed: true
format:
run: pnpm run format:check
stage_fixed: true
test:
run: pnpm run test:ci
stage_fixed: true
pre-push:
parallel: true
commands:
test:
run: pnpm run test
typecheck:
run: pnpm run typecheck提交前钩子(硬性要求):
- 必须运行代码检查
- 必须运行格式检查
- 必须运行单元测试
- 尽可能自动修复问题
推送前钩子(硬性要求):
- 必须运行完整测试套件
- 必须运行TypeScript类型检查 ()
tsc - 必须在检查不通过时阻止推送
Pull Request Templates
拉取请求模板
Create with the following structure:
.github/pull_request_template.mdmarkdown
undefined创建文件,结构如下:
.github/pull_request_template.mdmarkdown
undefinedOverview
Overview
<!-- Brief description of what this PR accomplishes -->
<!-- Brief description of what this PR accomplishes -->
Solution
Solution
<!-- Detailed explanation of the approach and implementation -->
<!-- Detailed explanation of the approach and implementation -->
Screenshots
Screenshots
<!-- Add screenshots or screen recordings if applicable -->
<!-- For web: browser screenshots -->
<!-- For mobile: iOS/Android screenshots -->
<!-- Add screenshots or screen recordings if applicable -->
<!-- For web: browser screenshots -->
<!-- For mobile: iOS/Android screenshots -->
Ticket
Ticket
<!-- Link to JIRA ticket or other project management tool -->
<!-- Format: [PROJECT-123](link-to-ticket) -->
<!-- Link to JIRA ticket or other project management tool -->
<!-- Format: [PROJECT-123](link-to-ticket) -->
Tested On
Tested On
<!-- Check all that apply -->
- Web
- Mobile
- iOS
- Android
- Other: ___________
<!-- Check all that apply -->
- Web
- Mobile
- iOS
- Android
- Other: ___________
Additional Notes
Additional Notes
<!-- Any additional context, breaking changes, or follow-up items -->
---<!-- Any additional context, breaking changes, or follow-up items -->
---GitHub CLI (gh) Integration
GitHub CLI (gh) 集成
The GitHub CLI () enables AI agents to interact with GitHub repositories programmatically, automating common Git and GitHub workflows.
ghGitHub CLI()允许AI Agent以编程方式与GitHub仓库交互,自动化常见的Git和GitHub工作流。
ghAuthentication
身份验证
Setup:
bash
undefined设置步骤:
bash
undefinedLogin to GitHub
Login to GitHub
gh auth login
gh auth login
Check authentication status
Check authentication status
gh auth status
gh auth status
Refresh authentication token
Refresh authentication token
gh auth refresh
**For AI Agents:**
- Use `gh auth token` to retrieve the authentication token for API calls
- Configure authentication before performing any GitHub operations
- Use `gh auth setup-git` to configure Git credentials automaticallygh auth refresh
**针对AI Agent:**
- 使用`gh auth token`获取API调用所需的身份验证令牌
- 在执行任何GitHub操作前配置身份验证
- 使用`gh auth setup-git`自动配置Git凭据Pull Request Management
拉取请求管理
Create Pull Requests:
bash
undefined创建拉取请求:
bash
undefinedCreate a PR interactively
Create a PR interactively
gh pr create
gh pr create
Create a PR with title and body
Create a PR with title and body
gh pr create --title "feat: add user authentication" --body "Implements OAuth2 login flow"
gh pr create --title "feat: add user authentication" --body "Implements OAuth2 login flow"
Create a PR from current branch to main
Create a PR from current branch to main
gh pr create --base main --head feature/auth --title "feat: add authentication"
gh pr create --base main --head feature/auth --title "feat: add authentication"
Create a draft PR
Create a draft PR
gh pr create --draft
**View and Manage PRs:**
```bashgh pr create --draft
**查看和管理拉取请求:**
```bashList open PRs
List open PRs
gh pr list
gh pr list
View PR details
View PR details
gh pr view <number>
gh pr view <number>
View PR diff
View PR diff
gh pr diff <number>
gh pr diff <number>
Checkout PR locally
Checkout PR locally
gh pr checkout <number>
gh pr checkout <number>
Review PR status and checks
Review PR status and checks
gh pr checks <number>
gh pr checks <number>
Comment on PR
Comment on PR
gh pr comment <number> --body "LGTM! Great work."
gh pr comment <number> --body "LGTM! Great work."
Approve PR
Approve PR
gh pr review <number> --approve
gh pr review <number> --approve
Merge PR
Merge PR
gh pr merge <number> --squash
gh pr merge <number> --merge
gh pr merge <number> --rebase
**For AI Agents:**
- Automatically create PRs after completing features
- Check PR status and wait for CI checks to pass
- Add comments with analysis or suggestions
- Merge PRs after approval (when appropriate)gh pr merge <number> --squash
gh pr merge <number> --merge
gh pr merge <number> --rebase
**针对AI Agent:**
- 完成功能后自动创建拉取请求
- 检查拉取请求状态,等待CI检查完成
- 添加包含分析或建议的评论
- 在获得批准后合并拉取请求(如适用)Issue Management
问题管理
Create and Manage Issues:
bash
undefined创建和管理问题:
bash
undefinedCreate an issue
Create an issue
gh issue create --title "Bug: login fails" --body "Description of the bug"
gh issue create --title "Bug: login fails" --body "Description of the bug"
List issues
List issues
gh issue list
gh issue list
View issue details
View issue details
gh issue view <number>
gh issue view <number>
Comment on issue
Comment on issue
gh issue comment <number> --body "This is fixed in PR #123"
gh issue comment <number> --body "This is fixed in PR #123"
Close issue
Close issue
gh issue close <number>
gh issue close <number>
Reopen issue
Reopen issue
gh issue reopen <number>
**For AI Agents:**
- Create issues for bugs discovered during code review
- Link issues to PRs automatically
- Update issue status based on PR status
- Create issues from TODO comments or technical debtgh issue reopen <number>
**针对AI Agent:**
- 为代码审查中发现的bug创建问题
- 自动将问题与拉取请求关联
- 根据拉取请求状态更新问题状态
- 从TODO注释或技术债务中创建问题Repository Operations
仓库操作
Repository Management:
bash
undefined仓库管理:
bash
undefinedClone a repository
Clone a repository
gh repo clone owner/repo
gh repo clone owner/repo
View repository details
View repository details
gh repo view
gh repo view
Create a new repository
Create a new repository
gh repo create my-project --public --clone
gh repo create my-project --public --clone
Fork a repository
Fork a repository
gh repo fork owner/repo
gh repo fork owner/repo
View repository settings
View repository settings
gh repo view --web
**For AI Agents:**
- Clone repositories for analysis or contribution
- Create new repositories with proper structure
- Fork repositories for experimentationgh repo view --web
**针对AI Agent:**
- 克隆仓库用于分析或贡献
- 创建结构规范的新仓库
- 复刻仓库用于实验Branch and Commit Operations
分支与提交操作
Branch Management:
bash
undefined分支管理:
bash
undefinedList branches
List branches
gh repo view --json defaultBranchRef
gh repo view --json defaultBranchRef
Create branch from issue
Create branch from issue
gh issue develop <number> --branch feature/issue-123
**Browse GitHub Resources:**
```bashgh issue develop <number> --branch feature/issue-123
**浏览GitHub资源:**
```bashOpen current PR in browser
Open current PR in browser
gh pr view --web
gh pr view --web
Open current issue in browser
Open current issue in browser
gh issue view --web
gh issue view --web
Open repository in browser
Open repository in browser
gh repo view --web
undefinedgh repo view --web
undefinedSearch and Discovery
搜索与发现
Search Capabilities:
bash
undefined搜索功能:
bash
undefinedSearch code
Search code
gh search code "function authenticate"
gh search code "function authenticate"
Search repositories
Search repositories
gh search repos "react typescript"
gh search repos "react typescript"
Search issues
Search issues
gh search issues "bug login"
gh search issues "bug login"
Search pull requests
Search pull requests
gh search prs "is:open author:@me"
**For AI Agents:**
- Search for similar implementations before creating new code
- Find existing issues or PRs related to current work
- Discover patterns and best practices from other repositoriesgh search prs "is:open author:@me"
**针对AI Agent:**
- 在创建新代码前搜索类似实现
- 查找与当前工作相关的现有问题或拉取请求
- 从其他仓库中发现模式和最佳实践API Access
API访问
Direct API Calls:
bash
undefined直接API调用:
bash
undefinedMake authenticated API calls
Make authenticated API calls
gh api repos/:owner/:repo/pulls
gh api repos/:owner/:repo/pulls
Get specific data
Get specific data
gh api repos/:owner/:repo/pulls/123 --jq '.title, .body'
gh api repos/:owner/:repo/pulls/123 --jq '.title, .body'
POST requests
POST requests
gh api repos/:owner/:repo/issues -X POST -f title="New Issue" -f body="Description"
**For AI Agents:**
- Access GitHub API for advanced operations not covered by CLI commands
- Retrieve detailed data in JSON format for processing
- Perform bulk operations efficientlygh api repos/:owner/:repo/issues -X POST -f title="New Issue" -f body="Description"
**针对AI Agent:**
- 访问GitHub API以执行CLI命令未覆盖的高级操作
- 以JSON格式检索详细数据用于处理
- 高效执行批量操作Workflow Automation Examples
工作流自动化示例
Complete Feature Workflow:
bash
undefined完整功能工作流:
bash
undefined1. Create and checkout feature branch
1. Create and checkout feature branch
git checkout -b feature/new-feature
git checkout -b feature/new-feature
2. Make changes and commit
2. Make changes and commit
git add .
git commit -m "feat: implement new feature"
git add .
git commit -m "feat: implement new feature"
3. Push branch
3. Push branch
git push -u origin feature/new-feature
git push -u origin feature/new-feature
4. Create PR
4. Create PR
gh pr create --title "feat: implement new feature" --body "Description" --draft
gh pr create --title "feat: implement new feature" --body "Description" --draft
5. Wait for CI checks
5. Wait for CI checks
gh pr checks --watch
gh pr checks --watch
6. After approval, merge
6. After approval, merge
gh pr merge --squash --delete-branch
**For AI Agents:**
- Automate the complete PR workflow from branch creation to merge
- Monitor CI checks and retry failed workflows
- Update PR descriptions with analysis or documentation
- Clean up branches after mergegh pr merge --squash --delete-branch
**针对AI Agent:**
- 自动化从分支创建到合并的完整拉取请求工作流
- 监控CI检查并重试失败的工作流
- 使用分析或文档更新拉取请求描述
- 合并后清理分支Best Practices for AI Agents
AI Agent最佳实践
- Always authenticate first: Check before operations
gh auth status - Use appropriate PR flags: Use for work-in-progress,
--draftto auto-populate from commits--fill - Monitor CI checks: Use to wait for checks to complete
gh pr checks --watch - Provide context: Always include meaningful titles and descriptions
- Link related items: Reference issues in PR descriptions with
Closes #123 - Handle errors gracefully: Check command exit codes and provide helpful error messages
- Respect rate limits: Implement delays for bulk operations
- Use JSON output: Use flag for programmatic processing
--json
- 始终先进行身份验证: 执行操作前检查
gh auth status - 使用合适的PR参数: 对进行中的工作使用,使用
--draft从提交中自动填充内容--fill - 监控CI检查: 使用等待检查完成
gh pr checks --watch - 提供上下文: 始终包含有意义的标题和描述
- 关联相关项: 在PR描述中使用引用问题
Closes #123 - 优雅处理错误: 检查命令退出码并提供有用的错误信息
- 遵守速率限制: 对批量操作实现延迟
- 使用JSON输出: 使用标志进行程序化处理
--json
Additional Resources
额外资源
Notes
说明
- This document should be reviewed and updated regularly as best practices evolve
- Team-specific additions and modifications are encouraged
- When in doubt, refer to official documentation and community standards
- 本文档应定期审查和更新,以适应最佳实践的演变
- 鼓励根据团队需求进行补充和修改
- 如有疑问,请参考官方文档和社区标准