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 Work After Accidentally Running git checkout — . on the Wrong File

- 05.09.26 - ErcanOPAK

🔀 One Wrong git checkout — . and Your Uncommitted Edits Are Gone

Meaning to discard changes in one specific file, a slightly wrong command instead runs against the whole directory – git checkout — . – silently reverting EVERY unstaged change across the entire working directory back to the last commit, including work that was never meant to be touched.

🐞 The Problem

$ git checkout -- .
# Intended: discard changes in ONE specific file
# Actual: discarded UNSTAGED changes in every file in
#          the current directory and below, silently,
#          with no confirmation prompt beforehand.

🔍 Why This Happens

"git checkout -- ." with a bare dot as the pathspec means "every
file in the current directory," not "the file I was just thinking
about" - a very easy typo to make when meaning to type a specific
filename, especially when working across multiple files and
switching context quickly. Unlike some destructive Git operations,
this one has no confirmation step and reverts unstaged changes
immediately, silently, the moment it runs.

🚨 The Hard Truth First

  • If the changes were never staged (never run through git add) and never committed anywhere, Git genuinely never had a copy of that exact content saved anywhere – this specific case is usually NOT recoverable through Git itself, unlike a dropped stash or a reset commit.

✅ The Fix: Check Every Non-Git Safety Net Before Assuming Total Loss

  • Check your EDITOR’s local history/undo feature first – VS Code, JetBrains IDEs, and many editors keep their own local file history independent of Git, often for exactly this kind of accident, and can recover content Git itself never tracked.
  • Check for OS-level file recovery/versioning (Windows File History, macOS Time Machine, a synced OneDrive/Dropbox folder’s own version history) – any of these can hold a snapshot from moments before the checkout ran, entirely separate from Git.
  • If the file WAS staged at any point before being modified further (even to a slightly older state), git fsck --unreachable can sometimes locate a dangling blob object with that staged content – worth checking, though it won’t help for changes that were never staged at all.

⚠️ Preventing This Specific Mistake Going Forward

  • Commit (or at least stage) work in small, frequent increments rather than leaving large unstaged changes sitting for a long time – staged or committed content survives this exact command, unstaged content does not.
  • Prefer the newer, more explicit git restore <filename> over git checkout -- <filename> for discarding changes – the separate ‘restore’ command was specifically introduced to reduce this class of ambiguous, high-blast-radius mistake.

Git only protects what it already knows about — staged and committed work has a safety net, but unstaged changes were only ever living in the working directory, one wrong command away from being gone.

— Version Control Tip

Related posts:

Fix Last Commit Without New History

Git: Use Submodules for External Repositories

Git: Recover a Commit You Accidentally Amended Over the Wrong Change

Post Views: 3

Post navigation

Photoshop: Fix Layer Styles That Disappear After Converting a Layer to a Smart Object
JavaScript: Why Adding a Number and a String With + Produces Unexpected Concatenation

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