Nâng CaoGit iconGit

Large file vô tình được commit vào git history, làm repo nặng. Xóa khỏi history mà không phá vỡ cộng tác?

File lớn trong git history không thể xóa bằng git rm đơn giản — vẫn tồn tại trong object store.

Identify large files:

bash
git rev-list --objects --all | sort -k 2 > allfiles.txt
git gc && git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10

Remove với git filter-repo (khuyến nghị):

bash
pip install git-filter-repo
git filter-repo --path large-video.mp4 --invert-paths
git filter-repo --strip-blobs-bigger-than 10M  # xóa tất cả blobs >10MB

Remove với BFG (nhanh hơn cho history dài):

bash
java -jar bfg.jar --strip-blobs-bigger-than 50M
git reflog expire --expire=now --all
git gc --prune=now

Sau khi xóa — coordinate với team:

bash
git push --force --all
git push --force --tags
# Thông báo team:
# "Repo history rewritten — please re-clone: git clone <url>"
# Đừng pull — pull sẽ không clean được

Phòng tránh lần sau:
- Git LFS cho binary assets (design files, videos, binaries)
- Custom pre-commit hook để check file size (ví dụ: find . -size +10M | grep -v .git và exit 1 nếu tìm thấy) — git config hooks.maxfilesize KHÔNG phải git config thực; phải viết script thủ công
- .gitignore cho build outputs, node_modules, log files.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git