- 安装Git LFS:git lfs install
- 创建文件 detect_large_files.sh :touch detect_large_files.sh
- 编辑文件detect_large_files.sh后保存
bash
#!/bin/bash
# 设置阈值,单位字节,100MB = 100 * 1024 * 1024
THRESHOLD=$((100 * 1024 * 1024))
# 遍历项目文件
find . -type f ! -path "./.git/*" | while read file; do
size=$(stat -c%s "$file")
if [ $size -gt $THRESHOLD ]; then
ext="${file##*.}"
echo "检测到大文件: $file (${size} bytes)"
pattern="*.$ext"
# 如果这个类型还没被 track,则添加 track 规则
if ! grep -q "$pattern" .gitattributes 2>/dev/null; then
echo "添加 Git LFS 跟踪规则: $pattern"
git lfs track "$pattern"
fi
fi
done
- 赋予脚本执行权限:chmod +x detect_large_files.sh
5.运行脚本:./detect_large_files.sh
-
查看已经跟踪的文件:git lfs track
-
把改动提交:
git add .gitattributes
git commit