#git-reset-hard-recovery
You accidentally ran `git reset --hard HEAD~5`. Local, not pushed. What do you do?
Что отвечать
`git reflog`, then find the entry "HEAD@{1}" (the state before the reset). It holds the SHA of what was there. From there you have two options: `git reset --hard HEAD@{1}` (move the branch back to the same place), or `git checkout -b restored HEAD@{1}` (create a branch from that point and leave the current one as it is). This works because reset moves only the branch pointer. The commits physically stay in `.git/objects/` until `git gc` runs.
Что хотят услышать
The candidate should: - name `git reflog` as the first command, no panic and no gc - explain why this works: reset does not delete commits, it only moves the ref, and the commits stay dangling until gc (by default 90 days for ones reachable from the reflog) - name `HEAD@{1}` as the idiomatic "one step back in the reflog" - warn: **do not** run `git gc --prune=now` after the screwup. You can wipe the dangling commits - say that the same approach works for `branch -D`: until gc the branch is reachable through the reflog
Подводные камни
- ✗ Running `git gc --prune=now` right after the panic wipes exactly what you wanted to recover
- ✗ Thinking reset physically erases commits. It does not, it only moves the ref
- ✗ Counting on the reflog three months later when the entry has already expired
Follow-up
- ? What does `git reflog HEAD` show after several resets?
- ? How do you recover if the reflog has already been cleaned out?
- ? Why is `git gc --prune=now` after a reset dangerous?
Глубина в базе знаний