Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

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: Fix git clean -fd Silently Deleting Untracked Files Inside an Uninitialized Submodule

- 15.10.26 - ErcanOPAK

🔀 The Clean Command That Found a Submodule Full of Strangers

git clean -fd removes untracked files and directories from the working tree – and it only special-cases a submodule directory the top-level repo currently recognizes as properly initialized. A submodule folder that was never initialized through the parent repo, or one someone cloned into directly on their own, looks to the top-level repo like any other untracked directory, and git clean -fd will delete it entirely – uncommitted local edits, local-only commits, and all – without treating it as anything special.

🔎 The Problem

# A teammate works directly inside a submodule folder without
# ever running "git submodule update --init" from the parent
# repo first - from the top level, this directory doesn't
# look like an initialized submodule yet:
cd vendor/shared-lib
git clone https://github.com/org/shared-lib.git .
# (makes local edits, commits them locally, never pushes)

# Back in the parent repo, someone runs a routine cleanup:
cd ../..
git status
# vendor/shared-lib/ shows up as untracked
git clean -fdx
# "just clean up stray build artifacts"

# Git deletes vendor/shared-lib/ entirely, local commits and
# all - -fd (and -x, which also removes ignored files) doesn't
# special-case "this happens to be its own git repository"; it
# only special-cases a submodule the top-level repo currently
# recognizes as initialized.

✅ Fix: Dry-Run Before You Ever Add -f

  • Run git clean -fdn (dry-run) first, always, and actually read the list of what would be removed before adding the real -f – a submodule directory with uncommitted work inside it looks exactly like any other untracked folder in that list.
  • Run git submodule foreach –recursive git status before any clean to make sure every submodule’s own working tree is clean and pushed first – a clean submodule has nothing left in it that clean -fd could meaningfully destroy.
  • Treat git clean -fdx (adding -x) as categorically more dangerous than -fd alone, since it also removes anything normally ignored – reserve it for a truly disposable working copy, never for a long-lived clone someone might also be using for exploratory work.

🚨 Why This Is Dangerous

  • git clean doesn’t look at the contents of a directory before deleting it, only whether the directory itself is tracked by the current repo’s index – a nested .git directory with real commit history inside is not special to the clean command at all, it’s just more bytes to remove.
  • This is the kind of mistake with no local undo – unlike a bad git reset, the deleted directory’s files aren’t sitting in any reflog or object store the parent repo knows about, because they were never part of the parent repo’s history to begin with.

git clean trusts the index to tell it what’s safe to throw away – point it at a directory the index doesn’t recognize as a submodule anymore, and it will throw that away too, commits and all.

— Version Control Tip

Related posts:

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

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

Git History Looks Clean but Lies

Git History Looks Clean but Lies

Git: Fix a Symlink That Silently Turns Into a Plain Text File After Cloning on a Filesystem With cor...

Git: Fix a Symlink That Silently Turns Into a Plain Text File After Cloning on a Filesystem With cor...

Views: 9

Post navigation

The AI Prompt That Turns a Pile of Recipe Screenshots and Bookmarks Into One Organized, Searchable Cookbook
Visual Studio: Fix a NuGet Package Reference That Silently Resolves to a Different Version Than the One Shown in the .csproj

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

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

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

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

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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