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: 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:

How to create a .gitignore file in Windows

How to create a .gitignore file in Windows

Git: Fix Background Maintenance Silently Running a Full gc Mid-Workday Because maintenance.auto Was ...

Git: Fix Background Maintenance Silently Running a Full gc Mid-Workday Because maintenance.auto Was ...

Git History Looks Clean but Bugs Exist

Git History Looks Clean but Bugs Exist

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

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 *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 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# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 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 584 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