Class 38 of 50 • Advance Git Internals

Git Reflog: The Developer Time Machine & Recovering Lost Commits or Branches

You accidentally ran git reset --hard and erased the last three commits. Or you ran git branch -D feature/important and accidentally deleted an entire week of work. In almost every other tool, your work would be gone forever. In Git, you have git reflog—the ultimate developer safety net.

What is Git Reflog?

While git log shows the public chronological commit history of the current branch, git reflog (Reference Log) records every single movement of the HEAD pointer on your local machine. Every time you switch branches, commit, rebase, reset, merge, or cherry-pick, Git writes a permanent entry to the local reflog!

Viewing the Reflog

$ git reflog
8a2f1b4 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
4f8e2a1 HEAD@{1}: commit: feat: add Stripe payment webhook
7d1935a HEAD@{2}: checkout: moving from feature/auth to main
e69de29 HEAD@{3}: commit: fix: validate JWT expiration
b5b7b99 HEAD@{4}: rebase (finish): returning to refs/heads/main

Reading Reflog Identifiers

  • HEAD@{0}: Where HEAD is right now.
  • HEAD@{1}: Where HEAD was 1 step ago.
  • HEAD@{2}: Where HEAD was 2 steps ago.
  • HEAD@{yesterday} or HEAD@{2.hours.ago}: Time-based lookup!

Emergency Rescue 1: Undoing an Accidental git reset --hard

Suppose you accidentally ran git reset --hard HEAD~1 and wiped out commit 4f8e2a1:

# Step 1: Check reflog to find the commit hash before the reset:
$ git reflog
8a2f1b4 HEAD@{0}: reset: moving to HEAD~1
4f8e2a1 HEAD@{1}: commit: feat: add Stripe payment webhook

# Step 2: Reset HARD back to that commit!
$ git reset --hard HEAD@{1}
HEAD is now at 4f8e2a1 feat: add Stripe payment webhook

Every single line of your code and files is instantly restored!

Emergency Rescue 2: Recovering a Deleted Branch

Suppose you deleted a branch with unmerged commits: git branch -D feature/ai-chat:

  1. Run git reflog and locate the last commit made on that branch (e.g. e69de29).
  2. Recreate the branch pointing to that commit:
    $ git branch feature/ai-chat e69de29
  3. Switch to it: git switch feature/ai-chat. Your entire deleted branch is back!

How Long Does Reflog Keep Abandoned Commits?

By default, Git preserves orphaned, unreferenced commits in .git/objects/ and reflogs for 30 days, and referenced commits for 90 days before git gc (garbage collection) purges them. You have plenty of time to recover any mistake!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Undoing Destructive Resets

Use the reflog to restore commits dropped by accidental git reset --hard calls.

Recovering from Rebase Errors

Roll back messy interactive rebases to the exact pre-rebase HEAD position.

Git Reference Logs Internals

Inspect how Git records reference updates inside the .git/logs/ folder.

Troubleshooting Master Guide

Recover deleted local branches and uncommitted changes like a senior engineer.

Frequently Asked Questions (FAQ) - Student & Interview Review

No! Reflog is strictly local to your personal computer. Teammates cannot see your reflog, and cloning a repository does not download the remote author's reflog.
Reflogs are stored in plain text files inside `.git/logs/HEAD` and `.git/logs/refs/heads/`.
No. Git reflog only tracks commits and reference movements. If you wrote code in your working directory and ran `git restore .` without ever running `git add` or `git commit`, that code never entered Git's database and cannot be recovered.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git bisect Debugging → Previous: ← git cherry-pick | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!