Git误操作急救:30秒拯救你的代码
Git误操作急救手册大纲常见误操作场景分类文件误删、提交丢失、分支误删、代码覆盖、硬重置git reset --hard后后悔文件误删恢复git checkout -- file恢复未暂存的文件修改git restore fileGit 2.23撤销工作区修改git fsck --lost-found找回已删除的未跟踪文件提交丢失恢复git reflog查看所有HEAD变更记录git cherry-pick commit_hash恢复特定提交git reset --hard HEAD{n}回退到reflog记录的某个状态分支误删处理git reflog | grep branch_name查找分支最后提交git checkout -b branch_name commit_hash基于历史提交重建分支git branch branch_name reflog_hash通过reflog恢复分支代码覆盖补救git stash list查看暂存栈历史git stash apply stash{n}恢复特定暂存版本git diff commit1 commit2 patch生成差异补丁文件硬重置后悔药git ORIG_HEAD引用指向reset前的状态git reset --hard ORIG_HEAD回退到重置前状态git fsck --dangling找回被丢弃的提交对象预防措施配置git config --global alias.safety !git add . git commit -m WIP创建安全别名设置core.hooksPath添加预提交验证钩子定期使用git bundle create backup.bundle --all创建离线备份高级恢复工具git bisect二分法定位问题提交git filter-repo重写历史记录替代git filter-branchgit archive导出特定版本代码快照可视化辅助方案GitKraken的undo功能SourceTree的日志视图VSCode GitLens插件的提交导航注所有操作前建议先执行git status确认当前状态关键操作前使用git clone --mirror创建完整仓库镜像备份。