
Git冷命令拯救崩溃现场的技术文章大纲
Git仓库损坏修复
使用git fsck检查仓库完整性,识别损坏对象
git reflog找回丢失的提交记录,定位误删分支的最后位置
git show <commit-hash>验证特定提交内容是否可恢复
撤销危险操作
git reset --hard ORIG_HEAD回退到危险操作前的状态
git cherry-pick -n <commit>选择性恢复部分修改内容
git stash apply stash@{n}恢复被意外清除的暂存内容
分支灾难恢复
git branch -f <branch> <commit>强制重置分支指针到历史位置
git merge --abort终止产生冲突的错误合并过程
git update-ref -d refs/original/refs/heads/<branch>删除错误的备份引用
数据抢救技巧
git log -g查看所有引用日志,包括已删除的提交记录
git rev-list --objects --all列出所有可达对象,定位丢失文件
git cat-file -p <hash>直接查看Git对象内容
配置级修复
git config --global alias.rescue '!git fsck && git reflog'创建应急别名
git replace --edit <commit>修改提交内容而不改变历史哈希
git filter-repo --force重写历史时强制覆盖备份
跨仓库救援
git remote add rescue /path/to/backup.git添加备份仓库为远程源
git fetch rescue refs/recovery/*:refs/recovery/*同步救援分支
git diff rescue/main...HEAD比较当前与备份仓库的差异
预防措施
定期执行git bundle create backup.bundle --all创建离线备份包
设置git config --global gc.auto 0禁用自动垃圾回收防止数据丢失
使用git maintenance run --task=gc手动控制仓库优化时机