丢 commit 时,先看 reflog。
很多“丢了”的 commit 其实还在本地,只是当前分支指针不再指向它。
git status
git log --oneline --decorate -10
git reflog -20
如果工作区还有未提交改动,先确认全部内容都属于自己并准备稍后恢复:
git stash push -u -m "backup before recover lost commit"
状态里出现其他 owner 的编辑或来源不清的未跟踪文件时,停止在这个工作区操作,保留现场或使用独立 worktree。
git reflog
找到目标 commit 后,不要急着 reset,先创建恢复分支:
git branch recovered-work <commit-sha>
git switch recovered-work
这样可以先检查内容:
<integration-branch> 是团队实际的集成分支,例如 main、master 或 develop:
git show --stat
git diff <integration-branch>...HEAD
如果从来没有 commit,Git 通常救不了,需要看编辑器、本地备份、IDE local history。