Reflog: Your Undo History for Everything

git reflog records where HEAD has been, so you can recover 'lost' commits after a bad reset, rebase, or branch delete.

The scariest Git moments — a git reset --hard to the wrong commit, a rebase that ate work, a branch deleted too soon — are almost never fatal. Git keeps a private journal of every place HEAD has pointed called the reflog.

How it saves you

Commits are not deleted the moment they leave a branch; they become unreachable and linger for weeks (default 90 days) before garbage collection. The reflog gives you their SHAs so you can point a branch back at them.

  • git reflog — list recent HEAD positions, newest first, each with an HEAD@{n} handle.
  • git reset --hard HEAD@{2} — jump back to where you were two moves ago.
  • git branch rescue <sha> — resurrect deleted work onto a fresh branch.

Reading the entries

Each line shows the action (commit, reset, rebase, checkout) so you can find the exact moment before things went wrong.

Example

Example · bash
# Oops — hard reset wiped two good commits
git reset --hard HEAD~2

# The reflog still remembers where HEAD was
git reflog
# e5f6a7b HEAD@{0}: reset: moving to HEAD~2
# 1a2b3c4 HEAD@{1}: commit: Add checkout flow
# 9d8e7f6 HEAD@{2}: commit: Add cart page

# Restore the branch to the good commit
git reset --hard HEAD@{1}

# Or rescue it onto a new branch without moving the current one
git branch recovered 1a2b3c4

When to use it

  • A developer accidentally runs git reset --hard and loses three commits, then recovers them within minutes using the reflog.
  • A team member deletes a branch before merging and uses git reflog to find the lost tip commit and re-create the branch.
  • A developer uses git reflog to pinpoint exactly when a bug was introduced by finding the last known-good HEAD entry.

More examples

Find a lost commit after reset

The reflog shows every position HEAD has occupied; you can create a new branch at any entry to recover lost commits.

Example · bash
# After accidentally running: git reset --hard HEAD~3
git reflog
# HEAD@{0}: reset: moving to HEAD~3
# HEAD@{1}: commit: feat: add checkout flow
# HEAD@{2}: commit: feat: add payment step

# Recover by creating a branch at the lost commit
git branch recovery/lost-work HEAD@{1}

Restore a deleted branch via reflog

The --all flag searches reflogs for every ref, including deleted branches, letting you recover the tip commit and restore the branch.

Example · bash
# Branch was deleted with git branch -D
git reflog --all | grep 'feature/payments'
# refs/heads/feature/payments@{0}: commit: feat: add Stripe integration

# Re-create it at its last commit
git switch -c feature/payments e4a1f3c

Inspect reflog with timestamps

The --date=iso flag shows absolute timestamps on each reflog entry, making it easy to find the exact moment a bad operation happened.

Example · bash
git reflog --date=iso
# HEAD@{2026-07-14 10:23:01 +0000}: commit: fix: resolve null pointer
# HEAD@{2026-07-14 09:55:12 +0000}: reset: moving to HEAD~1
# HEAD@{2026-07-14 09:50:44 +0000}: commit: feat: new API endpoint

Discussion

  • Be the first to comment on this lesson.