The Developer's Guide to Cleaning Massive Git Repositories
The Developer's Guide to Cleaning Massive Git Repositories
As developers, we rely on Git every single day. It tracks our code, saves our history, and rescues us from mistakes. But what happens when Git turns against your hard drive?
If you aren't careful, a single .git folder can silently balloon into gigabytes of wasted storage space, slowing down your computer and making cloud backups a nightmare.
In this post, we'll explore what causes Git bloat, how to identify if your repositories are too large, and how to use powerful PowerShell commands to hunt down storage hogs across your entire system.
What is a "Safe" and Normal Git Size?
There is no strict rule for repository size because a .git folder grows slowly over time as you write more code and make more commits. However, having a general benchmark helps you spot problems early:
- 1 MB to 50 MB (Perfectly Normal): Most typical code projects (HTML, Python, JavaScript) will stay in this range for their entire lifespan. Code is just text, and text is extremely tiny!
- 50 MB to 200 MB (Normal for Large Projects): This is perfectly safe for a massive "monorepo" that contains dozens of different applications and years of history.
- 300 MB to 800 MB (Warning Zone): At this size, your
.gitfolder is getting bloated. It usually means someone accidentally committed a few small videos, compiled.exefiles, or an image dataset. GitHub will actually start sending you warning emails when your repository gets this big. - 1 GB to 5 GB+ (Danger Zone): At this size, Git becomes extremely slow, it eats up your hard drive, and platforms like GitHub will flat-out block you from uploading it.
[!IMPORTANT] The Golden Rule: If a
.gitfolder is over 100 MB, you should probably check it to make sure you aren't accidentally tracking virtual environments (venv) ornode_modules. If it's over 500 MB, it is almost certainly bloated with heavy files and needs to be cleaned using a.gitignore!
How Does Git Get So Big?
Git is incredibly efficient at storing text. However, it is terrible at storing binary files. Every time you modify a compiled binary, video, or archive, Git creates a complete, brand-new copy of that file in its history.
The most common culprits for Git bloat include:
- Python Virtual Environments (
venv/,env/) - Node Modules (
node_modules/) - Compiled Applications (
*.exe,*.apk) - Media Files (
*.mp4,*.pngdatasets) - Build Directories (
dist/,build/)
Hunting Down Bloated Repositories
If you suspect you have massive .git folders hiding on your PC, you don't have to check them manually. You can use PowerShell to scan your entire drive.
[!NOTE] Because
.gitfolders are "Hidden" by default in Windows, many scripts fail to find them. You must use the-Forceflag in PowerShell to ensure it doesn't ignore hidden directories.
1. The Real-Time Scanner
This command scans your drive and prints out every .git folder it finds in real-time. Just change D:\ to whatever drive you want to search.
Get-ChildItem -Path D:\ -Directory -Filter ".git" -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
$size = (Get-ChildItem -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum;
$sizeMB = [math]::Round($size / 1MB, 2);
Write-Host "Found: $($_.FullName) - $sizeMB MB"
}
2. The Filtered Scanner (15MB+)
If you have hundreds of tiny projects, the output above can get noisy. This refined version uses an if statement to only print repositories that are larger than 15 MB:
Get-ChildItem -Path D:\ -Directory -Filter ".git" -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
$size = (Get-ChildItem -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum;
$sizeMB = [math]::Round($size / 1MB, 2);
if ($sizeMB -gt 15) {
Write-Host "Found: $($_.FullName) - $sizeMB MB"
}
}
3. The Top 10 Leaderboard
Want to skip the scrolling and just see the worst offenders? This command will silently calculate the sizes of all repositories, sort them from largest to smallest, and print a clean table of the top 10. (Note: Because it sorts the data, it won't print anything until the entire scan is finished).
Get-ChildItem -Path D:\ -Directory -Filter ".git" -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
$size = (Get-ChildItem -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum;
[PSCustomObject]@{ Path = $_.FullName; SizeMB = [math]::Round($size / 1MB, 2) }
} | Sort-Object SizeMB -Descending | Select-Object -First 10 | Format-Table -AutoSize
How to Fix It
If you find a massive repository, the fix is a two-step process:
- Create a Bulletproof
.gitignore: Create a.gitignorefile in your project root and add entries for virtual environments (venv/), build folders (dist/), and large extensions (*.exe,*.mp4). - Purge the History: If the bloat is already in your Git history, adding a
.gitignorewon't delete the old historical copies. You will need to use a tool likegit filter-repoor simply wipe the.gitfolder and re-initialize it (git init) if you don't mind losing your commit history.
Take a few minutes today to scan your drives. You might just reclaim gigabytes of storage space you didn't even know you were missing!
