mcpField
G
git-advanced-workflows

git-advanced-workflows

github.com/rmyndharis开发1.9k42 次安装2 周前更新
代码仓库

关于 git-advanced-workflows

面向需要处理复杂分支和提交历史的开发者,提供高级 Git 工作流指导,涵盖历史整理、并行开发、问题定位与错误恢复。

主要功能

  1. 01交互式变基与提交整理
  2. 02跨分支应用提交
  3. 03二分查找缺陷引入提交
  4. 04使用 worktree 并行开发
  5. 05通过 reflog 恢复提交

适用场景

  1. 01合并前清理功能分支历史
  2. 02将修复应用到多个发布分支
  3. 03定位引入缺陷的提交
  4. 04恢复误删或重置的提交

安装

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

$npx skills add https://github.com/rmyndharis/antigravity-skills --skill git-advanced-workflows

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

详细介绍

Git 高级工作流

掌握高级 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. 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 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. 留意 Reflog:记住,Reflog 是你 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

评论

支持 Markdown