关于 git-advanced-workflows
面向 Git 使用者的技能指南,涵盖历史整理、分支协作与错误恢复,并提供常用命令和操作注意事项。
主要功能
- 01交互式 rebase 整理提交历史
- 02跨分支应用指定提交
- 03用 bisect 定位引入缺陷的提交
- 04使用 worktree 并行处理分支
- 05通过 reflog 恢复提交
适用场景
- 01合并前整理提交历史
- 02追查缺陷由哪个提交引入
- 03同时开发多个功能分支
- 04恢复误删或丢失的提交
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill git-advanced-workflows安装后在 Claude Code 中输入/git-advanced-workflows确认已加载。
附带文件references/details.md
详细介绍
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
详细模式和示例
详细的模式文档位于 references/details.md。如果上述导航层级无法满足需求,请阅读该文件。
最佳实践
- 始终使用 --force-with-lease:比 --force 更安全,可避免覆盖他人的工作
- 仅变基本地提交:不要变基已推送并共享的提交
- 使用描述性提交消息:未来的你会感谢现在的你
- 原子化提交:每个提交都应对应一个独立的逻辑变更
- 强制推送前先测试:确保重写历史后没有造成问题
- 留意 Reflog:Reflog 是你的安全网,但默认情况下,当前提示位置无法访问的提交会在 30 天后过期(
gc.reflogExpireUnreachable),可访问的提交则会在 90 天后过期(gc.reflogExpire) - 执行高风险操作前先创建分支:在复杂的变基前创建备份分支
# 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
# WARNING: --hard also deletes uncommitted changes (staged and unstaged),
# and the reflog cannot recover them. Check `git status` and stash first.
git reset --hard HEAD^
# Recover a deleted branch soon. By default, git gc can prune HEAD's reflog
# entries for unmerged commits once they're 30 days old, counted from when
# each entry was recorded, not from the deletion
# Deleting a branch also deletes its own reflog; search HEAD's reflog
git reflog
git branch recovered-branch abc123
评论
支持 Markdown