Reflog in Git

🔁
Course Under Active Revision Re-visiting

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

Untitled

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
1 / 3
main
master
C1
#c1 Setup app
C2
#c2 Add models
HEAD → master
C3
#c3 Add controllers
$ 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

Featuregit loggit reflog
What it tracksThe graph of commits starting from HEAD backwards through parent commits.The chronological journal of everywhere HEAD or branch pointers have pointed locally.
ScopeShared across clones and pushes to remotes.Strictly local (never pushed to GitHub or shared).
After reset --hardAbandoned 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 reflog can recover virtually any committed state!