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

Recover Deleted Git Commits Even After Hard Reset (It’s Not Gone)

- 01.02.26 | 01.02.26 - ErcanOPAK

Accidentally did ‘git reset –hard’ and lost hours of work? Your commits aren’t deleted – they’re just orphaned. Here’s how to find them.

The Magic Command – Reflog:

git reflog

This shows every HEAD movement in your repository, even deleted commits. Output looks like:

a1b2c3d HEAD@{0}: reset: moving to HEAD~3
e4f5g6h HEAD@{1}: commit: Added user authentication  ← Your "lost" commit!
i7j8k9l HEAD@{2}: commit: Updated database schema

Recover the Commit:

# Find your commit hash from reflog
git reflog

# Reset to that commit
git reset --hard e4f5g6h

# Or create a new branch from it (safer)
git checkout -b recovery-branch e4f5g6h

Why This Works:
Git never truly deletes commits immediately. When you reset or delete a branch, Git removes the reference (pointer) but keeps the commit object. These orphaned commits exist for 30-90 days (configurable) before garbage collection removes them.

Advanced – Find Specific Lost Content:

# Search reflog for commits matching a pattern
git reflog --grep='authentication'

# Show all dangling commits (no branch points to them)
git fsck --lost-found

# View content of a dangling commit
git show COMMIT_HASH

Real Disaster Recovery Example:

# Scenario: Deleted feature branch by mistake
git branch -D feature-login
# ERROR: deleted branch 'feature-login'

# Solution 1: Find in reflog
git reflog | grep 'feature-login'
# Output: x1y2z3a HEAD@{5}: commit: Feature login complete

# Recreate branch
git checkout -b feature-login x1y2z3a

# Solution 2: Check recent commits
git fsck --no-reflogs
# Shows all unreachable commits

# Browse each one until you find your work
git log COMMIT_HASH --oneline

Prevent This Entirely – Git Aliases:

# Add safe reset aliases to ~/.gitconfig
[alias]
  # Soft reset (keeps changes in staging)
  undo = reset --soft HEAD~1
  
  # Show what would be deleted before hard reset
  check-reset = !git diff HEAD~1 HEAD
  
  # Safe hard reset with automatic backup
  safe-reset = !sh -c 'git branch backup-$(date +%Y%m%d-%H%M%S) && git reset --hard $1' -

# Usage
git safe-reset HEAD~3  # Creates backup branch automatically

Understanding Git’s 30-Day Safety Net:

# Check garbage collection settings
git config gc.reflogExpire
# Default: 90 days for reachable commits
# Default: 30 days for unreachable commits

# Extend safety net to 180 days
git config gc.reflogExpire 180.days.ago
git config gc.reflogExpireUnreachable 180.days.ago

Pro Tip – Periodic Backups:

# Create backup tags for important commits
git tag -a backup-before-refactor -m "Safety checkpoint"

# List all backup tags
git tag -l "backup-*"

# Restore from backup tag
git checkout backup-before-refactor
git checkout -b recovery-branch

Tags are permanent references. Even if you delete all branches, tagged commits never become unreachable.

Related posts:

Git: Use git diff --staged to Review Changes Before Commit

Git: Use git diff --staged to Review Changes Before Commit

Git Pull Suddenly Breaks Local Changes

Git Pull Suddenly Breaks Local Changes

Git: Writing Commits That Your Future Self Won't Hate

Git: Writing Commits That Your Future Self Won't Hate

Views: 32
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Handle AJAX Errors Properly: Retry Logic with Exponential Backoff
Speed Up Git Operations 10x by Cleaning Up Bloated Repository History

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 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 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 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 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