Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Git

Git: Fix a Reflog Entry That Expires Before You Can Use It to Recover a Lost Commit

- 04.10.26 - ErcanOPAK

🔀 The Safety Net That Was Already Gone When You Reached for It

The reflog is what makes commands like git reset –hard or an amended commit recoverable – it keeps a local history of every position a branch tip has pointed to, even after a rewrite that makes a commit unreachable from any branch or tag. That safety net isn’t permanent, though: entries referencing unreachable commits expire out of the reflog on a timer (90 days by default) the next time git gc runs, whether gc runs automatically in the background or as part of routine maintenance. A commit you lost weeks ago and only just now realized you need can be gone for good, with no error at the moment it happens – just a reflog that quietly no longer mentions it.

🔎 The Problem

$ git reflog
# You expect to see the commit you rewrote away two months ago...
# but the entry simply isn't there anymore - no error, no warning
# at the time it was pruned, it's just absent from the list now.

$ git log --all --source -- path/to/file.cs
# Also comes up empty, because the commit is unreachable from any
# branch, tag, OR reflog entry - there is no remaining reference
# to its SHA anywhere in the local repository at all.

# What actually happened, weeks earlier, invisibly:
#   git gc ran (automatically, as it periodically does) and pruned
#   every reflog entry older than gc.reflogExpireUnreachable (default
#   30 days) that pointed at a commit unreachable from any branch.

✅ Fix: Widen the Reflog Retention Window Before You Need It, Not After

  • Set git config gc.reflogExpireUnreachable 180 (or a value matching how long you realistically need to recover work) on any machine where you do a lot of interactive rebasing, amending, or force-pushing, since the default window is shorter than many people assume.
  • If a commit is truly critical and you know its SHA, tag it or check it out onto a dedicated branch immediately, rather than relying on the reflog to hold it indefinitely – a real ref never expires the way a reflog entry does.
  • Before intentionally rewriting history you might regret, run git log –oneline -5 (or note the SHA) first, so that if you need it back quickly, you’re not depending on reflog search at all.

⚠️ Why This Is Easy to Miss

  • Nothing about losing a reflog entry produces an error or a warning at the moment it’s pruned – git gc runs silently and successfully, and the only symptom is a commit that simply can’t be found weeks or months later.
  • Most day-to-day recovery stories about the reflog happen within hours of the mistake, which creates a false sense that the reflog is a permanent undo history rather than a retention window with a quiet expiration date.

The reflog isn’t a permanent record – it’s a grace period, and grace periods run out exactly when you’re not looking at the calendar.

— Version Control Tip

Related posts:

Why git pull Is Dangerous in Long-Lived Branches

Why git pull Is Dangerous in Long-Lived Branches

Git: Fix a Tag Still Pointing at the Old Commit After a Rebase Because Tags Never Move With the Comm...

Git: Fix a Tag Still Pointing at the Old Commit After a Rebase Because Tags Never Move With the Comm...

Git: Fix a Submodule That Always Shows as Modified With No Visible Diff Because It's Checked Out Ahe...

Git: Fix a Submodule That Always Shows as Modified With No Visible Diff Because It's Checked Out Ahe...

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Docker: Fix Container Logs That Go Silent Because the App Writes to a File Instead of stdout
Windows 11: Fix Window Snap Layouts That Stop Appearing When You Drag a Window to the Screen Edge

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com