mcpField
G
git-workflow

git-workflow

github.com/bobmatnyc开发78562 次安装15 周前更新
代码仓库

关于 git-workflow

git-workflow 是面向编码 Agent 的 Git 工作流技能,汇集提交、分支、冲突处理与历史检查等实践,涵盖日常操作和进阶命令。

主要功能

  1. 01Conventional Commits 提交规范
  2. 02原子提交与分支策略
  3. 03撤销、暂存与挑选提交
  4. 04冲突处理和历史查看
  5. 05交互式变基与 bisect

适用场景

  1. 01为功能或修复创建分支
  2. 02规范提交信息并拆分变更
  3. 03从其他分支应用指定提交
  4. 04定位引入缺陷的提交

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/bobmatnyc/claude-mpm-skills --skill git-workflow

安装后在 Claude Code 中输入/git-workflow确认已加载。

附带文件metadata.json

详细介绍

Git 工作流

有效进行版本控制所需的 Git 基本模式。可为每个 Agent 减少约 120-150 行重复的 Git 指导内容。

提交最佳实践

Conventional Commits 格式
<type>(<scope>): <subject>

<body>

<footer>

类型:

  • feat:新增功能
  • fix:修复错误
  • docs:仅修改文档
  • refactor:既不修复错误也不新增功能的代码变更
  • perf:性能改进
  • test:新增或更新测试
  • chore:构建流程、依赖项、工具

示例:

feat(auth): add OAuth2 authentication

Implements OAuth2 flow using Google provider.
Includes token refresh and validation.

Closes #123

fix(api): handle null response in user endpoint

Previously crashed when user not found.
Now returns 404 with error message.

perf(db): optimize user query with index

Reduces query time from 500ms to 50ms.
原子提交
# Good: Each commit does one thing
git commit -m "feat: add user authentication"
git commit -m "test: add auth tests"
git commit -m "docs: update API docs for auth"

# Bad: Multiple unrelated changes
git commit -m "add auth, fix bugs, update docs"

分支策略

Git Flow(功能分支)
# Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-authentication

# Work on feature with regular commits
git add src/auth.py
git commit -m "feat(auth): implement login endpoint"

# Keep branch updated with main
git checkout main
git pull origin main
git checkout feature/user-authentication
git rebase main  # Or: git merge main

# Push and create PR
git push -u origin feature/user-authentication
主干开发
# Work directly on main with short-lived branches
git checkout main
git pull origin main
git checkout -b fix/null-pointer
# Make small change
git commit -m "fix: handle null in user query"
git push origin fix/null-pointer
# Merge immediately via PR

常见工作流

使用最新更改更新分支
# Option 1: Rebase (cleaner history)
git checkout feature-branch
git fetch origin
git rebase origin/main

# Resolve conflicts if any
git add resolved_file.py
git rebase --continue

# Option 2: Merge (preserves history)
git checkout feature-branch
git merge origin/main
撤销更改
# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo changes to specific file
git checkout -- file.py

# Revert a commit (creates new commit)
git revert abc123

# Amend last commit
git add forgotten_file.py
git commit --amend --no-edit
暂存工作
# Save current work temporarily
git stash

# List stashes
git stash list

# Apply most recent stash
git stash pop

# Apply specific stash
git stash apply stash@{0}

# Create named stash
git stash save "WIP: authentication feature"
挑选提交
# Apply specific commit from another branch
git cherry-pick abc123

# Cherry-pick multiple commits
git cherry-pick abc123 def456

# Cherry-pick without committing
git cherry-pick -n abc123

解决冲突

# When conflicts occur during merge/rebase
# 1. Check conflicted files
git status

# 2. Edit files to resolve conflicts
# Look for conflict markers:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name

# 3. Mark as resolved
git add resolved_file.py

# 4. Continue operation
git rebase --continue  # or git merge --continue

查看历史记录

# Compact log
git log --oneline -10

# Graphical log
git log --graph --oneline --all

# Commits by author
git log --author="John Doe"

# Commits affecting specific file
git log -- path/to/file.py

# See changes in commit
git show abc123

# Compare branches
git diff main..feature-branch

分支管理

# List branches
git branch -a  # All branches (local + remote)

# Delete local branch
git branch -d feature-branch  # Safe delete (merged only)
git branch -D feature-branch  # Force delete

# Delete remote branch
git push origin --delete feature-branch

# Rename branch
git branch -m old-name new-name

# Track remote branch
git checkout --track origin/feature-branch

标签

# Create lightweight tag
git tag v1.0.0

# Create annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"

# Push tags to remote
git push origin v1.0.0
git push origin --tags  # Push all tags

# Checkout tag
git checkout v1.0.0

# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0

高级操作

交互式变基
# Edit last 3 commits
git rebase -i HEAD~3

# Options in editor:
# pick = use commit
# reword = change commit message
# edit = stop to amend commit
# squash = combine with previous commit
# fixup = like squash but discard message
# drop = remove commit
二分查找(定位引入错误的提交)
# Start bisect
git bisect start
git bisect bad  # Current version has bug
git bisect good v1.0.0  # This version was good

# Git checks out middle commit
# Test if bug exists
git bisect bad  # if bug exists
git bisect good  # if bug doesn't exist

# Git narrows down until finding first bad commit
git bisect reset  # Return to original branch
Blame(查找修改行的作者)
# See who last modified each line
git blame file.py

# Ignore whitespace changes
git blame -w file.py

# Show specific line range
git blame -L 10,20 file.py

Git 钩子

# Pre-commit hook (runs before commit)
# .git/hooks/pre-commit
#!/bin/bash
npm run lint
npm test

# Pre-push hook (runs before push)
# .git/hooks/pre-push
#!/bin/bash
npm run test:integration

最佳实践

✅ 应该
  • 经常提交,每次只包含原子化更改
  • 编写清晰、描述性强的提交消息
  • 推送前先拉取,避免冲突
  • 提交前检查更改(git diff --staged)
  • 使用分支开发功能和修复问题
  • 保持提交小而专注
❌ 不应该
  • 提交敏感数据(使用 .gitignore)
  • 提交生成的文件(构建产物、node_modules)
  • 强制推送到共享分支(git push --force)
  • 将进行中的工作提交到 main
  • 在一次提交中包含多个无关更改
  • 重写公开历史记录

.gitignore 模式

# Dependencies
node_modules/
venv/
__pycache__/

# Build outputs
dist/
build/
*.pyc
*.o
*.exe

# IDE
.vscode/
.idea/
*.swp

# Secrets
.env
*.key
*.pem
secrets.yml

# OS
.DS_Store
Thumbs.db

# Logs
*.log
logs/

快速命令参考

# Status and diff
git status
git diff
git diff --staged

# Commit
git add .
git commit -m "message"
git push

# Branch
git branch
git checkout -b branch-name
git merge branch-name

# Update
git pull
git fetch

# Undo
git reset HEAD~1
git checkout -- file
git revert commit-hash

# History
git log
git log --oneline
git show commit-hash

请记住

  • 经常提交 — 小型提交更易于审查和还原
  • 编写描述性消息 — 未来的你会感谢现在的你
  • 推送前先拉取 — 与团队保持同步
  • 使用分支 — 保持 main 稳定
  • 提交前审查 — 检查即将提交的内容

评论

支持 Markdown