#git-reflog-time-machine
What is the reflog, how do you use it, and how long do entries live?
Что отвечать
The reflog is a local log of HEAD and ref movements. Every time a branch moves (commit, reset, rebase, checkout), Git writes an entry to `.git/logs/HEAD` and `.git/logs/refs/heads/<branch>`. `git reflog` shows "where I have been," and `HEAD@{2}` means "two steps back." Entries live 90 days by default for reachable ones and 30 days for unreachable ones, and they get cleaned up by `git gc`. **This is local only.** A server usually has no reflog.
Что хотят услышать
The candidate should: - name the reflog as a rescue after a `reset --hard`, a failed rebase, or an accidental `branch -D` - show the recovery workflow: `git reflog` -> find the SHA -> `git checkout -b restored <sha>` or `git reset --hard <sha>` - explain that the reflog is LOCAL. If a colleague force-pushed to main, you have your own reflog (until you run gc), they have theirs, but there is no history on the server - name the settings `gc.reflogExpire` (90 days) and `gc.reflogExpireUnreachable` (30 days), plus `git gc --prune=now` as the way to clean up (or, conversely, to keep things) - mention `git reflog <branch>` for viewing the reflog of a specific branch, not just HEAD
Подводные камни
- ✗ Thinking the reflog exists on the server. It does not, and on a bare repo it is usually disabled
- ✗ Running `git gc --prune=now --aggressive` right after a screwup. You can wipe out exactly what you wanted to recover
- ✗ Relying on the reflog after 3 months. The entry has most likely already expired and been collected by gc
Follow-up
- ? How do you configure the reflog so it never expires?
- ? What does `git reflog refs/heads/main` show?
- ? Why does the reflog not help other developers after your force-push?
Глубина в базе знаний