mcpField
G
git

git

github.com/julianobarbosa开发1562 次安装1 周前更新
代码仓库

关于 git

git 是面向 Git 使用者的高级工作流技能,涵盖历史整理、分支协作与错误恢复。内容包括交互式 rebase、cherry-pick、bisect、worktree 和 reflog 的实用流程。

主要功能

  1. 01使用交互式 rebase 整理提交
  2. 02跨分支应用指定提交
  3. 03通过 bisect 定位引入问题的提交
  4. 04用 worktree 并行处理多个分支
  5. 05借助 reflog 恢复丢失的提交

适用场景

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

安装

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

$npx skills add https://github.com/julianobarbosa/claude-code-skills --skill git

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

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
ConventionalCommits.md
GitBestPractices.md
GitWorktree.md
SecurityChecklist.md
workflows/
Branch.md
Commit.md
CommitPush.md
Diff.md
Log.md
Merge.md
PullRequest.md
Rebase.md
Stash.md
Status.md
Sync.md
Undo.md
Worktree.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

实用工作流

工作流 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

资源

  • 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:清理已合并和过期的分支

注意事项

  • git pull --rebase 会改写共享分支上的本地提交——同事拉取该分支时会看到“已强制推送”警告,尽管你并未推送。
  • git rebase --onto X Y..HEAD 的语义:Y 不包含在内;从 Y 开始变基意味着从 Y+1 开始,而不是 Y。默认行为可能令人意外。
  • 在构建不稳定时使用 git bisect:误报的“坏”结果会破坏二分查找路径;它给出的答案在输入条件下从技术上来说是正确的。
  • git reflog 默认会过期——分支记录为 90 天,HEAD 记录为 30 天。对于重要分支,请设置 gc.reflogExpire=never。
  • 使用 git cherry-pick 时不加 -x:新提交中不会记录源 SHA,导致来源信息丢失。-x 会在提交消息中添加 (cherry picked from <sha>)。
  • git diff --no-index a b:可以比较 Git 仓库之外的两个文件,但遇到符号链接和大小写不敏感的文件系统时,路径语义会变得古怪。

评论

支持 Markdown