mcpField
G
git-advanced-workflows

git-advanced-workflows

github.com/ovachiever开发5647 次安装49 周前更新
代码仓库

关于 git-advanced-workflows

面向需要编辑历史、管理分支、调查缺陷或恢复丢失工作的开发者的 Git 工作流技能,提供变基、挑选提交、二分查找、工作树和 reflog 恢复的命令示例与操作流程。

主要功能

  1. 01交互式变基与历史整理
  2. 02挑选提交或指定文件
  3. 03二分查找问题提交
  4. 04使用工作树并行处理分支
  5. 05通过 reflog 恢复提交

适用场景

  1. 01提交 PR 前整理功能分支
  2. 02将修复应用到多个发布分支
  3. 03查找引入缺陷的提交
  4. 04误执行 reset 后恢复工作

安装

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

$npx skills add https://github.com/ovachiever/droid-tings --skill git-advanced-workflows

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

详细介绍

Git 高级工作流

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

何时使用此技能

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

核心概念

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 二分查找

通过提交历史进行二分查找,找出引入错误的提交。

# 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. 引用日志

你的安全网——跟踪所有引用的移动,即使是已删除的提交也不例外。

# 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 main
git checkout 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:查找错误的引入位置
# 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
git reset --hard HEAD~5  # Oh no!

# 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

# Recover lost commits
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 .
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. 始终使用 --force-with-lease:比 --force 更安全,可避免覆盖他人的工作
  2. 仅变基本地提交:不要变基已推送并共享的提交
  3. 使用描述性提交消息:未来的你会感谢现在的你
  4. 保持提交原子性:每个提交都应代表一个单独的逻辑变更
  5. 强制推送前进行测试:确保重写历史没有破坏任何内容
  6. 留意引用日志:记住引用日志是你 90 天内的安全网
  7. 高风险操作前创建分支:执行复杂变基前创建备份分支
# Safe force push
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
git reset --hard backup-branch

常见陷阱

  • 变基公共分支:会给协作者造成历史冲突
  • 不使用租约强制推送:可能覆盖队友的工作
  • 在变基中丢失工作:谨慎解决冲突,并在变基后进行测试
  • 忘记清理工作树:孤立的工作树会占用磁盘空间
  • 实验前未备份:务必创建安全分支
  • 在工作目录不干净时进行二分查找:二分查找前先提交或暂存更改

恢复命令

# 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 (within 90 days)
git reflog
git branch recovered-branch abc123

资源

  • references/git-rebase-guide.md:深入了解交互式变基
  • references/git-conflict-resolution.md:高级冲突解决策略
  • references/git-history-rewriting.md:安全地重写 Git 历史
  • assets/git-workflow-checklist.md:PR 前清理检查清单
  • assets/git-aliases.md:适用于高级工作流的实用 Git 别名
  • scripts/git-clean-branches.sh:清理已合并和过时的分支

评论

支持 Markdown