Git Reflog—Your Time Machine for Lost Commits

Git Reflog—Your Time Machine for Lost Commits

Ever run , deleted a branch, or had a messy rebase leave you thinking: "Oh no, where did my commit go?" Don't panic! Git has a secret weapon to help you recover your lost work: git reflog. Its actually ref-log (reference of logs), not re-flog!

This guide breaks it down so anyone can understand and use effectively.

What is git reflog?

Think of as Git's memory. Every time you move (e.g., via commits, resets, rebases, or checkouts), Git logs that movement in the reflog. Even if a commit no longer appears in , it might still be accessible in the reflog—for a limited time.

Common Use Cases

1. Recovering from a Hard Reset

You ran:

And now your latest commit is gone.

Solution:

Run:

You'll see output like:

To recover your commit:

  • View it:

  • Or restore it:

2. Restoring an Accidentally Deleted Branch

You deleted a branch with:

And now you need it back.

Solution:

Run:

Locate the last commit hash from the deleted branch, then recreate it:

Your branch is restored!

3. Fixing a Messy Rebase

If a rebase caused chaos and commits seem lost:

Run:

Find the entry before the rebase, then restore that state:

Replace with the reflog entry number for the desired state.

How Long Does Reflog Last?

  • Local branches: Retained for ~90 days by default.

  • Unreachable (dangling) commits: Retained for ~30 days.

After these periods, Git may garbage collect the data, so act quickly!

Quick Reflog Commands

Final Tip

Think of as your undo history. It’s not permanent, but it can save you from Git disasters if you act fast. Keep this guide handy for your next “I thought I lost everything” moment!


#Git, #Reflog, #Version_Control, #Recover_commits, #Git #Tips

To view or add a comment, sign in

Others also viewed

Explore topics