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

Git: Recover a Deleted Branch in 10 Seconds Using reflog — Even After a Force Push

- 30.08.26 - ErcanOPAK

🔀 Deleted ≠ Lost

You deleted a branch, or force-pushed over one, and realized seconds later it had unmerged work. Panic sets in — but Git almost never actually loses your commits. It just loses the pointer to them.

🐞 The Problem

git branch -D feature/important-work
# Deleted branch feature/important-work (was a1b2c3d).
# ...and you needed that.

🔍 Why This Is Recoverable (Cause & Effect)

Deleting a branch only removes the label pointing at a commit.
The commit object itself stays in Git's internal database until
garbage collection runs (which is infrequent, usually 30+ days by default).
=> The data still exists -- you just need to find its commit hash again.

🔢 Step 1 — Find the Lost Commit With reflog

git reflog
# a1b2c3d HEAD@{2}: commit: Add payment retry logic
# f4e5d6c HEAD@{3}: checkout: moving from main to feature/important-work
# ...
# reflog shows every place HEAD has pointed, including branches you deleted

🔢 Step 2 — Recreate the Branch From That Commit

git branch feature/important-work a1b2c3d
git checkout feature/important-work
# Your branch is back, exactly as it was before deletion

📌 Recovering From a Bad Force Push (Same Trick)

# You force-pushed and overwrote remote history
git reflog show origin/main
# Or, if you still have the old local reflog before the force push:
git reflog
# Find the commit hash BEFORE the bad force push, then:
git push origin a1b2c3d:main --force
# Restores the remote branch to its previous state

📌 If the Branch Was Deleted on GitHub/GitLab (Never Pulled Locally)

# GitHub keeps deleted branches recoverable via the "restore branch"
# button on the PR page for a limited time. If that's gone:
git fsck --no-reflogs --unreachable --lost-found
# Lists "dangling" commit objects still in the repo's object database

🛡️ Prevent This Going Forward

# Never force push without --force-with-lease -- it refuses to overwrite
# work you haven't seen yet, unlike plain --force:
git push --force-with-lease origin feature/important-work

reflog is local and defaults to a 90-day expiry — meaning almost every “I deleted my branch” panic is fully recoverable if you act before running garbage collection manually.

— Version Control Tip

Related posts:

Visualize Branch History Clearly

Git: Use git revert Instead of git reset for Shared Branches

Git: Fix a Worktree That Refuses to Check Out a Branch Already Checked Out Elsewhere

Post Views: 2

Post navigation

Ajax: Stop Double Form Submissions From Firing Duplicate Requests — Debounce and AbortController Explained
C#: Why DateTime.Now Is Silently Corrupting Your Data Across Time Zones

Leave a Reply Cancel reply

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

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

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