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

Git: Using Git Hooks for Automation

Git: Using Git Hooks for Automation

Git Conflicts Repeat in the Same Areas

Git Conflicts Repeat in the Same Areas

Git: Surgical Stashing - Don't Save Everything!

Git: Surgical Stashing - Don't Save Everything!

Views: 10

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 *

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,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

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

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

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