Rescuing Deleted Git Commits from Automated Nightly Runners
说明与用途
作者描述了一起自动化运行器在报告推送失败后删除包含未推送提交的本地分支的事件。通过 git fsck 成功恢复数据。脚本已修复:通过比较 HEAD 验证提交,从父仓库推送,并在确认远程成功后才删除分支。
问题
An automated nightly runner reported push failures and requested manual intervention, but had already deleted the local branches and worktrees containing the unpushed commits, effectively destroying the work it claimed needed saving.
结果
Four repositories were successfully recovered using git fsck. The automation script was patched to prevent future data loss by verifying state directly rather than relying on error messages, and by ensuring destructive cleanup only happens after verified remote success.
工作流
- Identify dangling commits using
git fsck --no-reflogs --lost-found. - Inspect the dangling commit hash using
git log -1 --oneline <hash>to verify it is the lost work. - Create a new branch pointing to the dangling commit using
git branch rescue/<date> <hash>. - Fix the automation script to check for commits by comparing HEAD before and after the child process runs.
- Fix the automation script to push from the parent checkout instead of the worktree.
- Fix the automation script to only delete the worktree/branch if the remote has all commits (unpushed count is 0).
证据
The rescue is three commands... git fsck --no-reflogs --lost-found... git branch rescue/2026-08-21 3f9c2a1
The destructive step in any automation has to be conditioned on the verified success, not the reported one.
- 记录日期: 2026-09-02T14:26:17.809Z