Git误操作急救手册大纲
常见误操作场景分类
基础急救工具与命令
- git reflog查看操作历史记录
- git fsck检查仓库完整性
- git cherry-pick选择性应用提交
- git stash临时保存工作区改动
提交相关误操作恢复方案
误提交后撤销最后一次提交但保留修改:
git reset –soft HEAD^
彻底删除最后一次提交(包括工作区修改):
git reset –hard HEAD^
修改最近一次提交信息:
git commit –amend
分支误操作恢复方法
恢复已删除的本地分支(需知道commit hash):
git branch <branch-name> <commit-hash>
撤销错误的合并操作:
git merge –abort
找回误删的分支(通过reflog):
git reflog –date=local | grep <branch-name>
版本回退解决方案
回退到指定版本但保留修改:
git reset <commit-hash>
彻底回退到历史版本:
git reset –hard <commit-hash>
创建回退操作的撤销点:
git revert <commit-hash>
远程仓库事故处理
恢复被覆盖的远程分支(本地需有正确版本):
git push -f origin <branch-name>@{"<commit-hash>"}
同步远程已被删除的分支:
git remote prune origin
数据丢失终极恢复方案
使用git fsck找回悬空对象:
git fsck –lost-found
从对象数据库中提取丢失内容:
git cat-file -p <object-hash> > recovered_file


