Skip to content

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
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 History Looks Clean but Bugs Appear

Search History by Content

Git: Use Cherry-Pick to Apply Specific Commits Across Branches

Post Views: 4

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 *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (753)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (753)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

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