You're right to think about this! .gitignore itself cannot directly filter files based on their size. It works based on file names and paths.
However, you can prevent large files from being committed using other Git mechanisms, primarily Git Hooks, or manage them properly using Git LFS (Large File Storage).
Here's how you can approach this:
Option 1: Using a Pre-Commit Hook (Prevents Committing)
This is the most direct way to "ignore" files over a certain size by preventing them from being committed in the first place.
-
Navigate to your Git hooks directory:
In your repository, go to .git/hooks/.
-
Create a pre-commit script:
Create a file named pre-commit (no extension) in this directory.
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
-
Add the following script content to .git/hooks/pre-commit:
#!/bin/sh
# Maximum file size in bytes (40MB = 40 * 1024 * 1024 bytes)
MAX_SIZE_BYTES=$((40 * 1024 * 1024))
# For a slightly more readable version: MAX_SIZE_BYTES=41943040
# Get a list of staged files (added or modified)
# Using -z and xargs -0 for better handling of filenames with spaces or special characters
git diff --cached --name-only --diff-filter=AM -z | while IFS= read -r -d $'\0' file; do
# Check if the file exists (it should, as it's staged)
if [ ! -f "$file" ]; then
continue
fi
# Get the file size in bytes
# Using `wc -c` for portability, awk to strip leading spaces
FILE_SIZE_BYTES=$(wc -c < "$file" | awk '{print $1}')
if [ "$FILE_SIZE_BYTES" -gt "$MAX_SIZE_BYTES" ]; then
FILE_SIZE_MB=$(echo "$FILE_SIZE_BYTES / (1024*1024)" | bc -l | awk '{printf "%.2f", $0}')
MAX_SIZE_MB=$(echo "$MAX_SIZE_BYTES / (1024*1024)" | bc -l | awk '{printf "%.0f", $0}')
echo "--------------------------------------------------------------------------------"
echo "ERROR: File '$file' is too large (${FILE_SIZE_MB}MB)."
echo " Maximum allowed file size is ${MAX_SIZE_MB}MB."
echo " Please unstage this file or use Git LFS for large files."
echo " To unstage: git reset HEAD '$file'"
echo " To add to LFS (if not already tracked): git lfs track '$file' && git add '$file'"
echo "--------------------------------------------------------------------------------"
exit 1 # Prevent commit
fi
done
exit 0 # Allow commit
How it works:
- This script runs before every commit.
- It checks each staged file.
- If any staged file is larger than
MAX_SIZE_BYTES, it prints an error message and exits with a non-zero status, which aborts the commit.
Important Notes for Pre-Commit Hooks:
- Local to Repository: Hooks in
.git/hooks/ are not cloned with the repository or pushed to remotes. Each developer needs to set up the hook manually or you can use a tool to manage hooks (see below).
- Bypassable: A user can commit with
git commit --no-verify to bypass the hook.
- Tooling for Sharing Hooks: For teams, consider using tools like Husky (for JS projects) or pre-commit framework (Python-based, but works for any language) to manage and share hooks easily.
Option 2: Using Git LFS (Large File Storage) (Manages Large Files)
If you need to version control these large files but want to keep your main repository small and fast, Git LFS is the recommended solution. It doesn't "ignore" them in the .gitignore sense but stores them externally and keeps pointers in your Git repo.
-
Install Git LFS:
Download and install it from git-lfs.github.com.
Then run git lfs install in your repository (once per user, per machine, or git lfs install --system).
Run git lfs install in each repository you want to use it in.
-
Track file types (or specific files):
You tell LFS which files to manage. For example, to track all .psd files:
git lfs track "*.psd"
Or, if you can identify large files by a pattern that might exceed 40MB:
git lfs track "*.largevideo"
git lfs track "assets/big_files/*"
This creates/updates a .gitattributes file. You must commit .gitattributes.
-
Add and commit files as usual:
git add your_large_file.psd
git add .gitattributes
git commit -m "Add large PSD file via LFS"
git push
Git LFS will intercept the large file, store it on the LFS server (e.g., GitHub, GitLab, Bitbucket all support LFS), and commit a small pointer file to your Git repository.
Why Git LFS is often preferred for large files:
- Keeps your Git repository history small and fast.
- Handles large files efficiently.
- Supported by major Git hosting platforms.
Which to choose?
- Pre-commit hook: If you absolutely want to prevent large files from ever entering the commit history (unless explicitly bypassed). Good for enforcing a strict policy.
- Git LFS: If you need to version the large files, but want to do it efficiently. This is generally the best practice for managing large binary assets in Git.
You can even use both: a pre-commit hook to warn about untracked large files and suggest using LFS.