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: Stop Committing Large Binary Files by Accident With a Pre-Commit Hook

- 31.08.26 - ErcanOPAK

🔀 One Accidental 400MB Video File Commit – and It’s in History Forever

Someone on the team accidentally commits a large binary file – a video, a database dump, a build artifact – straight into the repository. It gets caught in review and reverted in a later commit, but the repository stays bloated forever, because Git never actually forgets a blob just because a later commit stops referencing it in the working tree.

🐞 The Problem

$ git add .
$ git commit -m "Add demo video"
$ du -sh .git
1.2G  .git    <- was 40MB before this commit

# Even after deleting the file in a LATER commit,
# the 400MB blob stays in every clone's history forever.

🔍 Why This Happens

Git tracks the FULL HISTORY of every blob, not just the current
state of the working tree. Deleting a file in a new commit just
means the CURRENT tree no longer references that blob - the blob
itself, and every commit that ever pointed at it, stays in .git
permanently, and every future clone has to download it, forever,
even though nobody looking at the current code will ever see it.

✅ The Fix: Block Large Files Before They’re Committed

  • Add a pre-commit hook that checks staged file sizes and refuses the commit if anything exceeds a threshold (e.g. 5MB), with a clear message pointing to the right place for large assets (Git LFS, cloud storage, a CDN).
  • Install it as a SHARED hook via a tool like Husky or a repo-level core.hooksPath setting committed to the repo, so it applies for every teammate automatically rather than depending on each person setting it up manually.
  • For assets that genuinely need to live alongside the code (design files, sample media), use Git LFS specifically – it stores the large content outside the main object history while still versioning it properly.

📦 Simple Pre-Commit Size Check

#!/bin/sh
# .git/hooks/pre-commit (or via Husky)
MAX_SIZE=5242880  # 5MB in bytes
for file in $(git diff --cached --name-only); do
  size=$(git cat-file -s :"$file" 2>/dev/null || echo 0)
  if [ "$size" -gt "$MAX_SIZE" ]; then
    echo "❌ $file is $(($size / 1048576))MB - exceeds 5MB limit."
    echo "   Use Git LFS or external storage instead."
    exit 1
  fi
done

A commit is forever by design — the only real fix for an oversized file in history is preventing the commit in the first place, not cleaning up after it lands.

— Version Control Tip

Related posts:

Fix Last Commit Without New History

Git: Fix a Merge Conflict Loop Caused by Line-Ending Differences Between Windows and Mac

How to solve "Fatal: Not possible to fast-forward, aborting" problem

Post Views: 3

Post navigation

JavaScript: Fix a Memory Leak Caused by Event Listeners You Forgot to Remove
The AI Prompt That Plans a Realistic Moving-Day Checklist Down to the Hour

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