The hidden gem: Git's secret undo history. Recover "deleted" branches, lost commits, and botched rebases.
You just ran git reset --hard and wiped out an hour of commits. Or you deleted a branch.
Or a rebase went sideways. git log doesn't show the lost commits anymore.
They're not gone. Git reflog remembers everything.
The reflog (reference log) is a local journal of every time HEAD moved. Every commit, checkout, rebase, reset, merge β it's all recorded, even if the commits are no longer reachable from any branch.
git refloga1b2c3d HEAD@{0}: reset: moving to HEAD~3 β you did this
f4e5d6c HEAD@{1}: commit: add search feature β THIS is what you lost
b7a8c9d HEAD@{2}: commit: add filter by tag
e0f1a2b HEAD@{3}: commit: refactor task loading
Your "lost" commits are right there at HEAD@{1}, HEAD@{2}, etc.
# See what happened
git reflog
# Find the commit before the reset, then:
git reset --hard HEAD@{1}# Oops, deleted feature branch
git branch -D feature/search
# Find where it was
git reflog | grep "feature/search"
# Recreate it
git branch feature/search HEAD@{5}# The pre-rebase state is in the reflog
git reflog
# Find the entry right before the rebase started
git reset --hard HEAD@{N}- Reflog is local only β it's not pushed or shared.
- Entries expire (default: 90 days for reachable, 30 for unreachable).
- Each ref has its own reflog:
git reflog show main,git reflog show stash. HEAD@{N}means "where HEAD was N moves ago".main@{yesterday}means "where main pointed yesterday" β time-based access!
# Where was main yesterday?
git show main@{yesterday}
# What did the repo look like 2 hours ago?
git diff main@{2.hours.ago} main
# Where was HEAD last Monday?
git log HEAD@{2024-01-15}..HEAD --onelinebash episodes/demos/03-reflog-demo.sh