Recovering Work with Reflog
git reflog records movements of references such as HEAD and local branches. It can often help recover commits that are no longer reachable through normal branch references, such as after an accidental reset.
Reflog entries are local and are not part of the shared repository history.
Reflog Is a Safety Net
A commit disappearing from normal branch history does not immediately mean its objects are gone. Reflog can provide a route back while the relevant objects remain available.
Recover an Accidentally Reset Commit
git reflog
# Find the previous HEAD
git branch recovery abc1234
git switch recovery💡 After locating the desired historical HEAD position in the reflog, creating a new branch at that commit gives the commit a reachable reference again.
Key Takeaways
- Reflog records local reference movements.
- It is extremely useful after accidental reset or rebase operations.
- Reflog is local and should not be treated as a shared backup.
Knowledge Check
What is `git reflog` especially useful for?