← Back to Git Course | Chapter 5: Undoing Changes | Lesson 3 of 6

Git reflog Command

What is Git Reflog?

git reflog shows a chronological log of every place HEAD (and each branch) has pointed in your local repository, including commits, checkouts, resets, and rebases. Because it tracks *reference movements* rather than the commit graph itself, it can find work that's no longer reachable from any branch — exactly the kind of "lost" commit a hard reset or rebase leaves behind.

Example: What is Git Reflog?

bash
git reflog

Reading the Reflog

Each reflog entry pairs a commit hash with the action that produced it (e.g. commit, checkout: moving from..., reset: moving to...), which lets you reconstruct exactly what you did and when, entry by entry, most recent first.

Example: Reading the Reflog

bash
git reflog
# a1b2c3d HEAD@{0}: commit: Fix login bug
# e4f5g6h HEAD@{1}: checkout: moving from main to feature-x

Restoring Lost Commits

To recover a commit that no branch points to anymore, find its hash in the reflog and either git checkout <hash> to inspect it or git reset --hard <hash> / git branch recovery <hash> to bring it back onto a real branch. The commit itself was never deleted — only the reference to it was.

Example: Restoring Lost Commits

bash
git reflog
git reset --hard a1b2c3d

Restoring Deleted Branches

Deleting a branch doesn't delete its commits immediately either — the reflog still remembers where that branch's tip was. Look up the last commit hash the deleted branch pointed to and run git branch <name> <hash> to recreate it exactly as it was.

Example: Restoring Deleted Branches

bash
git reflog
git branch recovered-branch a1b2c3d

Managing Reflog Lifetimes

Reflog entries aren't kept forever — unreachable ones expire after a configurable window (90 days by default, 30 for entries already unreachable when created) before Git's garbage collector can prune them. git reflog expire and git gc let you control this manually if you need to reclaim disk space sooner.

Example: Managing Reflog Lifetimes

bash
git reflog expire --expire=90.days.ago --all
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.