Git Advanced Workflows — Git Advanced 工作流s
v1.0.0Master advanced Git 工作流s including rebasing, cherry-picking, bisect, worktrees, and ref记录 to mAIntAIn 清理 历史 and 恢复 from any situation. Use when managing complex Git histories, collaborating on feature branches, or troubleshooting 仓库 issues.
运行时依赖
版本
# 恢复 lost commits
安装命令
点击复制技能文档
Git Advanced 工作流s
Master advanced Git techniques to mAIntAIn 清理 历史, collaborate effectively, and 恢复 from any situation with confidence.
When to Use This 技能 清理ing up commit 历史 before merging 应用lying specific commits across branches Finding commits that introduced bugs Working on multiple features simultaneously 恢复ing from Git mistakes or lost commits Managing complex branch 工作流s Preparing 清理 PRs for review 同步hronizing diverged branches Core Concepts
- Interactive Rebase
Interactive rebase is the Swiss Army knife of Git 历史 editing.
Common Operations:
pick: Keep commit as-is reword: Change commit message edit: Amend commit content squash: Combine with previous commit fixup: Like squash but discard message drop: 移除 commit entirely
Basic 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
- Cherry-Picking
应用ly 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 启动) 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
- Git Bisect
Binary 搜索 through commit 历史 to find the commit that introduced a bug.
# 启动 bisect git bisect 启动
# Mark current commit as bad git bisect bad
# Mark known good commit git bisect good v1.0.0
# Git will 检查out 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 re设置
Automated Bisect:
# Use script to test automatically git bisect 启动 HEAD v1.0.0 git bisect 运行 ./test.sh
# test.sh should exit 0 for good, 1-127 (except 125) for bad
- Worktrees
Work on multiple branches simultaneously without stashing or switching.
# 列出 existing worktrees git worktree 列出
# 添加 new worktree for feature branch git worktree 添加 ../project-feature feature/new-feature
# 添加 worktree and 创建 new branch git worktree 添加 -b bugfix/urgent ../project-hotfix mAIn
# 移除 worktree git worktree 移除 ../project-feature
# P运行e stale worktrees git worktree p运行e
- Ref记录
Your safety net - 追踪s all ref movements, even 删除d commits.
# View ref记录 git ref记录
# View ref记录 for specific branch git ref记录 show feature/branch
# 恢复 删除d commit git ref记录 # Find commit 哈希 git 检查out abc123 git branch 恢复ed-branch
# 恢复 删除d branch git ref记录 git branch 删除d-branch abc123
Practical 工作流s 工作流 1: 清理 Up Feature Branch Before PR # 启动 with feature branch git 检查out feature/user-auth
# Interactive rebase to 清理 历史 git rebase -i mAIn
# Example rebase operations: # - Squash "fix typo" commits # - Reword commit messages for clarity # - Reorder commits 记录ically # - Drop unnecessary commits
# Force push 清理ed branch (safe if no one else is using it) git push --force-with-lease origin feature/user-auth
工作流 2: 应用ly Hotfix to Multiple Releases # 创建 fix on mAIn git 检查out mAIn git commit -m "fix: critical security 补丁"
# 应用ly to release branches git 检查out release/2.0 git cherry-pick abc123
git 检查out release/1.9 git cherry-pick abc123
# Handle conflicts if they arise git cherry-pick --continue # or git cherry-pick --abort
工作流 3: Find Bug Introduction # 启动 bisect git bisect 启动 git bisect bad HEAD git bisect good v2.1.0
# Git 检查s out middle commit - 运行 tests npm test
# If tests fAIl git bisect bad
# If tests pass git bisect good
# Git will automatically 检查out next commit to test # Repeat until bug found
# Automated version git bisect 启动 HEAD v2.1.0 git bisect 运行 npm test
工作流 4: Multi-Branch Development # MAIn project directory cd ~/projects/my应用
# 创建 worktree for urgent bugfix git worktree 添加 ../my应用-hotfix hotfix/critical-bug
# Work on hotfix in separate directory cd ../my应用-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/my应用 git fetch origin git cherry-pick hotfix/critical-bug
# 清理 up when done git worktree 移除 ../my应用-hotfix
工作流 5: 恢复 from Mistakes # Accidentally re设置 to wrong commit git re设置 --hard HEAD~5 # Oh no!
# Use ref记录 to find lost commits git ref记录 # 输出 shows: # abc123 HEAD@{0}: re设置: moving to HEAD~5 # def456 HEAD@{1}: commit: my 导入ant changes
# 恢复 lost commits git re设置 --hard def456
# Or 创建 branch from lost commit git branch 恢复y def456
Advanced Techniques Rebase vs Merge Strategy
When to Rebase:
清理ing up local commits before pushing Keeping feature branch up-to-date with mAIn Creating linear 历史 for easier review
When to Merge:
Integrating completed fe