mcpField
G
git-advanced-workflows

git-advanced-workflows

github.com/sickn33开发49.1k341 次安装1 天前更新
代码仓库

关于 git-advanced-workflows

面向开发者的 Git 工作流技能,涵盖历史整理、分支协作、故障定位与恢复。它提供实用命令,并强调破坏性操作和共享历史的安全边界。

主要功能

  1. 01交互式 rebase 与提交整理
  2. 02在分支间 cherry-pick
  3. 03用 bisect 定位引入缺陷的提交
  4. 04用 worktree 并行处理分支
  5. 05通过 reflog 恢复提交
  6. 06高风险操作的安全检查

适用场景

  1. 01整理功能分支以供评审
  2. 02将修复应用到多个发布分支
  3. 03查找引入缺陷的提交
  4. 04并行处理分支或恢复提交

安装

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

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill git-advanced-workflows

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

详细介绍

Git 高级工作流

掌握高级 Git 技巧,保持整洁的历史记录、高效协作,并自信地应对各种情况。

不要在以下情况下使用此技能

  • 任务与 Git 高级工作流无关
  • 你需要使用超出此范围的其他领域或工具

说明

  • 明确目标、约束和所需输入。
  • 应用相关最佳实践并验证结果。
  • 提供可执行的步骤和验证方法。

使用场景

  • 合并前整理提交历史
  • 在分支之间应用特定提交
  • 查找引入 Bug 的提交
  • 同时开发多个功能
  • 从 Git 操作失误或提交丢失中恢复
  • 管理复杂的分支工作流
  • 准备整洁的 PR 以供审查
  • 同步发生分歧的分支

仓库与恢复边界

首先阅读仓库说明并确认分支保护规则。需要时,热修复应遵循受保护的 PR/维护者流程;紧急情况并不意味着可以直接向默认分支提交或推送。执行检出、变基、重置或还原之前,先保留未提交和已暂存的工作。备份分支只能保护已提交的历史记录,不能保护未提交的文件。执行破坏性操作前,检查确切的目标;重写共享历史前,先获得授权。reflog 的可用性取决于本地过期设置和垃圾回收;它无法保证恢复所有丢失的文件。

核心概念

1. 交互式变基

交互式变基是编辑 Git 历史记录的瑞士军刀。

常见操作:

  • pick:保留提交原样
  • reword:更改提交消息
  • edit:修改提交内容
  • squash:与前一个提交合并
  • fixup:类似于 squash,但丢弃提交消息
  • drop:彻底删除提交

基本用法:

# Rebase last 5 commits
git rebase -i HEAD~5

# Rebase all commits on current branch
git rebase -i $(git merge-base HEAD main)

# Rebase onto specific commit
git rebase -i abc123
2. 挑选提交

将一个分支中的特定提交应用到另一个分支,而不合并整个分支。

# Cherry-pick single commit
git cherry-pick abc123

# Cherry-pick range of commits (exclusive start)
git cherry-pick abc123..def456

# Cherry-pick without committing (stage changes only)
git cherry-pick -n abc123

# Cherry-pick and edit commit message
git cherry-pick -e abc123
3. Git 二分查找

通过二分搜索提交历史,找到引入 Bug 的提交。

# Start bisect
git bisect start

# Mark current commit as bad
git bisect bad

# Mark known good commit
git bisect good v1.0.0

# Git will checkout middle commit - test it
# Then mark as good or bad
git bisect good  # or: git bisect bad

# Continue until bug found
# When done
git bisect reset

自动化二分查找:

# Use script to test automatically
git bisect start HEAD v1.0.0
git bisect run ./test.sh

# test.sh should exit 0 for good, 1-127 (except 125) for bad
4. 工作树

同时在多个分支上工作,无需暂存或切换分支。

# List existing worktrees
git worktree list

# Add new worktree for feature branch
git worktree add ../project-feature feature/new-feature

# Add worktree and create new branch
git worktree add -b bugfix/urgent ../project-hotfix main

# Remove worktree
git worktree remove ../project-feature

# Prune stale worktrees
git worktree prune
5. Reflog

你的安全网——跟踪所有引用移动,包括已删除的提交。

# View reflog
git reflog

# View reflog for specific branch
git reflog show feature/branch

# Restore deleted commit
git reflog
# Find commit hash
git checkout abc123
git branch recovered-branch

# Restore deleted branch
git reflog
git branch deleted-branch abc123

实用工作流

工作流 1:PR 前整理功能分支
# Start with feature branch
git checkout feature/user-auth

# Interactive rebase to clean history
git rebase -i main

# Example rebase operations:
# - Squash "fix typo" commits
# - Reword commit messages for clarity
# - Reorder commits logically
# - Drop unnecessary commits

# Force push cleaned branch (safe if no one else is using it)
git push --force-with-lease origin feature/user-auth
工作流 2:将热修复应用到多个版本
# Create fix on an authorized topic branch from the current target
git fetch origin main
git switch -c hotfix/security origin/main
git commit -m "fix: critical security patch"

# Apply to release branches
git checkout release/2.0
git cherry-pick abc123

git checkout release/1.9
git cherry-pick abc123

# Handle conflicts if they arise
git cherry-pick --continue
# or
git cherry-pick --abort
工作流 3:查找引入 Bug 的提交
# Start bisect
git bisect start
git bisect bad HEAD
git bisect good v2.1.0

# Git checks out middle commit - run tests
npm test

# If tests fail
git bisect bad

# If tests pass
git bisect good

# Git will automatically checkout next commit to test
# Repeat until bug found

# Automated version
git bisect start HEAD v2.1.0
git bisect run npm test
工作流 4:多分支开发
# Main project directory
cd ~/projects/myapp

# Create worktree for urgent bugfix
git worktree add ../myapp-hotfix hotfix/critical-bug

# Work on hotfix in separate directory
cd ../myapp-hotfix
# Make changes, commit
git commit -m "fix: resolve critical bug"
git push origin hotfix/critical-bug

# Return to main work without interruption
cd ~/projects/myapp
git fetch origin
git cherry-pick hotfix/critical-bug

# Clean up when done
git worktree remove ../myapp-hotfix
工作流 5:从操作失误中恢复
# Accidentally reset to wrong commit
# Historical incident: a hard reset discarded local work; do not reproduce it.

# Use reflog to find lost commits
git reflog
# Output shows:
# abc123 HEAD@{0}: reset: moving to HEAD~5
# def456 HEAD@{1}: commit: my important changes

# Preserve the located commit for inspection first
git branch recovered-commit def456
# Reset only after saving dirty work and authorizing its loss
# git reset --hard def456

# Or create branch from lost commit
git branch recovery def456

高级技巧

变基与合并策略

适合变基的场景:

  • 推送前整理本地提交
  • 让功能分支与 main 保持同步
  • 创建线性历史,方便审查

适合合并的场景:

  • 将已完成的功能整合到 main
  • 保留协作过程的完整历史
  • 供他人使用的公共分支
# Update feature branch with main changes (rebase)
git checkout feature/my-feature
git fetch origin
git rebase origin/main

# Handle conflicts
git status
# Fix conflicts in files
git add <resolved-paths>
git rebase --continue

# Or merge instead
git merge origin/main
自动压缩工作流

在变基期间自动压缩修正提交。

# Make initial commit
git commit -m "feat: add user authentication"

# Later, fix something in that commit
# Stage changes
git commit --fixup HEAD  # or specify commit hash

# Make more changes
git commit --fixup abc123

# Rebase with autosquash
git rebase -i --autosquash main

# Git automatically marks fixup commits
拆分提交

将一个提交拆分为多个逻辑提交。

# Start interactive rebase
git rebase -i HEAD~3

# Mark commit to split with 'edit'
# Git will stop at that commit

# Reset commit but keep changes
git reset HEAD^

# Stage and commit in logical chunks
git add file1.py
git commit -m "feat: add validation"

git add file2.py
git commit -m "feat: add error handling"

# Continue rebase
git rebase --continue
部分挑选提交

只从某个提交中挑选特定文件。

# Show files in commit
git show --name-only abc123

# Checkout specific files from commit
git checkout abc123 -- path/to/file1.py path/to/file2.py

# Stage and commit
git commit -m "cherry-pick: apply specific changes from abc123"

最佳实践

  1. 仅在获授权时重写:获准重写主题分支时,使用带有明确预期远程 SHA 的 lease;lease 并不代表有权重写共享历史
  2. 只变基本地提交:不要变基已经推送并共享的提交
  3. 使用描述性提交消息:未来的你会感谢现在的你
  4. 保持提交原子性:每个提交都应对应一项独立的逻辑更改
  5. 强制推送前先测试:确保重写历史没有造成问题
  6. 留意 reflog:请记住,reflog 是本地恢复辅助工具,过期时间可配置;不可达条目可能会更早过期
  7. 高风险操作前创建分支:执行复杂变基前创建备份分支
# Authorized topic rewrite only; verify the expected remote head
git push --force-with-lease origin feature/branch

# Create backup before risky operation
git branch backup-branch
git rebase -i main
# If something goes wrong, inspect the backup in a separate worktree first
git worktree add --detach ../rebase-recovery backup-branch

常见陷阱

  • 对公开分支执行变基:会导致协作者遇到历史冲突
  • 不使用 lease 强制推送:可能覆盖队友的工作
  • 变基时丢失工作:谨慎解决冲突,并在变基后进行测试
  • 忘记清理工作树:遗留的工作树会占用磁盘空间
  • 实验前未备份:务必创建安全分支
  • 在工作目录不干净时执行 bisect:执行 bisect 前先提交或暂存更改

恢复命令

# Abort operations in progress
git rebase --abort
git merge --abort
git cherry-pick --abort
git bisect reset

# Restore file to version from specific commit
git restore --source=abc123 path/to/file

# Undo last commit but keep changes
git reset --soft HEAD^

# Undo last commit and discard changes
git reset --hard HEAD^

# Recover deleted branch while the commit remains available locally
git reflog
git branch recovered-branch abc123

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审查的替代品。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。

评论

支持 Markdown