git

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git and GitHub Workflow Skill

Git与GitHub工作流技能指南

You are a Git and GitHub workflow specialist. This skill provides comprehensive guidance for version control, collaboration, and GitHub integration using git and the gh CLI.
你是Git与GitHub工作流专家。本技能提供了使用Git和gh CLI进行版本控制、协作及GitHub集成的全面指导。

Core Principles

核心原则

Commit Messages

提交消息规范

CRITICAL: ALL commits must use the
git-commit-message-writer
agent. This agent should auto-load when commit-related operations are detected.
Agent Auto-Loading: The
git-commit-message-writer
agent loads automatically when:
  • User requests to "commit", "create a commit", or "commit changes"
  • The system needs to generate a commit message
  • Git commit operations are being performed
Quick commands available:
  • /git:commit [context]
    - Create a commit using the git-commit-message-writer agent
  • /git:worktree <branch>
    - Create a git worktree with automatic naming
Commit message format (enforced by agent):
  • Commitizen conventional format:
    <type>(<scope>): <subject>
  • Professional, human-written style
  • Subject line: Maximum 50 characters
  • NO AI attribution: Never include "Generated with Claude Code" or "Co-Authored-By: Claude"
  • Follow project conventions
  • Clear, descriptive, focused on the "why" not just the "what"
重要提示:所有提交必须使用
git-commit-message-writer
Agent。当检测到与提交相关的操作时,该Agent应自动加载。
Agent自动加载场景: 当出现以下情况时,
git-commit-message-writer
Agent会自动加载:
  • 用户请求“commit”、“创建提交”或“提交更改”
  • 系统需要生成提交消息
  • 正在执行Git提交操作
可用快捷命令:
  • /git:commit [context]
    - 使用git-commit-message-writer Agent创建提交
  • /git:worktree <branch>
    - 创建带有自动命名的git工作区
提交消息格式(由Agent强制执行):
  • Commitizen规范格式:
    <type>(<scope>): <subject>
  • 专业、人工撰写风格
  • 主题行:最多50个字符
  • 禁止AI署名:绝不能包含“Generated with Claude Code”或“Co-Authored-By: Claude”
  • 遵循项目约定
  • 清晰、描述性强,重点说明“为什么”而非仅仅“做了什么”

GitHub Access

GitHub访问

Use
gh
CLI for all GitHub operations:
  • Issues
  • Pull requests
  • Releases
  • Repository management
  • API access
所有GitHub操作均使用
gh
CLI完成:
  • 议题(Issues)
  • 拉取请求(Pull requests)
  • 发布(Releases)
  • 仓库管理
  • API访问

Git Fundamentals

Git基础操作

Repository Setup

仓库设置

bash
undefined
bash
undefined

Initialize new repository

初始化新仓库

git init
git init

Clone existing repository

克隆现有仓库

git clone <url> git clone <url> <directory>
git clone <url> git clone <url> <directory>

Clone with specific branch

克隆指定分支

git clone -b <branch> <url>
git clone -b <branch> <url>

Clone with depth (shallow clone)

浅克隆(仅获取最新提交)

git clone --depth 1 <url>
undefined
git clone --depth 1 <url>
undefined

Basic Workflow

基础工作流

bash
undefined
bash
undefined

Check status

查看状态

git status
git status

View changes

查看更改

git diff git diff --staged git diff <file>
git diff git diff --staged git diff <file>

Stage changes

暂存更改

git add <file> git add . git add -p # Interactive staging
git add <file> git add . git add -p # 交互式暂存

Commit changes

提交更改

git commit -m "message"
git commit -m "message"

NOTE: Use commit message writer agent instead

注意:请使用提交消息编写Agent替代

Push changes

推送更改

git push git push origin <branch> git push -u origin <branch> # Set upstream
git push git push origin <branch> git push -u origin <branch> # 设置上游分支

Pull changes

拉取更改

git pull git pull --rebase # Rebase instead of merge
undefined
git pull git pull --rebase # 使用变基而非合并
undefined

Branch Management

分支管理

bash
undefined
bash
undefined

List branches

列出分支

git branch git branch -a # Include remote branches git branch -r # Remote branches only
git branch git branch -a # 包含远程分支 git branch -r # 仅显示远程分支

Create branch

创建分支

git branch <name> git checkout -b <name> # Create and switch git switch -c <name> # Modern alternative
git branch <name> git checkout -b <name> # 创建并切换分支 git switch -c <name> # 现代替代命令

Switch branches

切换分支

git checkout <branch> git switch <branch> # Modern alternative
git checkout <branch> git switch <branch> # 现代替代命令

Delete branch

删除分支

git branch -d <branch> # Safe delete git branch -D <branch> # Force delete
git branch -d <branch> # 安全删除(已合并的分支) git branch -D <branch> # 强制删除

Delete remote branch

删除远程分支

git push origin --delete <branch>
git push origin --delete <branch>

Rename branch

重命名分支

git branch -m <old-name> <new-name> git branch -m <new-name> # Rename current branch
undefined
git branch -m <old-name> <new-name> git branch -m <new-name> # 重命名当前分支
undefined

Viewing History

查看历史记录

bash
undefined
bash
undefined

View commit log

查看提交日志

git log git log --oneline git log --graph --oneline --all git log -p # Show patches git log --follow <file> # Follow file history
git log git log --oneline git log --graph --oneline --all git log -p # 显示提交补丁 git log --follow <file> # 追踪文件历史

View specific commits

查看特定提交

git show <commit> git show <commit>:<file>
git show <commit> git show <commit>:<file>

Search commits

搜索提交

git log --grep="pattern" git log --author="name" git log --since="2 weeks ago"
undefined
git log --grep="pattern" git log --author="name" git log --since="2 weeks ago"
undefined

Undoing Changes

撤销更改

bash
undefined
bash
undefined

Discard working directory changes

丢弃工作区更改

git checkout -- <file> git restore <file> # Modern alternative
git checkout -- <file> git restore <file> # 现代替代命令

Unstage changes

取消暂存更改

git reset HEAD <file> git restore --staged <file> # Modern alternative
git reset HEAD <file> git restore --staged <file> # 现代替代命令

Undo last commit (keep changes)

撤销最后一次提交(保留更改)

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

Undo last commit (discard changes)

撤销最后一次提交(丢弃更改)

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

Amend last commit

修改最后一次提交

git commit --amend
git commit --amend

NOTE: Only use --amend when:

注意:仅在以下情况使用--amend:

1. User explicitly requested amend OR

1. 用户明确要求修改

2. Adding edits from pre-commit hook

2. 添加预提交钩子的编辑内容

Revert commit (creates new commit)

回滚提交(创建新的提交)

git revert <commit>
undefined
git revert <commit>
undefined

Stashing

暂存操作

bash
undefined
bash
undefined

Stash changes

暂存更改

git stash git stash save "description"
git stash git stash save "description"

List stashes

列出暂存项

git stash list
git stash list

Apply stash

应用暂存项

git stash apply git stash apply stash@{n}
git stash apply git stash apply stash@{n}

Apply and remove stash

应用并删除暂存项

git stash pop
git stash pop

Drop stash

删除指定暂存项

git stash drop stash@{n}
git stash drop stash@{n}

Clear all stashes

清空所有暂存项

git stash clear
undefined
git stash clear
undefined

Remote Management

远程仓库管理

bash
undefined
bash
undefined

List remotes

列出远程仓库

git remote -v
git remote -v

Add remote

添加远程仓库

git remote add <name> <url>
git remote add <name> <url>

Change remote URL

修改远程仓库URL

git remote set-url <name> <url>
git remote set-url <name> <url>

Remove remote

删除远程仓库

git remote remove <name>
git remote remove <name>

Fetch from remote

从远程仓库获取更新

git fetch git fetch <remote> git fetch --all
git fetch git fetch <remote> git fetch --all

Prune deleted remote branches

清理已删除的远程分支

git remote prune origin git fetch --prune
undefined
git remote prune origin git fetch --prune
undefined

Advanced Git Operations

Git高级操作

Rebasing

变基操作

bash
undefined
bash
undefined

Rebase current branch

当前分支变基到目标分支

git rebase <base-branch> git rebase main
git rebase <base-branch> git rebase main

Interactive rebase

交互式变基

git rebase -i HEAD~3 # Last 3 commits git rebase -i <commit>
git rebase -i HEAD~3 # 针对最近3次提交 git rebase -i <commit>

Continue/abort rebase

继续/中止变基

git rebase --continue git rebase --abort
git rebase --continue git rebase --abort

Rebase with autosquash

自动合并修复提交的变基

git commit --fixup <commit> git rebase -i --autosquash <base>

**IMPORTANT**: Never use `git rebase -i` as it requires interactive input which is not supported.
git commit --fixup <commit> git rebase -i --autosquash <base>

**重要提示**:请勿使用`git rebase -i`,因为它需要交互式输入,当前环境不支持。

Merging

合并操作

bash
undefined
bash
undefined

Merge branch into current branch

将指定分支合并到当前分支

git merge <branch>
git merge <branch>

Merge without fast-forward

禁用快进合并

git merge --no-ff <branch>
git merge --no-ff <branch>

Merge with squash

压缩合并

git merge --squash <branch>
git merge --squash <branch>

Abort merge

中止合并

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

Cherry-picking

樱桃拣选(Cherry-pick)

bash
undefined
bash
undefined

Cherry-pick commit

拣选单个提交

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

Cherry-pick multiple commits

拣选多个提交

git cherry-pick <commit1> <commit2>
git cherry-pick <commit1> <commit2>

Cherry-pick range

拣选提交范围

git cherry-pick <start>..<end>
undefined
git cherry-pick <start>..<end>
undefined

Tags

标签管理

bash
undefined
bash
undefined

List tags

列出标签

git tag
git tag

Create tag

创建标签

git tag <name> git tag -a <name> -m "message" # Annotated tag
git tag <name> git tag -a <name> -m "message" # 带注释的标签

Push tags

推送标签到远程

git push origin <tag> git push origin --tags # Push all tags
git push origin <tag> git push origin --tags # 推送所有标签

Delete tag

删除标签

git tag -d <name> git push origin --delete <name>
undefined
git tag -d <name> git push origin --delete <name>
undefined

GitHub CLI (gh)

GitHub CLI (gh)

Authentication

身份验证

bash
undefined
bash
undefined

Login to GitHub

登录GitHub

gh auth login
gh auth login

Check auth status

检查认证状态

gh auth status
gh auth status

Logout

登出GitHub

gh auth logout
undefined
gh auth logout
undefined

Pull Requests

拉取请求(PR)操作

bash
undefined
bash
undefined

Create PR

创建PR

gh pr create gh pr create --title "Title" --body "Description" gh pr create --draft gh pr create --base develop --head feature-branch
gh pr create gh pr create --title "标题" --body "描述" gh pr create --draft gh pr create --base develop --head feature-branch

Using heredoc for body (recommended)

使用 heredoc 编写PR正文(推荐方式)

gh pr create --title "Feature: Add new thing" --body "$(cat <<'EOF'
gh pr create --title "Feature: Add new thing" --body "$(cat <<'EOF'

Summary

摘要

  • Added new feature
  • Fixed related bug
  • 新增功能
  • 修复相关Bug

Test plan

测试计划

  • Run unit tests
  • Test manually
EOF )"
  • 运行单元测试
  • 手动测试
EOF )"

List PRs

列出PR

gh pr list gh pr list --state open gh pr list --state closed gh pr list --author @me
gh pr list gh pr list --state open gh pr list --state closed gh pr list --author @me

View PR

查看PR详情

gh pr view gh pr view 123 gh pr view --web # Open in browser
gh pr view gh pr view 123 gh pr view --web # 在浏览器中打开

Check PR status

检查PR状态

gh pr checks gh pr checks 123
gh pr checks gh pr checks 123

Review PR

评审PR

gh pr review gh pr review 123 --approve gh pr review 123 --request-changes --body "Please fix X" gh pr review 123 --comment --body "Looks good!"
gh pr review gh pr review 123 --approve gh pr review 123 --request-changes --body "请修复X问题" gh pr review 123 --comment --body "看起来不错!"

Checkout PR

检出PR到本地

gh pr checkout 123
gh pr checkout 123

Merge PR

合并PR

gh pr merge gh pr merge 123 gh pr merge 123 --squash gh pr merge 123 --merge gh pr merge 123 --rebase
gh pr merge gh pr merge 123 gh pr merge 123 --squash gh pr merge 123 --merge gh pr merge 123 --rebase

Close/reopen PR

关闭/重新打开PR

gh pr close 123 gh pr reopen 123
gh pr close 123 gh pr reopen 123

Comment on PR

在PR下评论

gh pr comment 123 --body "Comment text"
gh pr comment 123 --body "评论内容"

Edit PR

编辑PR

gh pr edit 123 --title "New title" gh pr edit 123 --body "New description" gh pr edit 123 --add-label bug
undefined
gh pr edit 123 --title "新标题" gh pr edit 123 --body "新描述" gh pr edit 123 --add-label bug
undefined

Issues

议题(Issues)操作

bash
undefined
bash
undefined

Create issue

创建议题

gh issue create gh issue create --title "Title" --body "Description" gh issue create --label bug --label priority
gh issue create gh issue create --title "标题" --body "描述" gh issue create --label bug --label priority

List issues

列出议题

gh issue list gh issue list --state open gh issue list --assignee @me gh issue list --label bug
gh issue list gh issue list --state open gh issue list --assignee @me gh issue list --label bug

View issue

查看议题详情

gh issue view 123 gh issue view 123 --web
gh issue view 123 gh issue view 123 --web

Edit issue

编辑议题

gh issue edit 123 --title "New title" gh issue edit 123 --add-label bug gh issue edit 123 --add-assignee @me
gh issue edit 123 --title "新标题" gh issue edit 123 --add-label bug gh issue edit 123 --add-assignee @me

Close/reopen issue

关闭/重新打开议题

gh issue close 123 gh issue reopen 123
gh issue close 123 gh issue reopen 123

Comment on issue

在议题下评论

gh issue comment 123 --body "Comment text"
undefined
gh issue comment 123 --body "评论内容"
undefined

Repository Management

仓库管理

bash
undefined
bash
undefined

Create repository

创建仓库

gh repo create gh repo create my-repo --public gh repo create my-repo --private gh repo create my-repo --clone
gh repo create gh repo create my-repo --public gh repo create my-repo --private gh repo create my-repo --clone

Clone repository

克隆仓库

gh repo clone owner/repo gh repo clone owner/repo directory
gh repo clone owner/repo gh repo clone owner/repo directory

Fork repository

Fork仓库

gh repo fork gh repo fork owner/repo gh repo fork --clone
gh repo fork gh repo fork owner/repo gh repo fork --clone

View repository

查看仓库信息

gh repo view gh repo view owner/repo gh repo view --web
gh repo view gh repo view owner/repo gh repo view --web

List repositories

列出仓库

gh repo list gh repo list owner
gh repo list gh repo list owner

Archive repository

归档仓库

gh repo archive owner/repo
undefined
gh repo archive owner/repo
undefined

Workflow Management

工作流管理

bash
undefined
bash
undefined

List workflows

列出工作流

gh workflow list
gh workflow list

View workflow

查看工作流详情

gh workflow view gh workflow view <workflow-id>
gh workflow view gh workflow view <workflow-id>

Run workflow

运行工作流

gh workflow run <workflow> gh workflow run <workflow> --ref branch-name
gh workflow run <workflow> gh workflow run <workflow> --ref branch-name

List workflow runs

列出工作流运行记录

gh run list gh run list --workflow=<workflow>
gh run list gh run list --workflow=<workflow>

View run details

查看运行记录详情

gh run view gh run view <run-id>
gh run view gh run view <run-id>

Watch run

监控运行记录

gh run watch <run-id>
gh run watch <run-id>

Re-run workflow

重新运行工作流

gh run rerun <run-id>
undefined
gh run rerun <run-id>
undefined

GitHub API Access

GitHub API访问

bash
undefined
bash
undefined

Make API request

发送API请求

gh api <endpoint>
gh api <endpoint>

Get repository info

获取仓库信息

gh api repos/owner/repo
gh api repos/owner/repo

Get PR comments

获取PR评论

gh api repos/owner/repo/pulls/123/comments
gh api repos/owner/repo/pulls/123/comments

POST request

POST请求

gh api repos/owner/repo/issues --field title="Title" --field body="Body"
gh api repos/owner/repo/issues --field title="标题" --field body="正文"

With jq for JSON processing

使用jq处理JSON结果

gh api repos/owner/repo | jq '.stargazers_count'
undefined
gh api repos/owner/repo | jq '.stargazers_count'
undefined

Complete Workflows

完整工作流示例

Workflow 1: Feature Development

工作流1:功能开发

bash
undefined
bash
undefined

1. Update main branch

1. 更新主分支

git checkout main git pull
git checkout main git pull

2. Create feature branch

2. 创建功能分支

git checkout -b feature/new-thing
git checkout -b feature/new-thing

3. Make changes

3. 开发功能

... edit files ...

... 编辑文件 ...

4. Stage and commit

4. 暂存并提交

git add .
git add .

Use commit message writer agent for commit

使用提交消息编写Agent创建提交

5. Push to remote

5. 推送到远程仓库

git push -u origin feature/new-thing
git push -u origin feature/new-thing

6. Create PR

6. 创建PR

gh pr create --title "Add new feature" --body "Description"
gh pr create --title "新增功能" --body "功能描述"

7. Address review feedback

7. 处理评审反馈

... make changes ...

... 修改代码 ...

git add .
git add .

Use commit message writer agent

使用提交消息编写Agent提交

git push
git push

8. Merge PR (when approved)

8. 合并PR(通过评审后)

gh pr merge --squash
undefined
gh pr merge --squash
undefined

Workflow 2: Fix Bug in Main

工作流2:主分支Bug修复

bash
undefined
bash
undefined

1. Create hotfix branch

1. 创建热修复分支

git checkout main git pull git checkout -b hotfix/critical-bug
git checkout main git pull git checkout -b hotfix/critical-bug

2. Fix the bug

2. 修复Bug

... edit files ...

... 编辑文件 ...

3. Commit and push

3. 提交并推送

git add .
git add .

Use commit message writer agent

使用提交消息编写Agent提交

git push -u origin hotfix/critical-bug
git push -u origin hotfix/critical-bug

4. Create urgent PR

4. 创建紧急PR

gh pr create --title "Fix: Critical bug" --body "Fixes #123"
gh pr create --title "修复:严重Bug" --body "修复#123"

5. Fast-track merge

5. 快速合并

gh pr merge --merge # Keep commit history for hotfix
undefined
gh pr merge --merge # 保留热修复的提交历史
undefined

Workflow 3: Update Branch with Main

工作流3:同步主分支更新到当前分支

bash
undefined
bash
undefined

1. Fetch latest changes

1. 获取远程最新更改

git fetch origin
git fetch origin

2. Rebase on main (preferred)

2. 变基到主分支(推荐方式)

git rebase origin/main
git rebase origin/main

Or merge main (alternative)

或合并主分支(替代方式)

git merge origin/main
git merge origin/main

3. Resolve conflicts if any

3. 解决冲突(如有)

... fix conflicts ...

... 修复冲突 ...

git add . git rebase --continue # If rebasing
git add . git rebase --continue # 如果是变基操作

Or: git commit if merging

或:git commit(如果是合并操作)

4. Push (force push if rebased)

4. 推送(变基后需要强制推送)

git push --force-with-lease
undefined
git push --force-with-lease
undefined

Workflow 4: Review and Test PR

工作流4:评审并测试PR

bash
undefined
bash
undefined

1. Checkout PR

1. 检出PR到本地

gh pr checkout 123
gh pr checkout 123

2. Run tests

2. 运行测试

... test commands ...

... 执行测试命令 ...

3. Leave review

3. 提交评审意见

gh pr review --approve --body "LGTM! Tests pass."
gh pr review --approve --body "代码没问题!测试通过。"

Or request changes

或请求修改

gh pr review --request-changes --body "Please fix X"
gh pr review --request-changes --body "请修复X问题"

4. Return to your branch

4. 回到自己的分支

git checkout feature/my-work
undefined
git checkout feature/my-work
undefined

Workflow 5: Clean Up After Merge

工作流5:合并后清理

bash
undefined
bash
undefined

1. Switch to main

1. 切换到主分支

git checkout main
git checkout main

2. Pull latest

2. 获取最新代码

git pull
git pull

3. Delete local branch

3. 删除本地分支

git branch -d feature/old-feature
git branch -d feature/old-feature

4. Delete remote branch (if not auto-deleted)

4. 删除远程分支(如果未自动删除)

git push origin --delete feature/old-feature
git push origin --delete feature/old-feature

5. Prune deleted remote branches

5. 清理已删除的远程分支

git fetch --prune
undefined
git fetch --prune
undefined

Workflow 6: Release Management

工作流6:发布管理

bash
undefined
bash
undefined

1. Create release tag

1. 创建发布标签

git tag -a v1.0.0 -m "Release v1.0.0" git push origin v1.0.0
git tag -a v1.0.0 -m "发布v1.0.0" git push origin v1.0.0

2. Create GitHub release

2. 创建GitHub发布版本

gh release create v1.0.0 --title "v1.0.0" --notes "Release notes"
gh release create v1.0.0 --title "v1.0.0" --notes "发布说明"

With release assets

带发布附件的版本创建

gh release create v1.0.0 --title "v1.0.0" dist/*.tar.gz
gh release create v1.0.0 --title "v1.0.0" dist/*.tar.gz

3. List releases

3. 列出发布版本

gh release list
gh release list

4. View release

4. 查看发布版本详情

gh release view v1.0.0
undefined
gh release view v1.0.0
undefined

Best Practices

最佳实践

Branch Naming

分支命名规范

Use descriptive, categorized names:
  • feature/user-authentication
  • fix/login-bug
  • hotfix/critical-security-issue
  • refactor/cleanup-api
  • docs/update-readme
  • test/add-integration-tests
使用描述性的分类命名:
  • feature/user-authentication
  • fix/login-bug
  • hotfix/critical-security-issue
  • refactor/cleanup-api
  • docs/update-readme
  • test/add-integration-tests

Commit Guidelines

提交指南

  1. Use commit message writer agent for all commits
  2. Make atomic commits: One logical change per commit
  3. Write clear messages: Explain why, not just what
  4. Reference issues: Include issue numbers when relevant
  5. Keep commits clean: Avoid "WIP" or "fix typo" in main history
  1. 所有提交必须使用提交消息编写Agent
  2. 原子提交:每次提交对应一个逻辑更改
  3. 清晰的消息:说明“为什么”而非仅仅“做了什么”
  4. 关联议题:相关时包含议题编号
  5. 保持提交整洁:主分支历史中避免出现“WIP”或“修复拼写错误”这类提交

Pull Request Practices

拉取请求实践

  1. Keep PRs focused: One feature/fix per PR
  2. Write good descriptions: Explain what, why, and how to test
  3. Update regularly: Keep branch up to date with base
  4. Respond promptly: Address review comments quickly
  5. Use draft PRs: For work-in-progress feedback
  1. PR聚焦单一内容:每个PR对应一个功能/修复
  2. 编写优质描述:说明做了什么、为什么做以及如何测试
  3. 定期更新:保持分支与基准分支同步
  4. 及时响应:快速处理评审意见
  5. 使用草稿PR:用于获取工作进展的反馈

Git Configuration

Git配置

bash
undefined
bash
undefined

Set user info

设置用户信息

git config --global user.name "Your Name" git config --global user.email "you@example.com"
git config --global user.name "你的姓名" git config --global user.email "you@example.com"

Set default branch name

设置默认分支名称

git config --global init.defaultBranch main
git config --global init.defaultBranch main

Set pull strategy

设置拉取策略

git config --global pull.rebase true
git config --global pull.rebase true

Enable helpful colors

启用彩色输出

git config --global color.ui auto
git config --global color.ui auto

Set default editor

设置默认编辑器

git config --global core.editor vim
undefined
git config --global core.editor vim
undefined

Common Patterns

常见操作模式

Pattern 1: Quick Fix on Current Branch

模式1:当前分支快速修复

bash
git add <fixed-files>
bash
git add <fixed-files>

Use commit message writer agent

使用提交消息编写Agent提交

git push
undefined
git push
undefined

Pattern 2: Sync Fork with Upstream

模式2:同步Fork仓库与上游仓库

bash
undefined
bash
undefined

Add upstream remote (once)

添加上游远程仓库(仅需一次)

git remote add upstream <original-repo-url>
git remote add upstream <原仓库URL>

Fetch and merge upstream changes

获取并合并上游更改

git fetch upstream git checkout main git merge upstream/main git push origin main
undefined
git fetch upstream git checkout main git merge upstream/main git push origin main
undefined

Pattern 3: Squash Commits Before Merge

模式3:合并前压缩提交

bash
undefined
bash
undefined

Interactive rebase to squash (locally)

本地交互式变基压缩提交

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

Or use PR squash merge (preferred)

或使用PR压缩合并(推荐方式)

gh pr merge --squash
undefined
gh pr merge --squash
undefined

Pattern 4: Find Who Changed a Line

模式4:查找代码变更作者

bash
undefined
bash
undefined

Blame file

文件 blame 查看

git blame <file> git blame -L 10,20 <file> # Specific lines
git blame <file> git blame -L 10,20 <file> # 查看特定行范围

With gh

使用gh API查询

gh api repos/owner/repo/commits?path=<file> | jq '.[0]'
undefined
gh api repos/owner/repo/commits?path=<file> | jq '.[0]'
undefined

Pattern 5: Bisect to Find Bug

模式5:二分查找定位Bug

bash
undefined
bash
undefined

Start bisect

开始二分查找

git bisect start git bisect bad # Current commit is bad git bisect good <commit> # Known good commit
git bisect start git bisect bad # 当前提交存在Bug git bisect good <commit> # 已知正常的提交

Test each commit git presents

测试每个提交

... run tests ...

... 运行测试 ...

git bisect good # or bad
git bisect good # 或 bad

When found

找到问题提交后

git bisect reset
undefined
git bisect reset
undefined

Troubleshooting

故障排除

Issue: Merge Conflicts

问题:合并冲突

bash
undefined
bash
undefined

1. See conflicted files

1. 查看冲突文件

git status
git status

2. Resolve conflicts in each file

2. 解决每个文件的冲突

... edit files ...

... 编辑文件 ...

3. Mark as resolved

3. 标记为已解决

git add <resolved-files>
git add <resolved-files>

4. Continue operation

4. 继续操作

git rebase --continue # If rebasing git commit # If merging
undefined
git rebase --continue # 如果是变基操作 git commit # 如果是合并操作
undefined

Issue: Accidentally Committed to Wrong Branch

问题:意外提交到错误分支

bash
undefined
bash
undefined

1. Create new branch from current state

1. 从当前状态创建正确分支

git branch correct-branch
git branch correct-branch

2. Reset current branch

2. 重置当前分支

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

3. Switch to correct branch

3. 切换到正确分支

git checkout correct-branch
undefined
git checkout correct-branch
undefined

Issue: Pushed Sensitive Data

问题:推送了敏感数据

bash
undefined
bash
undefined

1. Remove from history

1. 从历史中删除文件

git filter-branch --tree-filter 'rm -f <file>' HEAD
git filter-branch --tree-filter 'rm -f <file>' HEAD

Or use BFG Repo-Cleaner (better)

或使用BFG Repo-Cleaner(更高效)

bfg --delete-files <file>

bfg --delete-files <file>

2. Force push (coordinate with team!)

2. 强制推送(需与团队沟通!)

git push --force
git push --force

3. Rotate any exposed secrets immediately

3. 立即轮换所有暴露的密钥

undefined
undefined

Issue: Need to Change Last Commit

问题:需要修改最后一次提交

bash
undefined
bash
undefined

If not pushed yet

如果尚未推送到远程

git commit --amend
git commit --amend

If already pushed (requires force push)

如果已推送(需要强制推送)

git commit --amend git push --force-with-lease

**WARNING**: Only amend when:
1. User explicitly requested amend OR
2. Adding edits from pre-commit hook

Always check authorship before amending:
```bash
git log -1 --format='%an %ae'
git commit --amend git push --force-with-lease

**警告**:仅在以下情况使用修改提交:
1. 用户明确要求修改
2. 添加预提交钩子的编辑内容

修改前务必检查作者信息:
```bash
git log -1 --format='%an %ae'

Issue: PR Has Conflicts

问题:PR存在冲突

bash
undefined
bash
undefined

Option 1: Rebase on base branch

方案1:变基到基准分支

git fetch origin git rebase origin/main git push --force-with-lease
git fetch origin git rebase origin/main git push --force-with-lease

Option 2: Merge base branch

方案2:合并基准分支

git fetch origin git merge origin/main git push
git fetch origin git merge origin/main git push

Option 3: Use gh CLI

方案3:使用gh CLI自动同步

gh pr update-branch 123
undefined
gh pr update-branch 123
undefined

Git Safety Rules

Git安全规则

  1. Never force push to main/master: Warn user if requested
  2. Never skip hooks: Don't use --no-verify unless explicitly requested
  3. Never run destructive commands: Without user confirmation
  4. Always check before amending: Verify authorship and push status
  5. Use --force-with-lease: Instead of --force when needed
  1. 绝不要强制推送到main/master分支:如果用户请求,需发出警告
  2. 绝不要跳过钩子:除非用户明确要求,否则不要使用--no-verify
  3. 绝不要随意执行破坏性操作:必须获得用户确认
  4. 修改提交前务必检查:验证作者身份和推送状态
  5. 使用--force-with-lease替代--force:当必须强制推送时

Integration with Commit Message Writer

与提交消息编写Agent的集成

After validation and before committing:
bash
undefined
验证完成后、提交前的流程:
bash
undefined

After all changes are ready

所有更改准备完成后

git add . git status # Review what will be committed git diff --staged # Review actual changes
git add . git status # 检查将被提交的内容 git diff --staged # 查看具体更改

Then request commit using commit message writer agent

然后请求使用提交消息编写Agent

Agent will:

Agent将:

1. Analyze changes

1. 分析更改内容

2. Draft professional commit message

2. 生成专业的提交消息

3. Create commit with proper format

3. 使用规范格式创建提交

4. NO AI attribution (per CLAUDE.md)

4. 提交消息中绝不包含AI署名(遵循CLAUDE.md)

undefined
undefined

Quick Reference

快速参考

bash
undefined
bash
undefined

Status and info

状态与信息查看

git status git log --oneline git diff
git status git log --oneline git diff

Branches

分支操作

git checkout -b <branch> git push -u origin <branch> git branch -d <branch>
git checkout -b <branch> git push -u origin <branch> git branch -d <branch>

Commits

提交操作

git add .
git add .

(use commit message writer)

(使用提交消息编写Agent)

git push
git push

Pull requests

PR操作

gh pr create gh pr list gh pr checkout 123 gh pr merge --squash
gh pr create gh pr list gh pr checkout 123 gh pr merge --squash

Issues

议题操作

gh issue create gh issue list gh issue view 123
gh issue create gh issue list gh issue view 123

Sync

同步操作

git pull --rebase git fetch --prune
git pull --rebase git fetch --prune

Cleanup

清理操作

git branch -d <branch> git push origin --delete <branch>
undefined
git branch -d <branch> git push origin --delete <branch>
undefined

Summary

总结

Primary directives:
  1. Always use commit message writer agent for commits
  2. Use gh CLI for GitHub operations
  3. Follow branch naming conventions
  4. Keep commits atomic and clear
  5. Never include AI attribution in commit messages
  6. Be careful with destructive operations
Most common commands:
  • git status
    - Check state
  • git checkout -b <branch>
    - New branch
  • gh pr create
    - Create PR
  • git pull --rebase
    - Stay updated
  • gh pr merge --squash
    - Merge PR
核心要求:
  1. 所有提交必须使用提交消息编写Agent
  2. 使用gh CLI完成GitHub操作
  3. 遵循分支命名规范
  4. 保持提交原子化且清晰
  5. 提交消息中绝不包含AI署名
  6. 谨慎执行破坏性操作
最常用命令:
  • git status
    - 检查仓库状态
  • git checkout -b <branch>
    - 创建并切换新分支
  • gh pr create
    - 创建拉取请求
  • git pull --rebase
    - 保持分支同步
  • gh pr merge --squash
    - 合并拉取请求