Reflog in Git
This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.
Reflog stands for Reference Log. It is an internal history of every single change made to HEAD in your local repository—functioning as Git’s hidden “undo history”.
Even if you remove the last commit or mess with history, Git still keeps track of them for some time.
git checkout HEAD^ # Refers to parent commit of current HEAD, move back 1 commit
git branch -f master # Force creation/reset of master branch to previous commit
git checkout master # Switch working directory to master branch
git log

Reflog records all movements of branches and HEAD in the repository. It contains references to the state of the repo at various points, even if those points are no longer reachable within standard git log.
1. Interactive Reflog Safety Net
Step through the interactive timeline below to see a commit disappear from git log after an accidental hard reset, and watch it get resurrected through git reflog:
Git Reflog Safety Net
Step 1 of 3: Normal History with Commits C1..C3
$ git log --oneline C3 contains your feature work. Both git log and the internal reflog show HEAD at C3.
2. Rescuing Lost Commits
Step 1: Open the Reflog
git reflog
Typical output:
c20002b HEAD@{0}: reset: moving to HEAD~1
c30003a HEAD@{1}: commit: Add auth controllers
c20002b HEAD@{2}: commit: Add database models
Notice that before the reset at HEAD@{0}, HEAD was at c30003a (HEAD@{1}).
Step 2: Restore the Commit
You can restore the commit directly:
# Reset back to the commit recorded in reflog:
git reset --hard HEAD@{1}
# Or restore by exact commit hash:
git reset --hard 9223a7a
Alternatively, you can create a new branch at that commit to safely inspect it without modifying your current branch:
git branch recovered-branch HEAD@{1}
3. How Reflog Differs from git log
| Feature | git log | git reflog |
|---|---|---|
| What it tracks | The graph of commits starting from HEAD backwards through parent commits. | The chronological journal of everywhere HEAD or branch pointers have pointed locally. |
| Scope | Shared across clones and pushes to remotes. | Strictly local (never pushed to GitHub or shared). |
After reset --hard | Abandoned commits disappear from the log. | Abandoned commits remain visible and recoverable. |
4. Reflog Lifecycle and Garbage Collection
Reflogs are saved on your local machine inside .git/logs/HEAD and .git/logs/refs/heads/.
- Retention Window: By default, unreachable commits tracked in the reflog are kept for 30 days, while reachable entries are kept for 90 days.
- Garbage Collection (
git gc): When the retention period expires, Git’s garbage collection process purges expired loose objects to save disk space. - As long as you notice a mistake within this window,
git reflogcan recover virtually any committed state!