git-advanced-workflows by wshobson/agents
npx skills add https://github.com/wshobson/agents --skill git-advanced-workflows掌握高级 Git 技巧,以维护清晰的历史记录、高效协作,并自信地从任何情况中恢复。
交互式变基是 Git 历史编辑的瑞士军刀。
常用操作:
pick:保持提交不变reword:更改提交信息edit:修改提交内容squash:与上一个提交合并fixup:类似于 squash 但丢弃信息drop:完全移除提交广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
基本用法:
# 变基最近 5 个提交
git rebase -i HEAD~5
# 变基当前分支上的所有提交
git rebase -i $(git merge-base HEAD main)
# 变基到特定提交
git rebase -i abc123
将特定提交从一个分支应用到另一个分支,而无需合并整个分支。
# 拣选单个提交
git cherry-pick abc123
# 拣选一系列提交(起始提交不包含在内)
git cherry-pick abc123..def456
# 拣选但不提交(仅暂存更改)
git cherry-pick -n abc123
# 拣选并编辑提交信息
git cherry-pick -e abc123
通过提交历史进行二分查找,以找到引入错误的提交。
# 开始二分查找
git bisect start
# 将当前提交标记为错误
git bisect bad
# 标记已知的正确提交
git bisect good v1.0.0
# Git 将检出中间提交 - 测试它
# 然后标记为正确或错误
git bisect good # 或:git bisect bad
# 继续直到找到错误
# 完成后
git bisect reset
自动二分查找:
# 使用脚本自动测试
git bisect start HEAD v1.0.0
git bisect run ./test.sh
# test.sh 应在正确时退出 0,错误时退出 1-127(125 除外)
无需储藏或切换即可同时处理多个分支。
# 列出现有工作树
git worktree list
# 为功能分支添加新工作树
git worktree add ../project-feature feature/new-feature
# 添加工作树并创建新分支
git worktree add -b bugfix/urgent ../project-hotfix main
# 移除工作树
git worktree remove ../project-feature
# 清理过时的工作树
git worktree prune
你的安全网 - 跟踪所有引用的移动,甚至是已删除的提交。
# 查看引用日志
git reflog
# 查看特定分支的引用日志
git reflog show feature/branch
# 恢复已删除的提交
git reflog
# 找到提交哈希
git checkout abc123
git branch recovered-branch
# 恢复已删除的分支
git reflog
git branch deleted-branch abc123
# 从功能分支开始
git checkout feature/user-auth
# 交互式变基以清理历史
git rebase -i main
# 变基操作示例:
# - 压缩 "修复拼写错误" 的提交
# - 重写提交信息以提高清晰度
# - 按逻辑重新排序提交
# - 删除不必要的提交
# 强制推送清理后的分支(如果其他人没有使用它是安全的)
git push --force-with-lease origin feature/user-auth
# 在 main 分支上创建修复
git checkout main
git commit -m "fix: critical security patch"
# 应用到发布分支
git checkout release/2.0
git cherry-pick abc123
git checkout release/1.9
git cherry-pick abc123
# 如果出现冲突则处理
git cherry-pick --continue
# 或者
git cherry-pick --abort
# 开始二分查找
git bisect start
git bisect bad HEAD
git bisect good v2.1.0
# Git 检出中间提交 - 运行测试
npm test
# 如果测试失败
git bisect bad
# 如果测试通过
git bisect good
# Git 将自动检出下一个要测试的提交
# 重复直到找到错误
# 自动版本
git bisect start HEAD v2.1.0
git bisect run npm test
# 主项目目录
cd ~/projects/myapp
# 为紧急错误修复创建工作树
git worktree add ../myapp-hotfix hotfix/critical-bug
# 在单独的目录中处理热修复
cd ../myapp-hotfix
# 进行更改,提交
git commit -m "fix: resolve critical bug"
git push origin hotfix/critical-bug
# 返回主工作区而不中断
cd ~/projects/myapp
git fetch origin
git cherry-pick hotfix/critical-bug
# 完成后清理
git worktree remove ../myapp-hotfix
# 意外重置到错误的提交
git reset --hard HEAD~5 # 糟糕!
# 使用引用日志查找丢失的提交
git reflog
# 输出显示:
# abc123 HEAD@{0}: reset: moving to HEAD~5
# def456 HEAD@{1}: commit: my important changes
# 恢复丢失的提交
git reset --hard def456
# 或者从丢失的提交创建分支
git branch recovery def456
何时使用变基:
何时使用合并:
# 使用 main 分支的更改更新功能分支(变基)
git checkout feature/my-feature
git fetch origin
git rebase origin/main
# 处理冲突
git status
# 修复文件中的冲突
git add .
git rebase --continue
# 或者改为合并
git merge origin/main
在变基期间自动压缩修复提交。
# 进行初始提交
git commit -m "feat: add user authentication"
# 稍后,修复该提交中的某些内容
# 暂存更改
git commit --fixup HEAD # 或指定提交哈希
# 进行更多更改
git commit --fixup abc123
# 使用自动压缩进行变基
git rebase -i --autosquash main
# Git 自动标记修复提交
将一个提交拆分为多个逻辑提交。
# 开始交互式变基
git rebase -i HEAD~3
# 用 'edit' 标记要拆分的提交
# Git 将在该提交处停止
# 重置提交但保留更改
git reset HEAD^
# 按逻辑块暂存和提交
git add file1.py
git commit -m "feat: add validation"
git add file2.py
git commit -m "feat: add error handling"
# 继续变基
git rebase --continue
仅从提交中拣选特定文件。
# 显示提交中的文件
git show --name-only abc123
# 从提交中检出特定文件
git checkout abc123 -- path/to/file1.py path/to/file2.py
# 暂存并提交
git commit -m "cherry-pick: apply specific changes from abc123"
--force-with-lease:比 --force 更安全,防止覆盖他人的工作# 安全强制推送
git push --force-with-lease origin feature/branch
# 在风险操作前创建备份
git branch backup-branch
git rebase -i main
# 如果出现问题
git reset --hard backup-branch
# 中止正在进行的操作
git rebase --abort
git merge --abort
git cherry-pick --abort
git bisect reset
# 将文件恢复到特定提交的版本
git restore --source=abc123 path/to/file
# 撤销最后一次提交但保留更改
git reset --soft HEAD^
# 撤销最后一次提交并丢弃更改
git reset --hard HEAD^
# 恢复已删除的分支(90 天内)
git reflog
git branch recovered-branch abc123
每周安装量
4.8K
仓库
GitHub 星标数
32.3K
首次出现
2026年1月20日
安全审计
安装于
opencode3.6K
claude-code3.6K
gemini-cli3.5K
codex3.4K
cursor3.2K
github-copilot3.0K
Master advanced Git techniques to maintain clean history, collaborate effectively, and recover from any situation with confidence.
Interactive rebase is the Swiss Army knife of Git history editing.
Common Operations:
pick: Keep commit as-isreword: Change commit messageedit: Amend commit contentsquash: Combine with previous commitfixup: Like squash but discard messagedrop: Remove commit entirelyBasic Usage:
# 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
Apply specific commits from one branch to another without merging entire branches.
# 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
Binary search through commit history to find the commit that introduced a 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
Automated Bisect:
# 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
Work on multiple branches simultaneously without stashing or switching.
# 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
Your safety net - tracks all ref movements, even deleted commits.
# 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
# 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
# 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
# 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
# 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
# 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
When to Rebase:
When to Merge:
Integrating completed features into main
Preserving exact history of collaboration
Public branches used by others
git checkout feature/my-feature git fetch origin git rebase origin/main
git status
git add . git rebase --continue
git merge origin/main
Automatically squash fixup commits during rebase.
# 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
Break one commit into multiple logical 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
Cherry-pick only specific files from a commit.
# 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"
# 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
Weekly Installs
4.8K
Repository
GitHub Stars
32.3K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode3.6K
claude-code3.6K
gemini-cli3.5K
codex3.4K
cursor3.2K
github-copilot3.0K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装